Help me please with color image processing
Show older comments
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)
Image Analyst
on 3 Apr 2015
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
Image Analyst
on 3 Apr 2015
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".
Andrew Tim
on 3 Apr 2015
Image Analyst
on 3 Apr 2015
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.
Andrew Tim
on 3 Apr 2015
Image Analyst
on 4 Apr 2015
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.
Andrew Tim
on 4 Apr 2015
Image Analyst
on 4 Apr 2015
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
Andrew Tim
on 4 Apr 2015
Image Analyst
on 5 Apr 2015
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?
Andrew Tim
on 6 Apr 2015
Image Analyst
on 7 Apr 2015
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.
Andrew Tim
on 8 Apr 2015
Image Analyst
on 8 Apr 2015
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.
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!