# selecting and removing all grey pixels in an RGB image

23 views (last 30 days)
Sara on 5 Jan 2012
Commented: Image Analyst on 3 May 2017
I have a set of fused optical and infrared images I need to analyse. The optical part of the image is visually greyscale and the infrared part is represented by colored blobs. The size, location, range and intensity of each blob changes with each image. Each image is an RGB 480 * 640 * 3 matrix of rgb values. I want to isolate the thermal section from each image and turn the optical portion to black.
Obviously, I can't apply a simple threshold to this because the "grey" part still ranges from 0:255. Edge detection and normal image techniques aren't working well because the grey part of the image is very noisy and grainy. I don't want to convert the whole image to grey because then I'd lose the colour mapping of the thermal portion.
I thought the best way would be masking out the grey images was to find where each pixel that is the same in each colour dimension.
for i = 0:255
A = im(:,:,1) & im(:,:,2) & im(:,:,3) == i;
end
That is obviously just for one image. I thought that if I could get a matrix of A which shows all the pixel locations where rgb is the same in all dimensions, for each value of i, I could use that to mask the non-thermal parts of my photo.
But the code above only identifies about 20 pixels, and not all of them are in the right locations. I'm not a programmer so none of this is obvious to me.
Where am I going wrong?

Titus Edelhofer on 5 Jan 2012
Hi,
what about looking for all pixels that are somewhat "greyish"? Instead of R, G and B exactly the same you could test
idx = abs(im(:,:,1)-im(:,:,2))<=tresh & abs(im(:,:,2)-im(:,:,3))<=thresh;
Thresh could be 1 or 2 (give it a try) so a pixel [50 51 50] would be considered grey as well.
Titus
##### 2 CommentsShow 1 older commentHide 1 older comment
Sara on 5 Jan 2012
Ignore that, it's working fine now. Thanks again for your help, it is brilliant.

### More Answers (4)

Image Analyst on 5 Jan 2012
You can't deal with the pseudocolored image. You need to obtain the actual grayscale infrared image. If that image is just one of the color planes and the optical image is in the other two color channels, then you're in luck. If not, you're going to have to find out how to obtain the thermal image by itself. I've dealt with thermal cameras so I know it can be done.
##### 1 CommentShow NoneHide None
Sara on 5 Jan 2012
Thank you. Nope, the IR isn't in one channel, which is why I had to search for shades of grey across each channel. I did manage to pull out those interest regions perfectly this time, but that is probably due more to luck from the way I set up the original IR fusion. I wouldn't do it the same way next time, it would probably be safer to take thermal and digital photos separately then fuse them AFTER selecting the interest features.

Sara on 5 Jan 2012
For info, this is what I used to mask through the code and create a corrected image with only the thermal features and everything else blacked out:
idx = abs(I(:,:,1)-I(:,:,2))<=thresh & abs(I(:,:,2)-I(:,:,3))<=thresh;
figure(2);imshow(idx)
[x1 y1 z1] = size(I);
I2 = I;
for x = 1:x1
for y = 1:y1
for z = 1:z1
if idx(x,y) == 1
I2(x,y,z) = 0;
else I2(x,y,z) = I2(x,y,z);
end
end
end
end
It might not have been necessary loop through the whole image like that but I couldn't think of a better way.
##### 1 CommentShow NoneHide None
Titus Edelhofer on 5 Jan 2012
Hi Sara,
for sake of completeness: in your solution at least the most inner loop could be saved: inside the y-loop it would have been sufficient to write
if idx(x,y)==1
I2(x,y,:) = 0;
end
Titus

Titus Edelhofer on 5 Jan 2012
Hi Sara,
the trick is to change the image from NxMx3 to (N*M)x3:
% store the size
s = size(I);
% make I a matrix with 3 columns and 480*640 rows:
I = reshape(I, s(1)*s(2), 3);
% apply the "filter:
I(idx(:),:) = 0;
% restore the original size
I = reshape(I, s);
Titus
Image Analyst on 8 Apr 2015
Abdoo, I think you missed my comment, between your two, about posting a new, separate question in a separate thread.
In the meantime, see my File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862. My demos there are very close to what you want to do.

Gebra maryam Alehegn on 2 May 2017
How to read multiple image from folder and extract RGB color features????
##### 1 CommentShow NoneHide None
Image Analyst on 3 May 2017
With imread() and regionprops().