Find pixel coordinates to the right and to the left of a given target pixel

11 visualizzazioni (ultimi 30 giorni)
In the image below (also attached), the central line of the object is burned with the value 1. The pixel values of the object are equal to two, and the background pixels (black) have values equal to zero.
Given a pixel from the central line, I want to extract all pixels to the right and the left of it, in the same direction of the object's orientation.
Any ideias on how to do this?

Risposta accettata

Matheus Ferreira
Matheus Ferreira il 9 Giu 2022
I find a way to do this. I will post my answer to anyone interested.
Given the image "I.png" (attached) I fitted a linear model to find the central line and move the intercept to move the central line to the right and to the left. The pixel value of a given pixel represent the number of pixels this pixel is far from the center.
% Fit a linear model using pixel coordinates to find the center line
[y,x] = find(I) ;
p = polyfit(x,y,1);
xi = linspace(min(x),max(x),max(x)) ;
yi = polyval(p,xi) ;
frameLength=100;
Izeros=zeros(size(I));
% Left side
for i= 1:frameLength
aux_p=p;
aux_p(1,2)=p(1,2)+i; % Change the intercept
yi = polyval(aux_p,xi);
centralLinePixValue=i+1;
for k = 1 : length(xi)
row = round(yi(k));
col = round(xi(k));
if row>size(I,1)||col>size(I,2)
row=size(I,1);
col=size(I,2);
end
Izeros(row, col) = centralLinePixValue;
end
end
% Right side
frameLength=70;
for i= 1:70
aux_p=p;
aux_p(1,2)=p(1,2)-i; % Change the intercept
yi = polyval(aux_p,xi);
centralLinePixValue=i+1;
for k = 1 : length(xi)
row = abs(round(yi(k)));
col = abs(round(xi(k)));
if row==0||col==0
row=1;
col=1;
end
if row>size(I,1)||col>size(I,2)
row=size(I,1);
col=size(I,2);
end
Izeros(row, col) = centralLinePixValue;
end
end
Izeros=Izeros.*I;
figure, imshow(Izeros,[])

Più risposte (1)

DGM
DGM il 8 Giu 2022
Modificato: DGM il 8 Giu 2022
I'm going to assume you already have a method for finding the central line. I'm just going to extract it from the example image given.
The easy way would simply be to find the angle of the object centerline and then rotate the image so that it's grid-aligned. At that point, it's just a matter of addressing columns from the image.
A = imread('tiltrect.png');
A = rgb2gray(A);
% crop off border artifacts
A = A(3:end-2,3:end-2);
% find angle of object line
objline = A==128;
[H,T,R] = hough(objline);
P = houghpeaks(H,1,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(objline,T,R,P,'FillGap',50,'MinLength',7);
theta = sign(lines.rho)*90 - lines.theta
theta = -23
% rotate the object image to correct for presentation angle
objpict = logical(A);
objpict = imrotate(objpict,-theta);
% close-crop image to object extents
S = regionprops(objpict,'image');
objpict = S(1).Image;
imshow(objpict)
Of course, it's usually unclear what people mean when they say they want to "extract pixels". If you were to literally extract the pixels perpendicular to the centerline through a given point, all you would have is a variable-length vector of ones. The character of ones are not unique. The only information you might get from the process is the length of said vector -- in which case, the question is "how do I calculate the width of the object".
On the other hand, if the logical image is merely a reference to another (unseen) image and you want to extract the pixels from that image, then you'd obviously have to apply the rotation to it instead.
  7 Commenti
Image Analyst
Image Analyst il 9 Giu 2022
Note that the Orientation will in general not be down the centerline of the rectangle since it's the angle of an ellipse fitted to the data. So usually it's tilted towards one of the diagonals. So you might have to use
or bwferet to find the centerline. Then you can rotate it with imrotate if you want. If you just want to find the distance of all pixels from the centerline, and again I'm not sure you need to, then you can just make the center line a binary image and get the distance transform with bwdist. This will get you the distance of all pixels to the closest point in the line.

Accedi per commentare.

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by