You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Overlaying a contourf plot on a grayscale image
3 views (last 30 days)
Show older comments
Accepted Answer
jonas
on 18 Oct 2018
Edited: jonas
on 18 Oct 2018
You can use two linked axes with different colormaps. Example:
% Axes
ax(1)=axes('color','none','xcolor','none','ycolor','none');hold on
ax(2)=axes('color','none','xcolor','none','ycolor','none','ydir','reverse');hold on
linkaxes(ax,'xy')
axis([-3 3 -3 3])
% Contour data
[X,Y,Z] = peaks;
% Image
axes(ax(2))
I = imread('cameraman.tif');
imagesc(I,'XData',[min(X(:)) max(X(:))],'YData',[min(Y(:)) max(Y(:))]);
colormap(ax(2),'gray')
% Contour
axes(ax(1))
h = contour(X,Y,peaks);hold on
colormap(ax(1),'parula')
Just make sure the axes have the same position. If you create a colorbar "outside" of the axes area, then one axis will shrink. Usually I just create the colorbar "inside" and then move it outside by adapting its position properties.
16 Comments
behzad
on 18 Oct 2018
Thank you dear Jonas, your solution helped me a lot. But there is still a problem. I should use contourf instead of contour in the command line "h=contour(X,Y,peaks); hold on" and when using that, the whole filled contour covers the image and the grayscale image is hidden behind it. I am trying to remove or make transparent the yellow section of the contourf (shown in the image containing contourf plot) which equals zero displacement values. Have you any solution for that?
jonas
on 18 Oct 2018
Edited: jonas
on 18 Oct 2018
My pleasure! Sure, could you perhaps upload the images? Would be easier if I knew exactly what kind of data you are working with. For the contourf I assume you have x-y-coordinates. Do you also have coordinates for the grayscale image so you can align the two?
behzad
on 18 Oct 2018
I calculate x-displacement of a hole containing plate under tension using digital image correlation. The size of the image is 1040*400 pixels. I calculated deformation in the x direction and formed a 1040*400 displacement matrix named U. The coordinates for the contour is in pixels and it is the same size as gray scale image. The values for the contour displays the values of the 1040*400 U matrix for each pixel in the deformed image. I hope the information helps otherwise tell me.
<<
>>
jonas
on 18 Oct 2018
Edited: jonas
on 18 Oct 2018
Alright! Could you upload the data used in the contourf plot? I'd like to test how to best remove the yellow "background". You could try setting all values larger than a threshold to NAN. That would probably be the easiest approach. If A is your contour matrix
A(A>0.01) = NaN
Then you can superimpose the images, but remember that you have to reverse the yaxis of one of your axes, since the image yaxis is reverse by default.
behzad
on 18 Oct 2018
I saved the matrix data in the text format. Is it OK? It includes the data for the contourf plot. The yellow section includes pixels with zero displacement which do not let the grayscale image to appear and should be removed.
jonas
on 18 Oct 2018
Edited: jonas
on 18 Oct 2018
You could try something like this:
Z = dlmread('data2.txt');
Z(Z==0)=NaN;
I = rand(size(Z));
% Axes
ax(1)=axes('color','none','xcolor','none','ycolor','none');hold on
ax(2)=axes('color','none','xcolor','none','ycolor','none','ydir','reverse');hold on
linkaxes(ax,'xy')
axis([0 size(Z,2) 0 size(Z,1)]);
% Image
axes(ax(2))
imshow(I,[],'XData',[1 800], 'YData', [1 1040]);
% Contour
axes(ax(1))
h = contourf(Z);hold on
axis equal; % <-- important!
colormap(ax(1),'parula')
colormap(ax(2),'gray')
This works with imshow. The axes of imshow are always equal, so you have to set the axis to equal in the contourf plot as well to scale them correctly. Took me a while to figure that out.. :)
behzad
on 18 Oct 2018
Dear jonas, sorry for my late response. I managed to run the code with your great helps. The only problem occurs when I want to show colorbar beside the contourf moving the contourf plot to the side of the image and out of the center as the below picture. This is my code and the resulted image. What do you think the solution is?
%X-Displacement
u(u==0) = NaN;
% Axes
ax(1) = axes('color','none','xcolor','none','ycolor','none');hold on ax(2)=axes('color','none','xcolor','none','ycolor','none','ydir','reverse');
hold on
linkaxes(ax,'xy');
axis([0 size(Im1,2) 0 size(Im1,1)]);
% Image axes(ax(2));
Im1 = imread('ohtcfrp_00.tif');
imshow(Im1,[],'XData',[1 400], 'YData', [1 1040]);
% Contour Plot
axes(ax(1)); [Cu, h1] = contourf(u);hold on
axis equal
set(gca,'color','none');
set(h1,'LevelStep',0.05);
colorbar;
caxis([min(Lag_Dis(:,2)), max(Lag_Dis(:,2))]);
colormap(ax(1),'parula');
colormap(ax(2), 'gray');
jonas
on 18 Oct 2018
Edited: jonas
on 18 Oct 2018
Looks nice! I adressed the colorbar problem in the original answer. Just set the location to inside the axes, for example
cb=colorbar(...,'location','east')
Then move the bar by changing position
cb.Position=...
This will avoid the axes shrinking when the colorbar is created. On mobile right now so cannot try it out, but it should work.
You may also have to change the location of the ticklabels, as they appear on the left side per default if you set the location to 'west' or 'east'. For some reason, this is an undocumented feature, but the command is
cb.YAxisLocation = 'right'
All in all, you can add something like this
cb = colorbar(ax(1),'location','east')
cb.Position = cb.Position+[0.1 0 0 0];
cb.YAxisLocation = 'right'
behzad
on 19 Oct 2018
Dear Jonas, sorry for my delay in answering. The colorbar problem was solved fortunately, however, I have 2 minor problems: The first problem is that the contour plot does not fit the grayscale in position as the below image. Additionally, I am trying to change the size of the final image to my desired size as the second image. Is there any solution for the 2 problems. Sorry for too many questions.
jonas
on 19 Oct 2018
Not sure what you mean about the first issue. Is the colorbar too far to the right? Just adjust the first value of the 'Position' property. Personally, I usually write like this
cb.Position = cb.Position+[0.1 0 0 0];
This takes the old position cb.Position and moves it horizontally by 0.1 normalized unit (unless other unit is specified). Just change this number and place it where you want.
Second problem. Personally I would use export_fig to print the image. This function crops the image by default. Strongly recommended, and removes the need to reduce the figure window size.
jonas
on 19 Oct 2018
Edited: jonas
on 19 Oct 2018
The tricky thing with export_fig is to install it, as you also need to install ghostscript. After you get it to run, you just write something like:
export_fig(gcf,'-jpg','MyFigureName')
There are tons of optional input, such as "nocrop".
As I said, it can be a bit tricky to install. But I cannot recommend it enough, been using it exclusively for years without issues.
behzad
on 19 Oct 2018
I solved the second issue by your help. By the first issue I mean that the circular hole of the contour plot should coincide the hole in the grayscale plate as the below image. However it does not happen through my code. What do you think the problem is?
jonas
on 19 Oct 2018
Edited: jonas
on 19 Oct 2018
I can take a look if you upload the actual image, without colorbar etc. I need to be able to load it exactly like you are loading it and it needs to have the correct resolution. The issue could be related to how the contour matrix has been determined, in which case I won't be able to fix it.
behzad
on 20 Oct 2018
Due to a black margin in the grayscale image, the two axes did not coincide. Fortunately I managed to solve the problem using the below command.
ax(1).Position = [...];
Thank you very much for your helps.
More Answers (0)
See Also
Categories
Find more on Image Segmentation and Analysis 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)