How to detect RGB values from a ROI into the whole image?
4 views (last 30 days)
Show older comments
Valentina Cala'
on 4 Jun 2017
Commented: Valentina Cala'
on 6 Jun 2017
Hi! I'm trying to create an interface to semplify the evaluation of an image. in particular, I found the way to select one point of the RGB image, store its RGB value and then display the original image in which pixels with the same RGB value of the selected poit are colored while the other are in grayscale.
Now, i'd like to consider a section of the image (i got it) and then display colored points on the original RGB image only at the coordinates corresponding to RGB values equal to one of that included into the selected region. I tried to do that using loops but it reuired too much time. I tried also applying:
arrayfun(@(x,y,j,k) isequal(image(x,y,:),ROI(j,k,:)), image, ROI)
but it doesn't work. I''m going crazy!
Is there anyone able to solve it? Thanks in advance =)
0 Comments
Accepted Answer
Guillaume
on 5 Jun 2017
Edited: Guillaume
on 5 Jun 2017
You don't need a loop, not even arrayfun to do what you want. You basically want to find which elements of one set (the full image) belongs to another set (the roi) where each element is an RGB triplet. The function for set membership in matlab is ismember with the 'row' option. Since ismember operates on rows, you need to reshape both image and roi into rows of RGB triplets. You can reshape the result back into 2D afterward:
isinroi = ismember(reshape(fullimage, [], 3), reshape(roi, [], 3), 'rows');
isinroi = reshape(isinroi, size(fullimage));
More Answers (1)
Image Analyst
on 4 Jun 2017
You need to mask your masks with the mask of the other image. Let's say the subsection is a box between row1 and row2, and column1 and column2. Then create a mask for that. Let's call it rectMask:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Make rect mask
rectMask = false(size(redChannel)); % Initialize.
rectMask(row1:row2, column1:column2) = true;
Now make the mask of what pixels match your specified color, but mask that mask with rectMask
redMask = (redChannel == desiredRedValue) & rectMask;
greenMask = (greenChannel == desiredGreenValue) & rectMask;
blueMask = (blueChannel == desiredBlueValue) & rectMask;
% Get overall mask of what pixels match the desired color
mask = redMask & greenMask & blueMask;
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
Now, the only pixels that will appear are those that are both in the box, and match your desired color. All other pixels will be black.
3 Comments
Image Analyst
on 4 Jun 2017
Please explain " coordinates corresponding to RGB values equal to one of that included into the selected region". I though "selected region" meant a rectangular box that you described as "a section of the image (i got it)".
Please post annotated images showing the "region" and where the colored pixels are to be located with respect to that region - inside, outside, or "covering the whole original image". It's not clear what you want, because "included into the selected region" seems contradictory to "covering the whole original image". Do you want the whole image or not???
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!