双上的 dohist - matlab

发布于 2024-12-13 06:22:09 字数 712 浏览 6 评论 0原文

如何将表示为双精度的图像转换为可用于生成直方图的图像?

(与多希斯特:)

% computes the histogram of a given image into num bins.
% values less than 0 go into bin 1, values bigger than 255
% go into bin 255
% if show=0, then do not show. Otherwise show in figure(show)


function thehist = dohist(theimage,show)

  % set up bin edges for histogram

  edges = zeros(256,1);
  for i = 1 : 256;
    edges(i) = i-1;
  end

  [R,C] = size(theimage);
  imagevec = reshape(theimage,1,R*C);      % turn image into long array
  thehist = histc(imagevec,edges)';        % do histogram

  if show > 0
      figure(show)
      clf
      pause(0.1)
      plot(thehist)
      axis([0, 256, 0, 1.1*max(thehist)])
  end

how do i convert an image represented as double into an image that i can use to produce a histogram?

(with dohist:)

% computes the histogram of a given image into num bins.
% values less than 0 go into bin 1, values bigger than 255
% go into bin 255
% if show=0, then do not show. Otherwise show in figure(show)


function thehist = dohist(theimage,show)

  % set up bin edges for histogram

  edges = zeros(256,1);
  for i = 1 : 256;
    edges(i) = i-1;
  end

  [R,C] = size(theimage);
  imagevec = reshape(theimage,1,R*C);      % turn image into long array
  thehist = histc(imagevec,edges)';        % do histogram

  if show > 0
      figure(show)
      clf
      pause(0.1)
      plot(thehist)
      axis([0, 256, 0, 1.1*max(thehist)])
  end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

煞人兵器 2024-12-20 06:22:09

我猜你只需要先标准化你的图像,为此你可以使用:

255*(theimage./(max(theimage(:)));

I am guessing that you just need to normalize your image first, to do this you can use:

255*(theimage./(max(theimage(:)));
雪若未夕 2024-12-20 06:22:09

您的代码看起来不错,您可以确保边界得到正确处理:

theimage(theimage<0) = 0;
theimage(theimage>255) = 255;

但这不是必需的,通常当您使用 imread 读取图像时,您会得到范围 [0,1] 或 uint8 [0,255] 的双图像()。在这种情况下,如果需要,只需重新缩放到 [0,255] 即可。

其他一些提示:

您可以像这样制作边缘向量:

edges = 0:255;

在这种情况下,theimage(:)reshape(theimage,1,R*C) 相同因为你想要一个长向量。

Your code seems fine, you could make sure the bounds get treated correctly with:

theimage(theimage<0) = 0;
theimage(theimage>255) = 255;

But this shouldnt be necessary, usually you either get a double image ranging [0,1] or uint8 [0,255] when you read an image with imread(). Just rescale to [0,255] in this case if needed.

Some other tips:

You can make the edges-vector like this:

edges = 0:255;

And theimage(:) is the same as reshape(theimage,1,R*C) in this case since you want one long vector.

橘虞初梦 2024-12-20 06:22:09

内置函数hist可以直接应用于double类的图像。

Matlab 文档链接

如果您怀疑图像有 N 位区间 [A,B] 上的分辨率,您可以直接在图像上调用 hist (无需转换),例如:

[H,bins] = hist(IM,linspace(A,B,2^N));

检索直方图和 bin 或

hist(IM,linspace(A,B,2^N));

简单地绘制直方图。

The built-in function hist can be applied directly to images of class double.

Matlab documentation link

If you have an image which you suspect to have N bits of resolution on the interval [A,B], you can call hist directly on the image (without conversion) like:

[H,bins] = hist(IM,linspace(A,B,2^N));

to retrieve the histogram and bins or

hist(IM,linspace(A,B,2^N));

to simply plot the histogram.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文