How do I use the find function to track every last point that is black, so as to create a matrix of that border between white a black image. See Below

1 次查看(过去 30 天)
I used the find function to find a single point, the row at which the image is black, in the first column. But I want to do this for every column, so as to create a defined line of every row and and column, instead of a single point
clear;clc;
img1 = imread('s1.png');
img2 = imread('s2.png');
img1BW = rgb2gray(img1);
img2BW = rgb2gray(img2);
counter = 0;
for k = 1:length(img1BW);
counter = counter+1;
end
for k = 1:length(img2BW);
[r2,c] = find(img2BW(:,1:end),1,'last');
counter = counter+1;
end
Border1 = [r1,c]
Border2 = [r2,c]
final = abs(r2-r1);
fprintf('The displacement of the black border from image 1 and image 2 is : %1.000f pixels \n',final);
  3 个评论

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2022-1-18
编辑:Image Analyst 2022-1-19
Try this. It assumes the top part is all white, and the only black is a big region along the bottom, which doesn't have to be perfectly rectangular.
[rows, columns] = size(img1BW);
topEdgeOfBlack = zeros(1, columns);
% Scan column-by-column finding out where (what row) the first black pixel
% in each column appears.
for col = 1 : columns
topEdgeOfBlack(col) = find(img1BW(:, col) == 0, 1, 'first');
end
hold on;
x = 1 : columns;
plot(x, topEdgeOfBlack, 'r-', 'LineWidth', 3);
hold off;
Alternatively you can find the last white pixel in each column like this:
[rows, columns] = size(img1BW);
bottomEdgeOfWhite = zeros(1, columns);
% Scan column-by-column finding out where (what row) the last white pixel
% in each column appears.
for col = 1 : columns
bottomEdgeOfWhite(col) = find(img1BW(:, col) > 0, 1, 'last');
end
hold on;
x = 1 : columns;
plot(x, bottomEdgeOfWhite, 'r-', 'LineWidth', 3);
hold off;
  9 个评论
Image Analyst
Image Analyst 2022-1-19
Try this:
img1 = imread('s3.png');
img1BW = rgb2gray(img1);
imshow(img1BW);
axis('on', 'image');
impixelinfo;
[rows, columns] = size(img1BW);
bottomEdgeOfWhite = zeros(1, columns);
% Scan column-by-column finding out where (what row) the last white pixel
% in each column appears.
for col = 1 : columns
bottomEdgeOfWhite(col) = find(img1BW(:, col) > 0, 1, 'last');
end
hold on;
x = 1 : columns;
plot(x, bottomEdgeOfWhite, 'r-', 'LineWidth', 3);
hold off;

请先登录,再进行评论。

更多回答(1 个)

KSSV
KSSV 2022-1-18
编辑:KSSV 2022-1-18
I = imread(myimage) ;
I1 = imbinarize(rgb2gray(I)) ;
[y,x] = find(I1) ;
idx = boundary(I1) ;
plot(x(idx),y(idx))
You can also use regionprops. Have a look on the function.
  1 个评论
alex contreras
alex contreras 2022-1-18
I am getting this error:
Error using boundary>sanityCheckPoints (line 172)
The input points must be 2D or 3D coordinates in numpoints-by-ndim format.
Error in boundary>sanityCheckInput (line 117)
sanityCheckPoints(P);
Error in boundary (line 54)
[P, S] = sanityCheckInput(varargin{:});

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

产品


版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by