Plotting a contour plot on top of an image?
84 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, I want to plot a contour plot on top of an image, but I cannot manage to do so. I want to overlay following images:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/164507/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/164509/image.png)
My code is as:
summation_front3=flipud(summation_front2);
figure(3);
imshow(avg_background_front);
hold on
fig = contourf(summation_front3);
set(gca,'YTick',[],'XTick',[])
colormap(jet);
hold off
The plot I receive looks like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/164511/image.png)
I want the 'blue area' in the contour plot, which is zeros, to be transparent. Is someone able to help me, thanks?
0 commentaires
Réponses (4)
Abel Babu
le 30 Mai 2017
Hi,
As of R2014b there is no direct way of doing the same. In case you are using any version prior do refer to this post: https://in.mathworks.com/matlabcentral/answers/60106-how-to-make-one-contour-transparent-in-contourf
For versions after R2014b, this post discusses a workaround:
This workaround involves manually defining the area that is required to be transparent. In your use case, to define the area, you can maybe check for location in the contour plot which is zero and then use the fill function with the corresponding x and y values.
Abel
0 commentaires
Olivier Haas
le 20 Jan 2023
Modifié(e) : Olivier Haas
le 21 Avr 2023
This shows different ways to overlay a contour on an image
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1269875/image.jpeg)
%% plotting contour from edges detected
% obtain a binary image
I = imread('rice.png');
BW = imbinarize(I);
% find the edges - easy for a binary image
Iedge = edge(BW,'Roberts');
% the edge may be different depending on the algorithm and may also be
% different to that found using bwboundaries (latter is perfect), BUT it is the edge contour
% found by the edge detector you may want to evaluate.
% plot the contours of the edges
figure
imshow(I)
hold on
[X,Y]=find(Iedge==1);
plot(Y,X,'c.') % note Y and X swapped
contour(Iedge,1,'k--'); %you get 2 contours on either side of the edge
contour(BW,1,'m-.'); % you get 1 contour outside the white blob
% Using mathematical morphology - from other post
[B, L] = bwboundaries(BW,'noholes');
for k = 1:length(B)
boundary = B{k};plot(boundary(:,2), boundary(:,1), 'r:', 'LineWidth', 2)
end
legend('edge > find > plot','edge > contour','BW > contour','bwboundaries')
title('Overlayed images')
0 commentaires
DGM
le 21 Avr 2023
Assuming that the overlay is an actual contour() object:
Here are two examples, one employing Will's technique of manipulating undocumented properties, and another using a reverse stacking technique. The first should work here.
Assuming that the overlay is an image, then this example might be of use, but that's mainly aimed at handling unfilled contours.
If I were to assume that the task is to combine the two given images, and that their extents should correspond to each other, then:
% read the images
BG = imread('thing.png');
FG = imread('contour.png');
% figure capture should be avoided whenever possible
% it will tend to add unpredictable extraneous padding
FG = FG(1:end-1,:,:);
% and it will not easily preserve aspect ratio or size
FG = imresize(FG,[size(BG,1) size(BG,2)],'nearest');
% alpha as a scalar
% imfuse(...,'blend') can also do this, but only for 50%
alpha = 0.5;
outpict = im2uint8(alpha.*im2double(FG) + (1-alpha).*im2double(BG));
imshow(outpict)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1363003/image.png)
% alpha as a map
alpha = abs(double(FG) - permute([1 1 143],[1 3 2])) > 3;
outpict = im2uint8(alpha.*im2double(FG) + (1-alpha).*im2double(BG));
imshow(outpict)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1363008/image.png)
I imagine that this latter composition is what's intended, though the concepts may be combined.
0 commentaires
Voir également
Catégories
En savoir plus sur Contour Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!