이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Overlaying a contourf plot on a grayscale image
조회 수: 1 (최근 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?
jonas
2018년 10월 18일
편집: jonas
2018년 10월 18일
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.
<<
>>
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 (한국어)