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

'3d matrices' 'rgb image' 'roi' 'find element'Image Processing Toolbox

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 =)

Best Answer

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));