Replacing RGB value for image segmentation

2 次查看(过去 30 天)
Hi!
Is it possible, for certain RGB values ​​of pixels in an image, to make modifications like this below?
For example, for pixels with values ​​in ranges: Red between 170 - 120, Green 150 - 120 and Blue between 70 - 50, I would not like to modify. But for pixels that are not in this range, I'd like to make them in black color.
Thanks.

采纳的回答

Image Analyst
Image Analyst 2019-3-20
Sure. Just make a mask and apply it:
rgbImage = imread('peppers.png');
subplot(2, 1, 1);
imshow(rgbImage);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Create a mask for each channel and use it to zero out the image.
mask = ~(redChannel >= 120 & redChannel <= 170);
redChannel(mask) = 0;
mask = ~(greenChannel >= 120 & greenChannel <= 150);
greenChannel(mask) = 0;
mask = ~(blueChannel >= 50 & blueChannel <= 70);
blueChannel(mask) = 0;
% Recombine separate color channels into a single, true color RGB image.
rgbImage2 = cat(3, redChannel, greenChannel, blueChannel);
subplot(2, 1, 2);
imshow(rgbImage2);
0001 Screenshot.png
  2 个评论
Bruno Fcav
Bruno Fcav 2019-3-20
It did not work very well for what I need, but it must have been my mistake. I'd like to segment only the light green parts of the figure. For this I studied some color trends for the areas of interest and arrived at the ranges of values ​​that I proposed in my question. I'll try to change the methodology ...
Image Analyst
Image Analyst 2019-3-21
It was a mistake to try to segment this in RGB color space. You need to use a colorimetric color space like HSV. I used the Color Thresholder app on the Apps tab of the tool ribbon to determine thresholds to use in HSV color space. Try the attached demo.
0001 Screenshot.png

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by