如何在图像中找到列和行的第一个和最后一个非零值? [英] How to find the first and last non-zero values for column and row in an image?
问题描述
我遇到了麻烦,因为我有这张图片,我想做的就是使用不是黑色的像素。但是我必须找到第一个和最后一个非零值来定义边界,我会工作,问题是我可以找到第一个非零值(rowandcolumn),但是在最后一个列中出现的值是1799,而我的图像是499x631x3 uint8,它应该是533.有什么问题?
I'm having trouble, because I have this image, what I want to do is just work with the pixels that aren't black. But I have to find the first and last nonzero values to define the boundaries were I will work, the problem is that I can find the first nonzero values(rowandcolumn), but in the last for the column appears the value 1799,and my image is 499x631x3 uint8 , and it should be 533. What is the problem??
我的代码如下:
%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
推荐答案
要查找与每列第一个非零元素对应的行索引:
To find the row index corresponding to the first nonzero element of each column:
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
查找 last :
[~, 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
并将您的代码应用于:
To find the first and last in linear indexing sense: compute A2
as above and apply your code to that:
[r_min, c_min]=find(A2,1,'first');
[r_max, c_max]=find(A2,1,'last');
这篇关于如何在图像中找到列和行的第一个和最后一个非零值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!