Find the (x,y) coordinate of a "white" pixel (from calculated matrix)

5 ビュー (過去 30 日間)
Alberto Acri
Alberto Acri 2023 年 2 月 3 日
編集済み: Image Analyst 2023 年 2 月 3 日
Hello! I want to determine the coordinate (x,y) of any "white" pixel that is in contact with a "black" pixel.
For example, in the detail in the image below, I have highlighted some pixels with a red square. I would like to determine the coordinate of just one of those pixels, whatever.
I thought about determining the matrix of pixels in this way but I wouldn't know how to go about it.
image = imread('image_BW.png');
Number_Of_Rows = height(image);
Number_Of_Columns = width(image);
grayLevel = zeros(Number_Of_Rows,Number_Of_Columns);
for Row = 1:Number_Of_Rows
for Column = 1:Number_Of_Columns
grayLevel(Row,Column) = image(Row, Column);
end
end
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 2 月 3 日
編集済み: Dyuman Joshi 2023 年 2 月 3 日
Do you want to find the coordinates of all the white pixels in the contact with black pixels or selected white pixels?
It's difficult to find specific indices without any specific input to differentiate them from others.
You can find the left most point -
img = imread('image_BW.png');
for jdx=1:size(img,2)
for idx=1:size(img,1)
if isequal(img(idx,jdx,:),255*ones(1,1,3))
img(idx,jdx,:)=[255 0 0];
imshow(img)
%you can see a tiny red dot in the image
return
end
end
end

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

採用された回答

Image Analyst
Image Analyst 2023 年 2 月 3 日
編集済み: Image Analyst 2023 年 2 月 3 日
You can use bwperim to find pixels on the perimeter, in contact with black:
rgbImage = imread('image_BW.png');
if size(rgbImage, 3) == 3
grayImage = rgb2gray(rgbImage); % Need to convert RGB image into gray scale.
else
grayImage = rgbImage; % If it's already gray scale.
end
mask = grayImage > 0 ; % converts the image to logical values sutaible for image morphology
perimImage = bwperim(mask);
% Find row and column coordinate of all perimeter pixels
[edgeRows, edgecolumns] = find(perimImage);
Don't use i (the imaginary constant) for the name of an image variable.
You can also use bwboundaries to find them and visboundaries to display them.
boundaries = bwboundaries(mask);
visboundaries(boundaries); % Display them over the image.
or you can get the x and y coordinates of each individual outline (there may be several) this way:
% Plot the borders of all the blobs in the overlay above the original grayscale image
% using the coordinates returned by bwboundaries().
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
imshow(grayImage); % Optional : show the original image again. Or you can leave the binary image showing if you want.
% Here is where we actually get the boundaries for each blob.
boundaries = bwboundaries(mask);
% boundaries is a cell array - one cell for each blob.
% In each cell is an N-by-2 list of coordinates in a (row, column) format. Note: NOT (x,y).
% Column 1 is rows, or y. Column 2 is columns, or x.
numberOfBoundaries = size(boundaries, 1); % Count the boundaries so we can use it in our for loop
% Here is where we actually plot the boundaries of each blob in the overlay.
hold on; % Don't let boundaries blow away the displayed image.
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k}; % Get boundary for this specific blob.
x = thisBoundary(:,2); % Column 2 is the columns, which is x.
y = thisBoundary(:,1); % Column 1 is the rows, which is y.
plot(x, y, 'r-', 'LineWidth', 2); % Plot boundary in red.
end
hold off;
caption = sprintf('%d Outlines, from bwboundaries()', numberOfBoundaries);
fontSize = 15;
title(caption, 'FontSize', fontSize);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.

その他の回答 (1 件)

Sarvesh Kale
Sarvesh Kale 2023 年 2 月 3 日
As per my understanding you are trying to extract pixels x,y co-ordinates which are on the boundary, you can do this using the following lines of code, the approach used includes image morphological operation where we shrink the image using a structuring element and then perform logical and of original image and inverted image obtained from morphological operation to obtain boundary image and then use find function to get x, y co-ordinates of all the boundary pixels,
clear
clc
i = imread('image_BW.png') ;
i = rgb2gray(i);
i = i > 0 ; % converts the image to logical values sutaible for image morphology
imshow(i);
se1=strel('square',5); % you can also try reducing the size of structuring element to get thin boundries
er_i = imerode(i,se1); % perform image erosion using the structuring element se1
subplot 311;
imshow(i);
subplot 312;
imshow(er_i);
i_boundary = ~er_i & i ; % only pixels which are in contact with black boundary highlighted
subplot 313;
imshow(i_boundary);
[x,y]=find(i_boundary);
the x and y vector will contain the co-ordinates of the white pixels on the boundary for example x(i) and y(i) will be the co-ordinates of a particular white pixel.
the documentation for find function and imerode function are given below

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by