How do i zoom imhist?

조회 수: 16 (최근 30일)
candra
candra 2022년 10월 24일
답변: DGM 2024년 12월 9일 18:22
i have problem with "imhist", is there any code to zoom in or out "imhist" automatically, because otherwise i have to zoom out manually

답변 (2개)

Abhinav Aravindan
Abhinav Aravindan 2024년 12월 9일 10:52
It seems that "imhist" adjusts the zoom such that the maximum Y-value is set to 2.5 times the variance of the Y-values in order to enhance the visibility of the plot.
As an alternative, you may consider using “imhist” with “stem” to achieve similar results and set the axis limits as desired.
I = imread('img');
[counts,binLocations] = imhist(I);
fig = stem(binLocations,counts, "Marker", "none");
You may refer to the below documentation for more information:

DGM
DGM 2024년 12월 9일 18:22
I see two potential needs here. Scaling YLim and possibly scaling XLim
Adjusting YLim:
The most common is probably a need to undo imhist()'s tight YLim scaling so that the peaks of the distribution are actually visible. Consider the example:
% you have an image
inpict = imread('cameraman.tif');
% when plotting, imhist() will truncate the ylimits
% imhist() does not have a means to specify this behavior
imhist(inpict)
% let's just re-plot that so we can compare against our modification
figure
imhist(inpict)
% imhist() creates two linked axes for the stem plot and the colorstripe
% but it does not return either axes handle, and only one is unambiguously labeled.
hax = get(gcf,'children')
hax =
2x1 Axes array: Axes (colorstripe) Axes
% however, it does reset the current axes to the stem plot axes
% so in this simple case, we can just use gca (or hax(2)) to set ylim
stemax = gca;
% ... but what ylim do we need?
% we would need to know the range of the bin counts,
% and imhist() can't both plot and return counts in a single call.
% so either we must always:
% - use imhist() to only get counts, and do the plotting ourselves.
% this is what the given answer suggests, but it's more complicated
% if you actually want the colorstripe and linked axes that imhist() provides.
% - call imhist() twice to get the counts and then to get a plot to modify.
% this is easy, but wasteful.
% - or just get the counts from the descendant stem() object
% this is probably the simplest way:
hstem = stemax.Children;
stemax.YLim = [0 1.1*max(hstem.YData)];
Can we simply just multiply the current YLim by some fixed factor to undo the tight YLim scaling that imhist() does? Off the top of my head, I'm going to say no, since the way imhist() does it is dependent on the intensity variance. The tight YLim is not always smaller than the maximum of the distribution (e.g. for a uniform distribution). If we need the distribution (i.e. the counts), then we still have to jump through the same hoops as before to get it.
Adjusting XLim:
The other potential need is to pick custom XLimits. This isn't a likely scenario if OP is doing the zooming manually in practice. In fact, this is a more difficult problem to solve -- which is why it's unlikely that OP is doing it manually.
I see two cases where this is needed:
  • improperly-scaled floating-point images (i.e. integer-class images cast as float without rescaling)
  • non-native integer scale images (e.g. uint10 or uint12-scale data in some wider native class)
In the first case, the suggestion is simply to avoid causing these problems for yourself. Keep things in proper scale unless you want all scale-dependent tools to interpret them incorrectly. Use im2double() instead of double().
The second case is still an improperly-scaled image, but IPT doesn't have convenience tools to put these into any native properly-scaled form. We'd either have to rescale the data ourselves, or we'd have to wrangle imhist() into handling them in non-native scale. The latter is difficult, since it also requires customizing the colorstripe and rescaling the YLim. Not only is it cumbersome, it involves potential inaccuracies due to rescaling.
In this answer, I show multiple approaches to handling images of this sort.
Therein, I give three core examples:
  • using imhist() with a bunch of modifications
  • using histcounts() and stem() with no colorstripe
  • using MIMT imhistFB()
While imhistFB() is the simple choice, using histcounts() and stem() without the colorstripe is accessible and far less hassle than trying to beat the functionality out of imhist().
Remarks (i.e. the way I'd do it):
There are problems with imhist() other than these inconveniences. See this thread for information:
The answer to that thread is the same that I'd recommend for anything more complicated than a simple YLim adjustment. If you're trying to do things with imhist() that imhist() can't do, then you're probably just going to waste a lot of time. MIMT imhistFB() has these particular bugs fixed, and it offers other functionality that greatly simplifies all these tasks. It can:
  • plot while also returning counts in a single call
  • return axes handles (both are labeled)
  • YLim scaling behavior can be controlled via 'yscale' parameter
  • XLim scaling can be specified explicitly
It also offers other options that might be useful in other cases:
  • The bin alignment can be specified
  • The plot type can be stem, bar, patch, or staircase
  • The colortable used by the colorstripe can be explicitly specified
Also, the prospect of using improperly-scaled images is somewhat improved within MIMT. Similar to IPT getrangefromclass(), MIMT imclassrange() can be used to programmatically get scaling factors for both native and non-native scales. That helps if we want to just have imhistFB() handle the non-native scaled data directly. Limits of arbitrarily-scaled float images can be trivially obtained using imrange().
While imhistFB() can simply accept an appropriate range parameter, the aforementioned option of putting the image in native scale is simplified. MIMT imcast() and imrescale() support non-native integer scales, both signed and unsigned. That makes our image usable by any other scale-dependent tools without needing to jump through hoops anymore.

카테고리

Help CenterFile Exchange에서 Data Exploration에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by