How to detect RGB values from a ROI into the whole image?

4 views (last 30 days)
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 =)

Accepted Answer

Guillaume
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
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
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???
Valentina Cala'
Valentina Cala' on 5 Jun 2017
I'm sorry, my english isn't so good. I uploaded an image to explain the probelm in a simpler way.
I have the image at the left. I'm able to select a ROI (for example, the central image) and then I'm also able to display the ROI with respect to the whole image (the image at the right) in which the points outside the ROI are in grayscale while the points inside it are coloured. I wrote a code able to do all these parts, while I didn't find the way to display an image in which they are displaied as colored also points outside the selected ROI. For example, if the point at coordinate (1,1) has the same red, green and blue value of one point inside the selected ROI, I want to see it as colored, not in grayscale.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!