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

조회 수: 7 (최근 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
alex contreras
alex contreras 2022년 1월 18일
I want to create an array/matrix of every point that is on that line that dinstinguishes black from white.
alex contreras
alex contreras 2022년 1월 18일
from this I can subtract every one those points from either image to find the difference.

댓글을 달려면 로그인하십시오.

채택된 답변

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