# Plotting a contour plot on top of an image?

87 vues (au cours des 30 derniers jours)
Rasmus Dueholm le 26 Mai 2017
Réponse apportée : DGM le 21 Avr 2023
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:
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:
I want the 'blue area' in the contour plot, which is zeros, to be transparent. Is someone able to help me, thanks?
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### 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 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

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
%% plotting contour from edges detected
% obtain a binary image
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 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Image Analyst le 21 Avr 2023
help labeloverlay
LABELOVERLAY Overlay label matrix regions on a 2-D image. B = LABELOVERLAY(A,L) fills the input image with a different solid color for each label in the label matrix L. L must be a valid MxN label matrix that agrees with the size of A. B = LABELOVERLAY(A,BW) fills the input image with a solid color where BW is true. BW must be a valid mask that agrees with the size of A. B = LABELOVERLAY(A,C) fills in the input image with a different solid color for each label specified by the categorical matrix C. B = LABELOVERLAY(___,NAME,VALUE) computes the fused overlay image B using NAME/VALUE parameters to control aspects of the computation. Parameters include: 'Colormap' Mx3 colormap where M is the number of labels in the label matrix L or binary mask BW. RGB triplets in each row of the colormap must be normalized to the range [0,1]. A string or character vector corresponding to one of the valid inputs to the MATLAB colormap function is also accepted, in which case a permuted form of the specified colormap suitable for labeled region visualization will be used. Default: 'jet' 'IncludedLabels' Scalar or vector of integer values in the range [0,max(L(:))] that specify the labels that will be falsecolored and blended with the input image. When a categorical, C, is provided as the specification of the labeled regions, 'IncludedLabels' can also be a vector of strings corresponding to labels in C. Default: 1:length(L(:)) 'Transparency' Scalar numeric value in the range [0,1] that controls the blending of the label matrix with the original input image A. A value of 1.0 makes the label matrix coloring completely transparent. A value of 0.0 makes the label matrix coloring completely opaque. Default: 0.5 Class Support ------------- The input image A is of type uint8, uint16, single, double, or int16. The input label matrix L is a numeric matrix. B is an RGB image of type uint8. Example 1 - Visualize over-segmentation of RGB data --------- A = imread('kobi.png'); [L,N] = superpixels(A,20); figure imshow(labeloverlay(A,L)); Example 2 - Visualize binary-segmentation of greyscale image --------- A = imread('coins.png'); t = graythresh(A); BW = imbinarize(A,t); figure imshow(labeloverlay(A,BW)) Example 3 - Visualize segmentation specified as categorical array --------- A = imread('coins.png'); t = graythresh(A); BW = imbinarize(A,t); stringArray = repmat("table",size(BW)); stringArray(BW) = "coin"; categoricalSegmentation = categorical(stringArray); figure imshow(labeloverlay(A,categoricalSegmentation,'IncludedLabels',"coin")); See also superpixels, imoverlay Documentation for labeloverlay doc labeloverlay
help visboundaries
VISBOUNDARIES Plot region boundaries. VISBOUNDARIES(BW) draws boundaries of regions in the binary image BW on the current axes. BW is a 2D binary image where pixels that are logical true belong to the foreground region and pixels that are logical false constitute the background. VISBOUNDARIES uses BWBOUNDARIES to find the boundary pixel locations in the image. VISBOUNDARIES(B) draws region boundaries specified by B, where B is a cell array containing the boundary pixel locations of the regions, similar in structure to the first output from BWBOUNDARIES (see function help for BWBOUNDARIES). Each cell contains a Q-by-2 matrix, where Q is the number of boundary pixels for the corresponding region. Each row of these Q-by-2 matrices contains the row and column coordinates of a boundary pixel. VISBOUNDARIES(AX, ___) draws region boundaries on the axes specified by AX. H = VISBOUNDARIES(___) returns a handle to an hggroup object for the boundaries. The hggroup object, H, is the child of the axes object, AX. H = VISBOUNDARIES(___,NAME,VALUE,...) passes the name-value pair arguments to specify additional properties of the boundaries. Parameter names can be abbreviated. 'Color' Specifies the color of the boundary using a 1x3 RGB triplet which must be normalized to the range [0,1]. A string or character vector corresponding to one of the valid inputs to the MATLAB colormap function is also accepted. For example, valid specifications for a red fill color are: 'red','r', and [1 0 0]. Default color is red. 'LineStyle' Specifies the line style for the boundary. The line style can be any of the following. {-} | -- | : | -. | none 'LineWidth' Size in points Width of the boundary. Specify this value in points. 1 point = 1/72 inch. The default value is 2 points. 'EnhanceVisibility' Specifies whether or not to augment the drawn boundary with contrasting features to improve visibility on a varying background. Setting the value to true draws an augmented boundary and setting it to false does not. Default value is true. Example 1 --------- % This example computes the boundaries of a binary images and plots it on % the image. BW = imread('blobs.png'); B = bwboundaries(BW); imshow(BW) hold on visboundaries(B) Example 2 --------- % This example shows how to visualize the result of segmentation of an % image using VISBOUNDARIES. I = imread('toyobjects.png'); imshow(I) hold on % Segment the image using active contour % First, specify initial contour location close to the object that is % to be segmented. mask = false(size(I)); mask(50:150,40:170) = true; % Display the initial contour on the original image in blue. visboundaries(mask,'Color','b'); % Segment the image using the 'edge' method and 200 iterations bw = activecontour(I, mask, 200, 'edge'); % Display the final contour on the original image in red. visboundaries(bw,'Color','r'); title('Red - Initial Contour, Blue - Final Contour'); See also BWBOUNDARIES, BWPERIM, BWTRACEBOUNDARY, VISCIRCLES. Documentation for visboundaries doc visboundaries
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

DGM le 21 Avr 2023
Assuming that the overlay is an actual contour() object:
From @Abel Babu's links, we find this answer from Will Grant, which should work in modern versions.
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.
As of R2022b, contour()/contourf() objects now support FaceAlpha and EdgeAlpha properties.
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
% 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)
% 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)
I imagine that this latter composition is what's intended, though the concepts may be combined.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Object Analysis 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!

Translated by