I am trying to plot an image histogram in a tiled layout with the pictures of the images in the same figure. currently I have
B = imread('building.jpg');
Grayimg = rgb2gray(B);
tiledlayout(2, 2);
nexttile;
imshow(B);
nexttile;
imshow(Grayimg);
nexttile;
imhist(Grayimg);
and I get the error Warning: Unable to set 'Position', 'Inner Position', 'OuterPosition', or ActivePositionProperty' for objects in a TiledChartLayout.
The figure that is output has the grayscale part of the histogram in tile 1, and the histogram in tile 3. I want the whole "imhist output" in one tile. How can I do this? It doesn't sem to like imhist for some reason.
Thank you,
Jake

 채택된 답변

Voss
Voss 2023년 7월 12일

1 개 추천

You're right, imhist doesn't seem to be very compatible with tiledlayout.
Here's a workaround that might work for you:
% B = imread('building.jpg');
B = imread('peppers.png');
Grayimg = rgb2gray(B);
f1 = figure();
tiledlayout(2, 2);
nexttile;
imshow(B);
nexttile;
imshow(Grayimg);
% make the nexttile, just to store its position:
ax1 = nexttile;
ax1_pos = ax1.Position;
delete(ax1);
% imhist() into a new figure:
f2 = figure();
imhist(Grayimg);
% copy the two imhist axes into the first figure:
ax1 = copyobj(gca(),f1);
ax_cs1 = copyobj(findall(f2,'Tag','colorstripe'),f1);
% and adjust the copies' positions to fit where the nexttile was:
ax_cs1.Position = [ax1_pos([1 2 3]) ax1_pos(4)*ax_cs1.Position(4)];
ax1.Position = [ax1_pos(1) ax1_pos(2)+ax_cs1.Position(4) ax1_pos(3) ax1_pos(4)-ax_cs1.Position(4)];
% finally, delete the separate imhist figure
delete(f2);

댓글 수: 2

Jacob
Jacob 2023년 7월 12일
편집: Jacob 2023년 7월 12일
awesome, that worked! Thanks for the workaround. Is 'Tag' and 'Colorstripe' just what that grayscale is called by MATLAB?
Also, just for my own curiosity, how did you know the position of the grayscale? - i.e. ax_cs1.Position(4)?
Voss
Voss 2023년 7월 12일
Yes, apparently, 'colorstripe' is the Tag given by MATLAB to the grayscale axes. I found that out by first doing findall(f2) to see what all was in an imhist plot.
ax_cs1.Position is the position of the colorstripe/grayscale axes; ax_cs.Position(4) is its height. Since its Units are 'normalized' I multiply by the height of the original nexttile axes (which is also 'normalized') height ax1_pos(4) to get the final height.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Color and Styling에 대해 자세히 알아보기

질문:

2023년 7월 12일

댓글:

2023년 7월 12일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by