【问题标题】:Plotting the area of an object in grayscale against its intensity level in the grayscale image根据灰度图像中的强度级别绘制对象的灰度区域
【发布时间】:2014-02-12 21:15:52
【问题描述】:

基本上,我想要生成的是图像在不同灰度强度下的直方图,向我展示了图像中连接组件的区域。

让我进一步解释一下,我计划在不同阈值水平上找到图像的所有连接组件的区域。然后以图形方式将它们全部组合起来,并根据灰度图像的强度级别绘制它们,即0 - 255

我希望我的代码能解释我想要做什么。

img = rgb2gray(imread('W1\Writer1_01_02.jpg'));

for k = 1:-0.01:0.1
     bw_normal = im2bw(img, k);
     bw = imcomplement(bw_normal);
     [label,n] = bwlabel(bw);
     stats = regionprops(label,img, {'Area', 'Centroid'});
      plot([stats.Area],k,'o');
      axis([0 1000 0.1 1])

     hold on;
     
 end

如您所知,我使用 for 循环来生成变化的阈值水平,计算 CC 的面积并将它们与选定的阈值水平进行对比。这是它产生的结果:

这不是我想要的。我试图复制这个结果。它不必看起来完全像这样,但任何非常相似的东西都可以

后来发现可以直接使用STATS = regionprops(..., I, properties)从灰度图中找到CC的属性

所以我写了这个:

img = rgb2gray(imread('W1\Writer1_01_02.jpg'));

for k = 1:-0.01:0.1
     bw_normal = im2bw(img, k);
     bw = imcomplement(bw_normal);
     [label,n] = bwlabel(bw);
     stats = regionprops(label,img, {'Area', 'Centroid'});
%       plot([stats.Area],k,'o');
%       axis([0 1000 0.1 1])
      imshow(img);
     hold on;
     for j = 1:numel(stats)
         text(stats(j).Centroid(1),stats(j).Centroid(2), ...
        sprintf('%2.1f', stats(j).Area), ...
        'EdgeColor','b','Color','r');
     end
     
 end

这产生了以下内容:

所以现在我找到了灰度连接组件的区域。如何绘制它们以显示为我想要的输出(我在上面显示的蓝色)?

感谢您的阅读

【问题讨论】:

  • 您能否提供原始数据。如果我要查看代码来完成某事,使用实际输入可以确保更高质量的输出。
  • 谢谢。不需要具体数据。任何文本图像都可以。就像我最后展示的图片一样。
  • 我认为对于每个灰度级别,您需要提取所有 Area 值的列表并从中构造一个直方图(使用一致的 bin 值)。该“宽度图”中的每一行都是单个阈值级别的直方图。
  • @nkjt 您能否添加一个包含代码的答案来说明如何完成。你所说的似乎产生了我想要的结果,但我不知道如何按照你解释的方式绘制直方图。谢谢

标签: matlab image-processing plot image-segmentation


【解决方案1】:

基于您现有的代码:

img = rgb2gray(imread('W1\Writer1_01_02.jpg'));
k = 1:-0.01:0.1;
bins = 1:100 % change depending on your image

% preallocate output - this will be filled with histograms
histout = zeros(length(k),length(bins); 

for m = 1:length(k); 
     bw_normal = im2bw(img, k(m));
     bw = imcomplement(bw_normal);
     [label,n] = bwlabel(bw);
     stats = regionprops(label,img, {'Area'});
     A = cell2mat(struct2cell(stats));
     histout(m,:) = hist(A,bins);
end

我将regionprops 的输出更改为Area,因为它简化了将输出结构转换为hist 可以读取的内容。从循环 k 更改为预定义向量 k 并在循环中使用 k(m) 只会使索引到 histout 更直接一些。

您可以使用imagesc 显示,然后更正刻度标签:

imagesc(histout)
colormap('jet')
set(gca,'XTickLabel',bins(get(gca,'XTick')));
set(gca,'YTickLabel',k(get(gca,'YTick')));
xlabel('Area')
ylabel('Threshold')

【讨论】:

    猜你喜欢
    • 2014-05-30
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多