# How can I overlap two images/pictures in Matlab?

조회 수: 210(최근 30일)
Zubair Nizamani 2016년 7월 14일
편집: DGM 2022년 5월 15일
I am trying to overlap two pictures one of them is in .jpg file and other is contour plot from MATlab. I want to overlap on one an other to compare the contour lines. Does any body know what code can be or any manual way?

댓글을 달려면 로그인하십시오.

### 답변(3개)

David Young 2016년 7월 14일
You can use hold to superimpose contour lines on an image. Here's an example:
% Get an image from a jpg file
% display it
imshow(img);
% superimpose a contour map (in this case of the image brightness)
hold on;
contour(rgb2gray(img), 5);
hold off;
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Image Analyst 2017년 4월 9일
What do you want to do with the rows that have missing measurements? Ignore them? Interpolate them?

댓글을 달려면 로그인하십시오.

DGM 2022년 5월 15일
편집: DGM 2022년 5월 15일
There's an awful lot of traffic on this question. I don't know that passers-by actually have the same intent, but let's assume some do.
The original question was about overlaying a contour map an an image. It almost reads as if both the contour map and the underlying image are (or are assumed to be) raster images. It's good advice to simply avoid such a scenario and plot the contour map on the image instead of recycling a rasterized copy that's likely been degraded due to any number of factors. There are a few points to consider in such an effort.
First of all, a contour plot uses the current axes colormap, but imshow()/image()/imagesc() also use the current colormap to display single-channel (grayscale) images. This causes problems if you want the two objects to use different colormaps. While you can work around it by creating duplicate axes, the simple solution is to just expand the image so that it doesn't require the use of the current colormap.
% some sort of image
%bgimage = imread('cameraman.tif'); % works for grayscale
bgimage = imread('jagblobs.png'); % or RGB images
% some sort of height information to make a contour map
heightdata = imgaussfilt(bgimage,20);
if size(bgimage,3) == 3
heightdata = rgb2gray(heightdata);
elseif size(bgimage,3) == 1
% need to expand single-channel images
% if you don't want them to share the same cmap as the contour
bgimage = repmat(bgimage,[1 1 3]);
end
% draw things
imshow(bgimage); hold on
contour(heightdata)
colormap(parula)
If you want to do the same with a contourf() plot, dealing with transparency is another issue to consider. See this answer for an example of a truncated semitransparent contourf() over an image. Take note of the chosen method and the link provided at the top for the opposite method.
That leaves the less-favorable prospect of trying to somehow overlay an image of a contour plot. I'm going to assume that the images have been registered/scaled/cropped somehow and ancillary plot features like labels, ticks, titles, and colorbars have been dealt with. I'm assuming you're not trying to do something like this:
I'm going to assume two simple images of identical geometry. Consider these two images:
Can you use imfuse() to combine them? You could. If you're using the image registration tools, it might offer a little bit of convenience in that it supports a workflow built around them.
% some sort of image
% some sort of height information
outpict = imfuse(fgimage,bgimage,'blend');
imshow(outpict)
So you could use imfuse(), but the results are pretty terrible. This should be expected, as imfuse() is a simple visualization tool for image comparison, not an image composition tool. The closest it can do is above, which is a simple opacity blend. Even if that's all you want, bear in mind that it can't even be adjusted. All imfuse() can do is a fixed 50% blend (i.e. it's just the arithmetic mean). If you want anything else, you'll won't be doing it with imfuse().
You can do simple adjustable opacity blending without imfuse(), though bear in mind that you'll have to pay attention to the numeric class, data range, and number of color channels when trying to do this.
% some sort of image
% some sort of height information
alph = 0.7; % at least it's adjustable
outpict = fgimage*alph + bgimage*(1-alph);
imshow(outpict)
Either way, the results of an opacity blend are generally going to look terrible. It might be about all you can do for a filled contour, but for an unfilled contour, it's obtrusive.
% some sort of image
% some sort of height information
% compose images
alph = ~all(fgimage>=254,3);
outpict = im2double(fgimage).*alph + im2double(bgimage).*(1-alph);
outpict = im2uint8(outpict);
% draw things
imshow(outpict)
I wouldn't exactly call that good. If visual appearances matter (especially with smooth images), using binary masking techniques is likely going to be an exercise in creating ragged, ugly results.
What can be done better? There are a number of image blending methods that might work in certain cases depending on the content and intent, but for a plain contour map like this example, the following would be where I might start (if I had no other choice):
% some sort of image
% some sort of height information
% compose images
fgimage = color2alpha(fgimage,[1 1 1]); % make white transparent
fgimage(:,:,4) = imadjust(fgimage(:,:,4)); % boost opacity
outpict = imblend(fgimage,bgimage,1,'normal','linear'); % compose
outpict = splitalpha(outpict); % strip extraneous alpha channel
% draw things
imshow(outpict)
It's not the greatest, but the rasterized contour image was pretty terrible to begin with. The tools used in this example (color2alpha(), imblend(), and splitalpha()) are part of MIMT (on the File Exchange).
For other blending/composition examples, see this answer and the links therein. Those answers cover techniques based on MATLAB/IPT tools, as well as techniques based on MIMT tools.
##### 댓글 수: 0표시숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

Image Analyst 2016년 7월 14일
There is a function for that. It's called imfuse().
##### 댓글 수: 1표시숨기기 없음
mohammed alany 2019년 3월 31일

댓글을 달려면 로그인하십시오.

### 범주

Find more on Basic Display in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by