selecting and removing all grey pixels in an RGB image

조회 수: 32 (최근 30일)
Sara
Sara 2012년 1월 5일
댓글: Image Analyst 2017년 5월 3일
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
Titus Edelhofer 2012년 1월 5일
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
Sara
Sara 2012년 1월 5일
Thank you! That worked perfectly, setting the threshold to 19 actually stripped out virtually all of the noise. Only a few tiny blobs of noise remain and I can filter them out very easily.
Here's a question though, and maybe I'm still being a bit dumb -- how do I use that to mask my original image and leave the rgb values in place? Do I have to apply it to each channel? Now that I'm trying it, most of the roi functions I knew of seem to require greyscale images. I'm still quite new to Matlab and completely new to image processing.
Sara
Sara 2012년 1월 5일
Ignore that, it's working fine now. Thanks again for your help, it is brilliant.

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Image Analyst
Image Analyst 2012년 1월 5일
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
Sara
Sara 2012년 1월 5일
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
Sara 2012년 1월 5일
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
Titus Edelhofer
Titus Edelhofer 2012년 1월 5일
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
Titus Edelhofer 2012년 1월 5일
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
  댓글 수: 4
Abdoo
Abdoo 2015년 4월 8일
편집: Abdoo 2015년 4월 8일
I dont have Idea to how can select and convert the value of the range color from RGB image, for example the below image show the select color and the range of this color want to convert it to black and all the remaining colors convert to white.
Image Analyst
Image Analyst 2015년 4월 8일
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
Gebra maryam Alehegn 2017년 5월 2일
How to read multiple image from folder and extract RGB color features????

카테고리

Help CenterFile Exchange에서 Modify Image Colors에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by