问题描述
我遇到了麻烦,因为我有这张图片,我想做的只是处理不是黑色的像素.但是我必须找到第一个和最后一个非零值来定义边界,我会工作,问题是我可以找到第一个非零值(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
要找到最后一个:
[~, 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');
这篇关于如何找到图像中列和行的第一个和最后一个非零值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!