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

3 visualizaciones (últimos 30 días)
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 comentarios
alex contreras
alex contreras el 18 de En. de 2022
I want to create an array/matrix of every point that is on that line that dinstinguishes black from white.
alex contreras
alex contreras el 18 de En. de 2022
from this I can subtract every one those points from either image to find the difference.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 18 de En. de 2022
Editada: Image Analyst el 19 de En. de 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 comentarios
Image Analyst
Image Analyst el 19 de En. de 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;

Iniciar sesión para comentar.

Más respuestas (1)

KSSV
KSSV el 18 de En. de 2022
Editada: KSSV el 18 de En. de 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 comentario
alex contreras
alex contreras el 18 de En. de 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{:});

Iniciar sesión para comentar.

Categorías

Más información sobre Image Processing Toolbox en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by