Image Background Overlay Color Changes Why Does It happen?

47 views (last 30 days)
Hi i just Worked on an Assignment and Im having some deficulties with it.....Canany one check whats wrong...
Note: Assignment is to modify the background image to display a Logo on it.
something like this,
I'm still new with Matlab please can anyone help me with this...
The code I have been working is the following.
% Some of the Functons were written in the assignment to use specifically like im2double and more.
% the starting is simple reading the image, resizing the logo image, and converting it to im2double, im2bw (Part of the assignment).
%I faced issues on the third where i have to use condition and for loop to chane the pixals valse one-by-one to display the logo
% on the background.
% Please help me with this assignment...
Bgimg = imread("Background_Image.jpg");
Bgimgdouble = im2double(Bgimg);
imshow(Bgimgdouble);
Fgimg = imread("Forground_Image.png");
FgimgRs = imresize(Fgimg, [576 720]);
imshow(Fgimg);
% converting White and Black to Black and White.
FgimgRsComp = imcomplement(FgimgRs);
imshow(FgimgRsComp);
Fgimg2bw = im2bw(FgimgRsComp, 0.4);
imshow(Fgimg2bw);
%Here im having some trouble
img = zeros(size(Fgimg2bw));
for ii=1:size(Fgimg2bw,1)
for jj=1:size(Fgimg2bw,2)
% get pixel value
pixel=Fgimg2bw(ii,jj);
% check pixel value and assign new value
if pixel<0.5
new_pixel=0;
elseif pixel>3
new_pixel=0;
else
new_pixel = pixel;
end
% save new pixel value in img image
img(ii,jj)=new_pixel;
end
end
imshow(img)
% When Place the image on to each other the logo image (VU) turns Blue instead of white.
% Also i wanted the logo to be transparent.
image(Bgimgdouble)
axis image
hold on
im = image(img);
im.AlphaData = max(img,[],3);
hold off
This is the Result im getting.....

Answers (2)

Selva Karna
Selva Karna on 4 Mar 2021
use image imfuse command

DGM
DGM on 17 Mar 2022
Edited: DGM on 21 Apr 2022
Dead post, I know. I just love talking to ghosts.
Since no source images were ever provided, this was never a directly answerable question. Still, there are some things that can be reasonably known.
This is a basic image blending problem. It may have originally included composting aspects, but since no alpha content is being read from the source images, then we can probably assume that the original method is moot. It's image blending.
Imfuse() is a basic image comparison tool. It has no practical utility in image composition or blending. The closest it can do is a uniform 50% opacity composition or an absolute difference blend.
survey of the evidence
Without knowing what the source images were, it's hard to be certain how exactly they were blended. Let's start with one assumption. Assume that the foreground image is solid white text on a black matting. At that point, the blending method should be fairly apparent, but let's try to make some more observations.
On the inner left-hand vertical edge of the U, I'm going to crop out a 2px-wide stripe -- one column sampling the blended area, the other column sampling the unblended area. If we can assume again that the original BG variation would be small over this 2px horizontal distance, we can derive the relationship between the blended and unblended regions.
A = imread('vusample.jpg');
A = im2double(A);
colors = {'r','g','b'};
BGf = linspace(0,1,100);
for c = 1:3
tc = A(:,:,c);
[BG idx] = unique(tc(:,2),'sorted');
R = tc(idx,1);
% interpolate onto a uniform base
Rf = interp1(BG,R,BGf,'linear');
plot(BGf,Rf,colors{c}); hold on
end
xlim([0 1])
ylim([0 1])
axis square
xlabel('background')
ylabel('result')
It already looks (more or less) like what should be expected. These curves represent a meridian of the blend function (result vs background for one value of the foreground). They are all roughly linear, leveling off as the output is clipped. Note that the curves do not begin at the origin. While it might generally be difficult to say what a given 2D function looks like when one can only vary one of the two independent variables, the given image and context implies that it is something simple -- namely a Linear Dodge blend (addition).
If this is the case, the y-intercept of this meridian should tell us something about the blend and/or the foreground image. Either the foreground was white and it was blended using a foreground blend weight of 0.6, or the foreground was simply not white. There is no way to know. If it were blended in an image manipulation program such as Photoshop, then the foreground was simply not white. No conventional image manipulation suites implement such a parameterization of either Addition or Linear Dodge blends.
It's not really visible in the above contour plot, but the case where FG = 0 is the R = BG diagonal. In other words, this blend function has a neutral response when FG = 0. If it weren't obvious from the fact that this is simple addition, the location of the neutral response should make it clear that the black FG matting will not affect the output.
realizing the blending
So let's try something similar. These are the FG and BG images. Since I don't have the original images, the given composition will become the new BG:
First let's do this the rudimentary way. Note the change of class. While it's not strictly necessary for this blend, it will be for other blends. I'm writing it this way as a generalization of the process for any blend.
FG = imread('VUtext.jpg'); % a uint8 image
BG = imread('VU.jpg'); % a uint8 image
FG = im2double(FG); % cast to double
BG = im2double(BG);
R = 0.6*FG + BG; % the blend: simple addition with 60% FG weight
R = im2uint8(R); % for sake of consistency, cast back to original class
figure
imshow(R)
In practice, the convenience of an image manipulation suite boils down to the fact that you don't have to know or remember the math behind every single blending operation. While this particular example happens to be one of the simplest possible blends, it's best to avoid the hassle and use something designed for the purpose.
The only tools in IPT or base MATLAB that look similar are imadd(), imsubtract(), imabsdiff(), immultiply() and imdivide(). These are not blending tools. This handful of tools are not consistently class-agnostic in their behavior. They cannot safely be used for blending of mixed-class (or even same-class) images without being wrapped in an extra layer of class-dependent babysitting.
  • immultiply(uint8(128),uint8(128)) does not equal uint8(64)
  • imadd(uint8(128),double(0.5)) does not equal uint8(255)
  • imadd(double(0.5),uint8(128)) will result in an error
For purpose-built blending tools, you need to use third-party tools or write your own. There are at least a couple image blending tools on the File Exchange, the most comprehensive option being the blending tools that come with MIMT.
FG = imread('VUtext.jpg'); % a uint8 image
BG = imread('VU.jpg'); % a uint8 image
R = imblend(FG,BG,1,'lineardodge',0.6); % a uint8 image
imshow(R) % the result is identical to the above example
Which is at least concise. The class handling is all done internally. The FG weight is a simple scalar parameter in the case of this blend mode.
If you use MIMT imcompose(), you can adjust everything interactively, though for something this simple, it's not really warranted.
Again, this is one of the simplest possible blending operations. Any other case would have been more difficult to identify with such certainty, and any other case would be far more inconvenient in comparison to using a purpose-built tool. Take the simple case of a "soft light" blend (the one from SVG specs):
Compared to the prior example, this is no more complex to do with imblend():
FG = imread('VUtext.jpg');
BG = imread('VU.jpg');
R = imblend(FG,BG,1,'softlightsvg');
imshow(R)
... but trying to roll your own each time?
FG = imread('VUtext.jpg');
BG = imread('VU.jpg');
FG = im2double(FG);
BG = im2double(BG);
m1 = FG <= 0.50;
m2 = BG <= 0.25;
m3 = ~m1 & m2;
m4 = ~m1 & ~m2;
R = (BG - (1-2*FG).*BG.*(1-BG)).*m1 ...
+ (BG + (2*FG-1).*(4*BG.*(4*BG + 1).*(BG-1) + 7*BG)).*m3 ...
+ (BG + (2*FG-1).*(sqrt(BG) - BG)).*m4;
R = im2uint8(R);
imshow(R)
... not nearly as convenient.
  1 Comment
DGM
DGM on 17 Mar 2022
I doubt it's necessary at this point, but as to what's wrong with the OP's code:
Bgimg = imread('VU.jpg');
Bgimgdouble = im2double(Bgimg);
Fgimg = imread('VUtext.jpg');
%FgimgRs = imresize(Fgimg, [576 720]);
FgimgRs = Fgimg; % i don't need to resize my FG image
% converting White and Black to Black and White.
% mind you, this destroys all the antialiasing
%FgimgRsComp = imcomplement(FgimgRs);
FgimgRsComp = FgimgRs; % i don't need to invert my FG
imshow(FgimgRsComp);
Fgimg2bw = im2bw(FgimgRsComp, 0.4);
figure
imshow(Fgimg2bw);
% Since Fgimg2bw is logical, this entire loop does nothing
% values are either 0 or 1, so the output is identical to the input
img = zeros(size(Fgimg2bw));
for ii=1:size(Fgimg2bw,1)
for jj=1:size(Fgimg2bw,2)
% get pixel value
pixel=Fgimg2bw(ii,jj);
% check pixel value and assign new value
if pixel<0.5
new_pixel=0;
elseif pixel>3
new_pixel=0;
else
new_pixel = pixel;
end
% save new pixel value in img image
img(ii,jj)=new_pixel;
end
end
figure
imshow(img)
% image() will render an RGB image in truecolor
image(Bgimgdouble)
axis image
hold on
% but image() will render a single-channel image
% in false-color using the current colormap
% the blue is the first tuple in the parula() colormap
im = image(img);
% img only has one channel. max() does nothing here
im.AlphaData = max(img,[],3);
hold off
If the colormap were different, the text color would be different.
Even if the FG were rendered as white, it would still be solid. Adjusting the opacity/alpha of a white FG will not produce the results depicted by the original example.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!