【问题标题】:How to find the first and last non-zero values for column and row in an image?如何找到图像中列和行的第一个和最后一个非零值?
【发布时间】:2015-03-26 12:43:14
【问题描述】:

我遇到了麻烦,因为我有这张图片,我想做的只是处理不是黑色的像素。但是我必须找到第一个和最后一个非零值来定义边界,我会工作,问题是我可以找到第一个非零值(rowandcolumn),但在列的最后一个出现值 1799,我的图像是 499x631x3 uint8 ,应该是 533 有什么问题??

我的代码如下:

%Find where the image begins and starts
[r_min, c_min]=find(movingRegistered(:),1,'first');
[r_max, c_max]=find(movingRegistered(:,:),1,'last');

图片链接https://www.dropbox.com/s/6fkwi3xbicwzonz/registered%20image.png?dl=0

【问题讨论】:

  • 是否要查找与每列的第一个和最后一个非零值对应的行?还是绕路?还是……?
  • 我想找到定义图像“矩形”的行和列,其中包含有关图像的信息开始和结束的信息。这些是对我来说唯一重要的像素。请看图片链接。

标签: matlab indexing find


【解决方案1】:

要找到每列的第一个个非零元素对应的行索引:

A2 = logical(any(A,3)); %// reduce to 2D array, which equals 0 if all three color
                        %// components are 0, and 1 otherwise
[~, row_first] = max(A2,[],1); %// the second output of `max` gives the row index of
                               %// the first maximum within each column

要找到最后一个

[~, row_last] = max(flipud(A2),[],1); %// matrix upside down to find last, not first
row_last = size(A,1)-row_last+1; %// correct because matrix was upside down

要找到 线性索引 意义上的第一个和最后一个:如上计算 A2 并将您的代码应用于此:

[r_min, c_min]=find(A2,1,'first');
[r_max, c_max]=find(A2,1,'last');

【讨论】:

  • 谢谢,但这与我想要的不同。我的图像有一个带有信息的“矩形”,而其他部分是黑色的,我只想定义“信息的矩形”以仅使用该像素。我想要的是所有图像的第一个和最后一个非零值。而且我不明白为什么我的代码给 col_last 的值 1799,如果我的图像是 499x631x3 uint8 。你有什么建议吗?
  • 哦,我明白了。尝试A2 = logical(any(A,3)); 并将您的代码应用到A2。我认为这应该有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-15
  • 2019-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多