Find index position of the second last '1' per row in a BW image
4 次查看(过去 30 天)
显示 更早的评论
I have a image (or matrix) with only '0' and '1'.
I need to find the index of the second last of '1' in each row.
The matrix (E) is the result of finding the profile of a BW image.
[E,th]=edge(BW,'canny'); %profile of the image.
surface = accumarray(row,col,[size(E,1),1],@max,NaN); %
this gives the maximum index per row but I need the second maximum.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/795339/image.jpeg)
0 个评论
回答(2 个)
Image Analyst
2021-11-9
Try this to find the last column in each row that is true for your binary image
[rows, columns] = size(E)
lastColumns = zeros(1, columns);
for row = 1 : rows
% Scan down row-by-row.
thisRow = E(row, :);
% Find last true value
r = find(thisRow, 1, 'last'); % Will be empty if there are no white pixels in that row.
if ~isempty(r)
% There is at least one white pixel in that row,
% so record its position.
lastColumns(row) = r;
end
end
If you need starting and stopping columns for each of the blobs, you will have a problem and may need to take a different approach. That's because you can't simply fill the outlines because some of your outlines are broken/open and can't be filled so you can't just use find(thisRow) and take all the even numbered indexes. You may need to reevaluate whether Canny is something you really need to do. In most cases edge detection is not the best approach but I'd have to see your original image.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!