Overlaying a contourf plot on a grayscale image
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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

採用された回答
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?
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
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.
<<

>>

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.
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');

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.
behzad
2018 年 10 月 19 日
How can I use export_fig in my code? Can you give an example?
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?

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.
My pleasure!
You could also try adding this argument to imshow
imshow(..., 'border', 'tight')
It should remove any extra space around the image.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Contour Plots についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
