Segment pink color spots from image

6 views (last 30 days)
I = imread('input_image');
figure, imshow(I)
How to extract the pink colour spots in the attached image and find how much area it occupies in the light orange/peach colour region?
I tried imbinarize and multithresh, but cant identify the spots correctly. Please could someone help me to extract the pink spots and find the area it occupies in the peach colour region.

Accepted Answer

DGM
DGM on 25 Jul 2022
Edited: DGM on 25 Jul 2022
This is one example.
A = imread('pink.jpg');
% HSV thresholds for pink areas
th = [0.95 0.04;
0.15 1.00;
0.61 1.00];
% get pink areas
[H S V] = imsplit(rgb2hsv(A));
mkH = H >= th(1,1) | H <= th(1,2);
mkS = S >= th(2,1) & S <= th(2,2);
mkV = V >= th(3,1) & V <= th(3,2);
mkpink = mkH & mkS & mkV;
% clean up mask
mkpink = bwareaopen(mkpink,100);
mkpink = imfill(mkpink,'holes');
% get overall object mask
mkobj = V > 0.5 & S > 0.091;
mkobj = bwareaopen(mkobj,100);
mkobj = imfill(mkobj,'holes');
imshow(imfuse(mkpink,mkobj))
% get area ratio
arearatio = nnz(mkpink)/nnz(mkobj)
arearatio = 0.0825
Bear in mind that's not perfect. The white underlay (paper?) at the RHS is difficult to exclude from the object mask, since white doesn't separate well in HSV and reliance on S is severely limited due to the damage caused by the JPG compression. Also note that I ignored the presence of the staples.
  3 Comments
DGM
DGM on 19 Oct 2022
1 and 4 are similar and fairly easy to process, but the colors are different than the prior image. I'm going to do this slightly different today for sake of consistency. I'm going to use similar processes to get the mask defining the paper and the mask defining the spots.
A = imread('4.jpg');
[H S V] = imsplit(rgb2hsv(A));
% HSV thresholds for paper areas
% note that hue lower bound is above upper bound
% this is because hue is cyclic and the range crosses zero
th = [0.749 0.438;
0.00 1.00;
0.50 1.00];
% get paper mask
mkH = H >= th(1,1) | H <= th(1,2); % union instead of intersection!
mkS = S >= th(2,1) & S <= th(2,2);
mkV = V >= th(3,1) & V <= th(3,2);
mkpaper = mkH & mkS & mkV;
% clean up mask
mkpaper = bwareaopen(mkpaper,100); % get rid of specks
mkpaper = imclose(mkpaper,strel('disk',15)); %try to fill in staples
mkpaper = imfill(mkpaper,'holes'); %try to fill in staples
% HSV thresholds for blue spot areas
th = [0.223 0.521;
0.00 1.00;
0.00 0.904];
% get spot mask
mkH = H >= th(1,1) & H <= th(1,2);
mkS = S >= th(2,1) & S <= th(2,2);
mkV = V >= th(3,1) & V <= th(3,2);
mkspot = mkH & mkS & mkV;
% clean up mask
mkspot = mkspot & mkpaper; % spots only exist on the paper
mkspot = bwareaopen(mkspot,100); % get rid of specks
mkspot = imclose(mkspot,strel('disk',15)); %try to fill in staples
mkspot = imfill(mkspot,'holes'); %try to fill in staples
imshow(imfuse(mkspot,mkpaper))
spotratio = nnz(mkspot)/nnz(mkpaper)
spotratio = 0.0134
The other two are problematic in their own ways. Image 3 has very poorly-defined boundaries.
A = imread('3.jpg');
[H S V] = imsplit(rgb2hsv(A));
% HSV thresholds for paper areas
% note that hue lower bound is above upper bound
% this is because hue is cyclic and the range crosses zero
th = [0.749 0.438;
0.00 1.00;
0.50 1.00];
% get paper mask
mkH = H >= th(1,1) | H <= th(1,2); % union instead of intersection!
mkS = S >= th(2,1) & S <= th(2,2);
mkV = V >= th(3,1) & V <= th(3,2);
mkpaper = mkH & mkS & mkV;
% clean up mask
mkpaper = bwareaopen(mkpaper,100); % get rid of specks
mkpaper = imclose(mkpaper,strel('disk',15)); %try to fill in staples
mkpaper = imfill(mkpaper,'holes'); %try to fill in staples
% HSV thresholds for red spot areas
th = [0.857 0.040;
0.202 1.00;
0.00 0.904];
% get spot mask
mkH = H >= th(1,1) | H <= th(1,2);
mkS = S >= th(2,1) & S <= th(2,2);
mkV = V >= th(3,1) & V <= th(3,2);
mkspot = mkH & mkS & mkV;
% clean up mask
mkspot = mkspot & mkpaper; % spots only exist on the paper
mkspot = bwareaopen(mkspot,100); % get rid of specks
mkspot = imclose(mkspot,strel('disk',15)); %try to fill in staples
mkspot = imfill(mkspot,'holes'); %try to fill in staples
imshow(imfuse(mkspot,mkpaper))
spotratio = nnz(mkspot)/nnz(mkpaper)
spotratio = 0.2396
Image 2 is smaller than the others, so morphological and area-based operations might need to be adjusted if there are other images that are small like this. Image 2 also has a blown-out region that's not really seperable from the image without causing problems isolating the yellow paper from the white paper.
A = imread('2.jpg');
[H S V] = imsplit(rgb2hsv(A));
% HSV thresholds for paper areas
% note that hue lower bound is above upper bound
% this is because hue is cyclic and the range crosses zero
th = [0.749 0.438;
0.00 1.00;
0.50 1.00];
% get paper mask
mkH = H >= th(1,1) | H <= th(1,2); % union instead of intersection!
mkS = S >= th(2,1) & S <= th(2,2);
mkV = V >= th(3,1) & V <= th(3,2);
mkpaper = mkH & mkS & mkV;
% clean up mask
mkpaper = bwareaopen(mkpaper,100); % get rid of specks
mkpaper = imclose(mkpaper,strel('disk',15)); %try to fill in staples
mkpaper = imfill(mkpaper,'holes'); %try to fill in staples
% HSV thresholds for red spot areas
th = [0.857 0.040;
0.202 1.00;
0.00 1.00]; % can't tighten V due to bright spot
% get spot mask
mkH = H >= th(1,1) | H <= th(1,2);
mkS = S >= th(2,1) & S <= th(2,2);
mkV = V >= th(3,1) & V <= th(3,2);
mkspot = mkH & mkS & mkV;
% clean up mask
mkspot = mkspot & mkpaper; % spots only exist on the paper
mkspot = bwareaopen(mkspot,100); % get rid of specks
mkspot = imclose(mkspot,strel('disk',15)); %try to fill in staples
mkspot = imfill(mkspot,'holes'); %try to fill in staples
imshow(imfuse(mkspot,mkpaper))
spotratio = nnz(mkspot)/nnz(mkpaper)
spotratio = 0.0625
Note that all of these examples are identical except for the spot mask creation.
Things like staples, the background paper (white), and shadows are sources of area error. Things like vague boundaries are likely to cause inaccurate selection if thresholds are reused without care.
The thresholds can be at least initially obtained using the Color Thresholder app.
If the conditions are variable and there are only a few samples to check, it may even be simpler to just generate the paper mask and then generate the spot mask by manual selection. Your eyes and brain are better at identifying blob extents than an unattended fixed color-based segmentation routine.
Elysi Cochin
Elysi Cochin on 20 Oct 2022
Thank you so much Sir, for your reply.

Sign in to comment.

More Answers (1)

Abderrahim. B
Abderrahim. B on 25 Jul 2022
Edited: Abderrahim. B on 25 Jul 2022
Hi!
I recommend to use Color Thresholder.
Workflow I used to get the pink color spots (picture attached) using the app is as follow:
1- Read the image
I = imread('input_image');
2- Start the app like if it was a function
colorThresholder(I)
3- Select HSV color space
4- Tune H, S and V until you segment the pink spots . Mainly H .
5. You can export the segmented image as well as generate a function for your processing.
Hope this helps

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!