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 view (last 30 days)
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 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 18 Jan 2022
Edited: Image Analyst on 19 Jan 2022
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 Comments
Image Analyst
Image Analyst on 19 Jan 2022
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;

Sign in to comment.

More Answers (1)

KSSV
KSSV on 18 Jan 2022
Edited: KSSV on 18 Jan 2022
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 Comment
alex contreras
alex contreras on 18 Jan 2022
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{:});

Sign in to comment.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!