-2

To be exact I need the four end points of the road in the image below.

enter image description here

I used find[x y]. It does not provide satisfying result in real time.

PinkTurtle
  • 6,942
  • 3
  • 25
  • 44

2 Answers2

1

I'm assuming the images are already annotated. In this case we just find the marked points and extract coordinates (if you need to find the red points dynamically through code, this won't work at all)

The first thing you have to do is find a good feature to use for segmentation. See my SO answer here what-should-i-use-hsv-hsb-or-rgb-and-why for code and details. That produces the following image:

color spaces

we can see that saturation (and a few others) are good candidate colors spaces. So now you must transfer your image to the new color space and do thresholding to find your points.

Points are obtained using matlab's region properties looking specifically for the centroid. At that point you are done.

Here is complete code and results

im = imread('https://i.stack.imgur.com/eajRb.jpg');
HUE = 1;
SATURATION = 2;
BRIGHTNESS = 3;

%see https://stackoverflow.com/questions/30022377/what-should-i-use-hsv-hsb-or-rgb-and-why/30036455#30036455
ViewColoredSpaces(im)

%convert image to hsv
him = rgb2hsv(im);

%threshold, all rows, all columns, 
my_threshold = 0.8;     %determined empirically
thresh_sat = him(:,:,SATURATION) >  my_threshold;

%remove small blobs using a 3 pixel disk
se = strel('disk',3');
cleaned_sat = imopen(thresh_sat, se);% imopen = imdilate(imerode(im,se),se)

%find the centroids of the remaining blobs
s = regionprops(cleaned_sat, 'centroid');
centroids = cat(1, s.Centroid);

%plot the results
figure();
subplot(2,2,1)  ;imshow(thresh_sat) ;title('Thresholded saturation channel')
subplot(2,2,2)  ;imshow(cleaned_sat);title('After morpphological opening')
subplot(2,2,3:4);imshow(im)         ;title('Annotated img')

hold on
for (curr_centroid = 1:1:size(centroids,1))
    %prints coordinate
    x = round(centroids(curr_centroid,1));
    y = round(centroids(curr_centroid,2));
    text(x,y,sprintf('[%d,%d]',x,y),'Color','y');
end
%plots centroids
scatter(centroids(:,1),centroids(:,2),[],'y')
hold off

%prints out centroids
centroids

centroids =

7.4593 143.0000
383.0000 87.9911
435.3106 355.9255
494.6491 91.1491


color spaces color spaces color spaces

Community
  • 1
  • 1
andrew
  • 2,451
  • 1
  • 15
  • 22
-2

Some sample code would make it much easier to tailor a specific solution to your problem.

One solution to this general problem is using impoint.

Something like

h = figure();
ax = gca; 

% ... drawing your image

points = {};
points = [points; impoint(ax,initialX,initialY)];

% ... generate more points

indx = 1 % or whatever point you care about
[currentX,currentY] = getPosition(points{indx});

should do the trick.

Edit: First argument of impoint is an axis object, not a figure object.