# Segment pink color spots from image

조회 수: 5 (최근 30일)
Elysi Cochin 2022년 7월 25일
댓글: Elysi Cochin 2022년 10월 20일
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.

댓글을 달려면 로그인하십시오.

### 채택된 답변

DGM 2022년 7월 25일
편집: DGM 2022년 7월 25일
This is one example.
% 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;
mkpink = bwareaopen(mkpink,100);
mkpink = imfill(mkpink,'holes');
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이전 댓글 1개 표시이전 댓글 1개 숨기기
DGM 2022년 10월 19일
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.
[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];
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;
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];
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;
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.
[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];
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;
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];
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;
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.
[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];
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;
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
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;
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 2022년 10월 20일

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Abderrahim. B 2022년 7월 25일
편집: Abderrahim. B 2022년 7월 25일
Hi!
I recommend to use Color Thresholder.
Workflow I used to get the pink color spots (picture attached) using the app is as follow:
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

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Region and Image Properties에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by