How to change the color of each pixel in a masked image?
8 views (last 30 days)
Show older comments
I found the Euclidean distances between each pixel of the mask and six reference colors. Now i want to change the color of every pixel which is in the skin lesion (masked image). Each pixel has to change color and will take the color of the shortest euclidean distance ( for example if the shortest euclidean distance is the one with red color , the pixel will turn into red). So I have to create a new image in which every pixel of mask will obtain one color of the 6 reference colors. Then the last step is to check if the number of pixels belonging to each reference colour exceeds by 5% of the total number of pixels of the lesion. If this occurs the color score is incremented by 1, otherwise the color score of each color is 0.
Any idea for doing this procedure ?
I attach my matlab code so far .
Accepted Answer
Image Analyst
on 11 Oct 2022
You just didn't use the min function like I suggested in your other post. Here's the relevant snippet. The full demo is attached.
% Stack all the delta E into a 3-D image.
stack_array = cat(3,deImage_bg,deImage_wh,deImage_bl,deImage_red,deImage_db,deImage_lbr);
% Produce a classified image where the value is the slice that has the lowest delta E.
[~, classifiedImage] = min(stack_array, [], 3);
hFigClassified = figure;
imshow(classifiedImage, [])
impixelinfo;
cmap = lines(6);
colormap(cmap);
hcb = colorbar(gca,'Ticks', 1:6, 'TickLabels', {'BlueGray', 'White', 'Black', 'Red', 'Dark Brown', 'LightBrown'});
hcb.Label.String = 'Color Category';
hold on;
visboundaries(lesionBoundary);
hold off;
title('Classified Image', 'FontSize',fontSize)
drawnow;
hFigClassified.WindowState = "maximized";
Here is the image showing the classifications. As you can see, most of the melanoma is classified as being in the dark brown category.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1152538/image.png)
7 Comments
Image Analyst
on 12 Oct 2022
One of the colors (#1) was not in the mask area so the histogram did not include that one. It's probably best to specify the edges for histogram so that the first and last bin are known and constant.
figure
% Get the histogram of classes within the mask.
edges = 0:6
histObj = histogram(classifiedImage(mask), edges, 'Normalization','probability')
% Show fractions of each class in the mask area.
probs = histObj.Values
probs =
Columns 1 through 4
0 0.000903117785839316 0 0.00423991814438046
Columns 5 through 6
0 0.99485696406978
More Answers (1)
Benjamin Thompson
on 11 Oct 2022
See if this helps. There is probably a faster way to do it with color mapping functions in the Image Processing Toolbox but it does not take very long on your image size.
4 Comments
Image Analyst
on 12 Oct 2022
OK, try the attached.
Snippet:
figure
% Get the histogram of classes within the mask.
edges = 0.5:1:6.5
pixelValuesInMaskOnly = classifiedImage(mask);
histObj = histogram(pixelValuesInMaskOnly, edges, 'Normalization','probability')
grid on;
ylabel('Area Fraction (0 - 1)')
xticklabels(colorNames);
title('Fractions for Each Color Class')
% Show fractions of each class in the mask area.
probs = histObj.Values
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1154193/image.png)
probs =
Columns 1 through 4
0.000903117785839316 0 0.00423991814438046 0
Columns 5 through 6
0.99485696406978 0
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!