Automatically align image and graph with common x axis

I have an image that I want to build under a graph showing the intensity of an arbitrary line of this image.

Apparently, I cannot "automatically" make two graphs as aligned (they have the same x axis) and are not distorted .

Here is the MWE that uses the kobi.png image, which should come with MATLAB. For this solution, I used the answer to this question , but this is not quite what I am looking for. The reason will be clear after the code.

 im = imread('kobi.png'); % read default image img = rgb2gray(im); % convert to grayscale y = 600; % select line to "scan" % plot image with highlithed line subplot(3,3,4:9); imagesc(img); colormap gray hold on line([0 size(img,2)], [yy], 'Color', 'r', 'LineWidth', 1.5); hold off axis image photoAxs = gca; photoAxsRatio = get(photoAxs,'PlotBoxAspectRatio'); % plot intensity of selected row subplot(3,3,1:3); r = img(y, :); plot(r); axis tight topAxs = gca; % adjust ratios topAxsRatio = photoAxsRatio; topAxsRatio(2) = photoAxsRatio(2)/2.4; % I want to get rid of this number! set(topAxs,'PlotBoxAspectRatio', topAxsRatio) 

Result

As you can see, this leads to the (almost) expected result, but there is a hard-coded number (which in the answer I linked was different, 3.8 , but 2.4 ) that I would like to Eliminate. Also, I think this number only gives a clearly aligned solution, but with my little OCD this room for error gives me creeps!

So the question is:

Is there a viable way to automatically align a graph and an image that have the same x axis but maintain the aspect ratio of the image ?

+1
source share
1 answer

Old code:

 topAxsRatio = photoAxsRatio; topAxsRatio(2) = photoAxsRatio(2)/2.4; % I want to get rid of this number! set(topAxs,'PlotBoxAspectRatio', topAxsRatio) 

New code:

 photoratio = photoAxs.PlotBoxAspectRatio(1)/photoAxs.PlotBoxAspectRatio(2); ratio = photoratio * photoAxs.Position(4)/topAxs.Position(4); topAxs.PlotBoxAspectRatio = [ratio, 1, 1]; 

Result:

enter image description here


A little explanation:

When the numbers are first displayed, you will notice that only the heights are different, although you can clearly see that the width is also different.

I am not 100% sure why Matlab does this, but this is my hunch.

Usually, two properties, width and height, are enough to determine the size of a two-dimensional figure, but Matlab introduces an additional PlotBoxAspectRatio property to control the size. To avoid conflict, Matlab decides to set the width property to a fixed number when the shape is first created. However, the actual width is calculated using height*PlotBoxAspectRatio .

Therefore, we have:

 TopAxis.width = TopAxis.height * TopAxis.ratio Photo.width = Photo.height * Photo.ratio 

To preserve the initial height of TopAxis, we can only change the aspect ratio.

Let be

 TopAxis.width = Photo.width 

We have

 TopAxis.height * TopAxis.ratio = Photo.height * Photo.ratio TopAixs.ratio = Photo.ratio * Photo.height / TopAxis.height 

And the Matlab code equivalent is the new code proposed.

+2
source

All Articles