Overlaying a contourf plot on a grayscale image
2 次查看(过去 30 天)
显示 更早的评论
behzad
2018-10-18
Hi guys, I need to overlay a displacement contour of a deformed plate image (using contourf command) on its original grayscale undeformed image. How can I do that in matlab? Thank you

采纳的回答
jonas
2018-10-18
编辑:jonas
2018-10-18
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 个评论
behzad
2018-10-18
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?
behzad
2018-10-18
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
2018-10-18
编辑:jonas
2018-10-18
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
2018-10-18
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
2018-10-18
编辑:jonas
2018-10-18
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
2018-10-18
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
2018-10-18
编辑:jonas
2018-10-18
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
2018-10-19
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
2018-10-19
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
2018-10-19
编辑:jonas
2018-10-19
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
2018-10-19
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
2018-10-19
编辑:jonas
2018-10-19
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
2018-10-20
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Contour Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
