Help me please with color image processing

Hello, I have two images. Img1 and Img2. All I want is to write function in MATLAB that brings each pixel from one image and compares it to each pixels of another, and then the pixels are equal then it write white color pixel on the second image. Could you please help me to write this function? Thank you.

Answers (1)

Try this:
r1 = Img1(:,:,1);
g1 = Img1(:,:,2);
b1 = Img1(:,:,3);
r2 = Img2(:,:,1);
g2 = Img2(:,:,2);
b2 = Img2(:,:,3);
% Find pixels where each color matches.
matchingPixels = (r1 == r2) & (g1 == g2) & (b1 == b2);
imshow(matchingPixels);
% Make those pixels white in the second image's color channels:
r2(matchingPixels) = 255;
g2(matchingPixels) = 255;
b2(matchingPixels) = 255;
% Concatenate to make a full color image again
Img2 = cat(3, r2, g2, b2);
imshow(Img2);

13 Comments

If you want an approximate match rather than an exact match, then you have to convert to LAB color space, and then compute the color difference, called "Delta E".
Thank you very much for the quick answer. But unfortunately it doesnt work:(...following error:
Error using == Matrix dimensions must agree.
How can I fix it? P.S. Two images of mine are of different sizes...
If the small image is 100x100, and the large image is 900x900, what do you want to compare pixel (600, 700) of the larger image to? There is no such pixel location in the smaller image.
yes, but my task is to compare each pixel from small image to the each pixel from large...
So if you have a 1 megapixel image and you have a second megapixel image, you want to compare each of the one million pixels in the first image to each of the two million pixels in the second image? If you had a row for each pixel of the first image, then you'd have a million rows, and if each pixel in the second image was a column in the output image, then you'd have 2 million columns. So you'd have a 2 gigapixel image. That is enormous. Why do you need such an image? And since most pixels will not be an exact match, virtually the entire 2 gigapixel output image would be white. Again, why do you need this? I think you don't need this. What is the "use case"? What do you really want to do? What is the big picture? I know you think you need to do this, but I'm not convinced, especially not without hearing the overall, higher-context reason.
What I want is to find specific colours in second image and make them white. All these colours I want to find are in the first image, the first image is the gradient sample containing whole bunch of colours I want to find. Not all of them will be in the second image but if some of them will then I want them to be white in the second image. Does it answer you questions?:)
Post your two images and state how "close" the colors need to be to each other to be considered a "match". Also, you might want to look into rgb2ind() to quantize your original image into fewer colors. http://en.wikipedia.org/wiki/Color_quantization
Thank you, but it seems for me that there is some misunderstanding...I thought I could do it just through the loop...what if my first image containing only 10 different colours ( for example(1<r<10, same g=100, same b=100), what will the fumction be in that case? ...just to find in other second image 10 colors and make them white?
Yes, misunderstandings are often what happens when someone posts a question for advice on image processing but forgets to post an image. A color photo/snapshot might have 1 to 20 megapixels with anywhere up to 16.7 million colors. Maybe your image is pure computer graphics with only 10 unique colors - we don't know because you're not sharing it. Or you might want yellow or red and not realize that there are thousands of possible RGB triplet values that can mean yellow or red. Have you seen my File Exchange where I find colors?
Please read this
Thank you sir, now I see that your Delta E function is actually what I needed:), Actually what I need is to give to the function coordinates of rectangle area (box) in the image, and after this recieve an image that you called "matching colors (Delta E<=30)" in your example. Could you help me to rewrite your Delta E function just to realize this simple task (coordinates of the box could be written just in code, for example let it be bottom right rectangle (end-39:end,end-49:end))? I will really appreciate this sir. And thank you again.
Comment out this line
mask = DrawFreehandRegion(h1, rgbImage); % Draw a freehand, irregularly-shaped region.
and use this:
mask = false(rows, columns);
mask((end-39:end,end-49:end) = true;
This will make the mask a rectangle instead of a freehand drawn region.
Thank you very much. But to write my function I described earlier (function that recieves coordinates of rectangle area (box) in the image, and after this returns an image called in your terms "matching colors (Delta E<=30)") I need to analize whole code of this big Delta E app. And it's too difficult for me now :( (I started to learn Matlab only 2 weeks ago:)). Could you please help me? I dont need any GUIs and any other extra functional. Or if its difficult to rewrite it for me then could you point me when the main body of Delta E function begins in your file that can help me to write my function (skipping all other tasks like GUI etc)? And thank you very much for your time again.
To get rid of GUI things, just comment out any line of code that calls title(), imshow(), plot(), bar(), xlabel(), ylabel(), xlim(), ylim(), and functions like that. Once those are gone, just step through the code and if any line of code displays anything, just delete that line.

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 3 Apr 2015

Commented:

on 8 Apr 2015

Community Treasure Hunt

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

Start Hunting!