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;
alex contreras
alex contreras 2022 年 1 月 19 日
Thank you very much!! I really appreciate it.

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeImage Processing Toolbox についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by