How to clasify the color of each specific pixel to some constant

1 visualizzazione (ultimi 30 giorni)
Hello , I have this image where I have to relate the color of each pixel to some known constant. Let's say, red(including all pixels that are reddish, slightly red, etc.)=STRONG pixels; yellow(in the region of yellow)=WEAKER pixels; and so on... I have to do this for each pixel of my image and the problem I'm having is there are so many possibilities of getting a color somewhat red/reddish/pinkish (with rgb values being different) & all of them must be under the same STRONG pixels category.I've tried coding by taking ranges of values for rgb differenes, but I believe there should be another/clearer way to do so, which won't leave any pixel that is not in the range I've specified. My code is as following:
RedPIXELS='1010-808kPa';
YellowPIXELS='700-600kPa';
img=imread('1.jpg');
for k=1:vidHeight-1
if img(k,k,1)>250 && img(k,k,1)-img(k,k,2)>100 && img(k,k,1)-img(k,k,3)>100
img(k,k)=RedPIXELS;
if img(k,k,2)-img(k,k,1)<50 && img(k,k,3)<10
img(k,k)=YellowPIXELS;
end
end
end

Risposta accettata

Walter Roberson
Walter Roberson il 17 Gen 2016
No, there is no simpler or clearer way to do it along the lines you are proceeding, not if you want accuracy. The names that people associate with various RGB colors never form regular regions in RGB space, so you would have to code a lot of comparisons if you want to proceed like you are.
  2 Commenti
Anar Alshanbayeva
Anar Alshanbayeva il 17 Gen 2016
Modificato: Walter Roberson il 17 Gen 2016
Thank you, but my code still not seem to work even if I use these specifications. Is it logically correct or can you see major mistake somewhere that keeps repeating?
I completed it as such:
img=imread('1.jpg'); % Reading the first interested frame
for k=1:vidHeight-1 % The loop for one image
if img(k,k,1)>150 && img(k,k,1)-img(k,k,2)>130 && img(k,k,1)-img(k,k,3)>130
img(k,k)=RedEl;
end% For the RedEl=RedElasticity level determination
if img(k,k,2)>150 && img(k,k,2)-img(k,k,1)>130 && img(k,k,2)-img(k,k,3)>130
img(k,k)=GreenEl;
end
if img(k,k,3)>150 && img(k,k,3)-img(k,k,1)>130 && img(k,k,3)-img(k,k,2)>130
img(k,k)=BlueEl;
end
if img(k,k,1)>150 && img(k,k,1)-img(k,k,2)<130 && img(k,k,1)-img(k,k,3)>130
img(k,k)=OrangeEl;
end
if img(k,k,1)>200 && img(k,k,2)>200 && img(k,k,3)<150
img(k,k)=YellowEl;
end
end
Walter Roberson
Walter Roberson il 17 Gen 2016
Is there a reason that you are only examining along the major diagonal, and that you are assigning the resulting value back to the original matrix instead of to a different matrix? Note that assigning to img(k,k) like you do is the same as assigning to img(k,k,1) which is the red plane of the location you just examined. Also notice that with the structure of your code, when you examine img(k,k,1) for the determination of GreenEl, you are examining the img(k,k,1) that you might have just written to above in its alternate name of img(k,k)=RedEl .

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 17 Gen 2016
Walter's right - eventually it comes down to thresholding, which is a part of color classification where you say if the color meets your threshold criteria then it belongs in that class. There are some functions that make that decision for you, such as rgb2ind() but they may not carve up the 3D color gamut exactly like you'd decide to. If you want good control you can use threshold, but to be robust to changes in illumination, you might want to do it in a different color space, such as HSV. Go to the Apps tab, and call up the Color Thresholder app. Then click load and load in your image. Then pick a color space from those shown, and then mess around with the thresholds to interactively get something you like. Then have it export the code which you can then paste into your own app.
By the way, I have several color segmentation demos in my File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862 Feel free to adapt them to get different colors.

Community Treasure Hunt

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

Start Hunting!

Translated by