Combining 2 images of different size

I am trying to combine 2 image of different size. The smaller image is to be fully overlaying a small part of the bigger image without transparency. Like a sticker over a image. How do I go about doing it? I have the specific coordinates of where the smaller image should be overlaid on.

 Accepted Answer

CompositeImage = BiggerImage;
CompositeImage(FirstRow : FirstRow + size(SmallerImage,1) - 1, FirstColumn : FirstColumn + size(SmallerImage,2) - 1) = SmallerImage;

9 Comments

I tried your code and got the error "Assignment has more non-singleton rhs dimensions than non-singleton subscripts". Any idea why?
CompositeImage(FirstRow : FirstRow + size(SmallerImage,1) - 1, FirstColumn : FirstColumn + size(SmallerImage,2) - 1, :) = SmallerImage;
That solved the problem and I can see it's at the correct position now. However, it's showing just black instead of what the smaller image is supposed to be.
Please show
size(BiggerImage)
size(SmallerImage)
class(BiggerImage)
class(SmallerImage)
Here you go
size(BiggerImage)
ans =
701 574 3
size(SmallerImage)
ans =
40 66 3
class(BiggerImage)
ans =
uint8
class(SmallerImage)
ans =
uint8
Seems to work fine in my test.
BiggerImage = uint8(rand(701,574,3) * 255);
SmallerImage = uint8(207 * ones(40,66,3));
FirstRow = 85; FirstColumn=417;
CompositeImage = BiggerImage;
CompositeImage(FirstRow : FirstRow + size(SmallerImage,1) - 1, FirstColumn : FirstColumn + size(SmallerImage,2) - 1,:) = SmallerImage;
image(CompositeImage)
For your data, what shows up for min(SmallerImage(:)) and max(SmallerImage(:)) ?
I got 0 and 1.
min(SmallerImage(:))
ans =
0
max(SmallerImage(:))
ans =
1
The level 1 in a uint8 image is practically black. You would have a hard time telling the 0 (true black) and the 1 (nearly black) apart in uint8 .
If your original data is in the range 0 to 1 but is double precision instead of uint8, then you should be using
SmallerImage = im2uint8(SmallerImageThatIsDouble);
before starting the above. Or if you do not have the Image Processing Toolbox then
SmallerImage = uint8(255 * SmallerImageThatIsDouble);
Yes that worked! Thank you so much kind sir.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!