Use imhist in tiled layout

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

 Risposta accettata

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 Commenti

Jacob
Jacob il 12 Lug 2023
Modificato: Jacob il 12 Lug 2023
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 il 12 Lug 2023
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Images in Centro assistenza e File Exchange

Richiesto:

il 12 Lug 2023

Commentato:

il 12 Lug 2023

Community Treasure Hunt

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

Start Hunting!

Translated by