How to change color in overlapping image ?

9 views (last 30 days)
I created an overlapping image from two images, a and b; I get green-magenta colors:
I want to control the color output: for example, I want blue and red.
c=imfuse(a,b,'falsecolr','Scaling','joint');
All suggestions are welcome

Accepted Answer

Image Analyst
Image Analyst on 25 Jul 2017
You can use cat(3, r, g, b) to combine images into any color channels you want.
  15 Comments
Image Analyst
Image Analyst on 26 Jul 2017
You're welcome - thanks for Accepting. Actually you don't need this line
matching = binaryImage1 & binaryImage2;
I ended up not using it.

Sign in to comment.

More Answers (2)

Saif
Saif on 23 Mar 2023
how we change background color into logo colour and logo into backgorund color white and green
  1 Comment
DGM
DGM on 23 Mar 2023
Please open a new question for this by clicking Ask at the top of the page. When you do, make sure to provide example images for context and explain specifically which parts of which image(s) should be a particular color.

Sign in to comment.


DGM
DGM on 23 Mar 2023
Imfuse() does support channel specification. That may suffice in some cases.
% read the files
frame1 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/83841/frame0017.png');
frame2 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/83840/frame0031.png');
% one line
outpict = imfuse(frame1,frame2,'colorchannels',[1 0 2]);
imshow(outpict)
Of course, these images are negative (black-on-white), so it's confusing. The object colors are actually swapped because the black regions are colored by the background of the opposite image. It would be a lot simpler to just invert the source images.
% invert sources for less confusion
frame1 = imcomplement(frame1);
frame2 = imcomplement(frame2);
outpict = imfuse(frame1,frame2,'colorchannels',[1 0 2]);
imshow(outpict)
Note that this does not require any information to be lost in binarization, and it still allows the use of other features of imfuse().
Now let's say you wanted the background to stay white. For this simple diagram, the object polarity seems almost arbitrary, but let's consider a different set of sources.
frame1 = imread('cameraman.tif');
frame2 = fliplr(frame1);
frame1 = imcomplement(frame1);
frame2 = imcomplement(frame2);
outpict = imfuse(frame1,frame2,'colorchannels',[1 0 2]);
imshow(outpict)
In this case, we have images where the primary object content is obviously supposed to be dark-on-light. It's also plausible that we wouldn't want to binarize the sources.
This is an extra complication that really can't be solved with imfuse() directly. One might think that the current output could simply be inverted, but that would require specifying that the image colors are yellow and cyan -- and we can't specify two secondary colors. Not only does imfuse() not allow it, we'd be losing a lot of color mixing if we did.
One way to deal with the problem is by doing a lightness inversion on the output. If you have the tools, this adds only one line.
frame1 = imread('cameraman.tif');
frame2 = fliplr(frame1);
frame1 = imcomplement(frame1);
frame2 = imcomplement(frame2);
outpict = imfuse(frame1,frame2,'colorchannels',[1 0 2]);
% invert L
outpict = imtweak(outpict,'hsl',[0 1 -1]);
imshow(outpict)
Now the primary object in frame1 appears red and in the appropriate polarity. The same can be applied to the original images:
frame1 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/83841/frame0017.png');
frame2 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/83840/frame0031.png');
frame1 = imcomplement(frame1);
frame2 = imcomplement(frame2);
outpict = imfuse(frame1,frame2,'colorchannels',[1 0 2]);
% invert L
outpict = imtweak(outpict,'hsl',[0 1 -1]);
imshow(outpict)
Note that this transformation works in HSL, but not in HSV. Base MATLAB/IPT do not have HSL conversion tools, so I'm using imtweak() from MIMT to do the adjustment.
Personally, I'd rather avoid the confusion and stay with light-on-dark polarity, as it makes it easier to visually understand the contributions from each image. It's my opinion that having the images in the original polarity is unimportant when treating imfuse() simply as a visual comparison tool.
  1 Comment
DGM
DGM on 23 Mar 2023
I mentioned that we'd lose some color mixing if we tried to do the blending using two secondary-color images. Here's an example of what would happen. First, let's consider the case with a continuous image.
frame1 = imread('cameraman.tif');
frame2 = fliplr(frame1);
% invert sources
frame1 = imcomplement(frame1);
frame2 = imcomplement(frame2);
% reduce each image to grayscale (same thing imfuse() does)
frame1 = im2gray(frame1);
frame2 = im2gray(frame2);
% assemble two secondary-color images
blank = zeros(size(frame1));
frame1 = cat(3,blank,frame1,frame1); % cyan
frame2 = cat(3,frame2,frame2,blank); % yellow
% combine them by addition, invert
outpict = imcomplement(frame1 + frame2);
imshow(outpict)
That doesn't look so bad. In fact, in some cases, this might be preferable. Let's see what happens when we try this with our high-contrast black-on-white images.
frame1 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/83841/frame0017.png');
frame2 = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/83840/frame0031.png');
% invert sources
frame1 = imcomplement(frame1);
frame2 = imcomplement(frame2);
% reduce each image to grayscale (same thing imfuse() does)
frame1 = im2gray(frame1);
frame2 = im2gray(frame2);
% assemble two secondary-color images
blank = zeros(size(frame1));
frame1 = cat(3,blank,frame1,frame1); % cyan
frame2 = cat(3,frame2,frame2,blank); % yellow
% combine them by addition, invert
outpict = imcomplement(frame1 + frame2);
imshow(outpict)
We lose color mixing in the overlap regions, since the combination of two secondary colors truncates to white.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!