creating mask around line

23 次查看(过去 30 天)
bes
bes 2012-10-9
I have an edge detected image. I have to compute all the pixel coordinates which lies near the edge line. That means matlab have to create a mask (region) around the line first and then it should compute the coordinates of all the points lying within the mask. If I know the region then can easily compute the points using poly2mask
x = [28 59 59 28 28];
y = [38 40 44 42 38];
[rows, cols] = size(im);
myregion = poly2mask(x,y,cols,rows);
STATS = regionprops(myregion, 'PixelList','PixelIdxList');
My problem is I know the endpoints of the line. How can I get the vertices of the rectangle (mask) from that
For example if the line endpoints are (30,40) and (57,44) if i give mask size = 2 it should create a polygon vertices (28,38) , (59,40), (28,42) , (59,44)
if line endpoints (30,40) and (28,80) with mask size = 2 then vertices of polygon should be (28,38), (32,38), (26,82), (30,82) How i can i do this?
Or is there any easy way to get the coordinates of the points (pixel) lying within the mask using line end points?

采纳的回答

Matt J
Matt J 2012-10-9
编辑:Matt J 2012-10-9
There are infinitely many rectangles enclosing a given line. The particular rectangle you want, and the rule you use to get its vertices, is not clear to me. Below is a way of finding a rectangle with edges parallel/perpendicular to the line and at a distance of masksize from the nearest endpoint.
%data
pt1=[30,40];
pt2=[57,44];
masksize=2;
%engine
dirvec=pt2-pt1;
dirvec=dirvec/norm(dirvec);
perpvec=[dirvec(2), - dirvec(1)];
perpvec=perpvec/norm(perpvec);
%masksize=masksize/sqrt(2);
vertex1 = pt1 + masksize*(-dirvec+perpvec),
vertex2 = pt1 + masksize*(-dirvec-perpvec),
vertex3 = pt2 + masksize*(dirvec+perpvec),
vertex4 = pt2 + masksize*(dirvec-perpvec),
  2 个评论
bes
bes 2012-11-14
http://picturepush.com/public/11402453. I asked something like this a rectangle around the dark black color line (mask size should be same -sorry about the poor image). Your code works well. thankyou

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2012-11-14
Sounds like another case of someone asking how to do something without giving us the big picture of why they want to do it. What if your edge outline looks like a "C" - what good are the endpoints going to do for you? What does the box you get from those endpoints mean?
Anyway to answer your question, use imdilate to expand your lines
dilatedImage = imdilate(edgeImage, false(2));
Then, don't call poly2mask(). Just call regionprops and ask for the bounding box.
blobMeasurements = regionprops(dilatedImage, 'BoundingBox');
Then, "to get the coordinates of the points (pixel) lying within the mask" you need to use meshgrid on each box (adapted from my blobs demo):
for k = 1 : numberOfBlobs % Loop through all blobs.
% Find the bounding box of each blob.
thisBlobsBoundingBox = blobMeasurements(k).BoundingBox;
x1 = ceil(thisBlobsBoundingBox(1));
y1 = ceil(thisBlobsBoundingBox(2));
x2 = x1 + thisBlobsBoundingBox(3) - 1;
y2 = y1 + thisBlobsBoundingBox(4) - 1;
[allXs, allYs] = meshgrid(x1:x2, y1:y2);
end
This will give you all the (x,y) coordinates inside the bounding box of each outline/curve/line. Note that the bounding box of a "C" will be the actual bounding box, not the bounding box only around the endpoints that you (for some reason) asked for. Of course if your edge outlines are straight lines, then they're the same thing, but if it's not a line, then they're not.
Anyway, I'm not sure a list of all the pixels coordinates inside the bounding box is what you need, like I said before. If you want to clarify, please go ahead.
  1 个评论
bes
bes 2013-2-12
I have some lines and have to get the best fit line of those lines so first i got the pixel cordinates which lying on that line and inputed to ransac algorithm that works ok but i just thought to try buy giving some points near the line as well.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by