2
$\begingroup$

In this program, I need to select the point on the contour image that has the same abscissa as the center of the contour and has the highest ordinate. However, since the contour is not full closed, the last line does not return any thing. How can I fix this problem.

image

edge = MorphologicalPerimeter[ColorNegate@img] // DeleteSmallComponents

coordinatescontour = PixelValuePositions[edge, 1] ;

center = Round[ComponentMeasurements[edge, "BoundingDiskCenter"][[1, 2]]];

radius = Round[ComponentMeasurements[edge, "BoundingDiskRadius"][[1, 2]]];

Show[edge,Graphics[{{Red, Circle[center, radius]}, {Green, Point[center]}}]]

        (*I need to select the point on the contour which has the same abscissa as the center point and has the highest ordinate *)

Sort[Select[coordinatescontour, #[[1]] == center[[1]]&]][[1]]
$\endgroup$
  • $\begingroup$ After the Show command, I see the contour, a green dot, and a red circle. Can you draw a picture of the point you want returned? $\endgroup$ – bill s Jan 23 '16 at 21:39
  • $\begingroup$ In coordinatescontour = [...] // DeleteSmallComponents the DeleteSmallComponents is probably a mistake. Other than that, your code seems to do what you want already. $\endgroup$ – Niki Estner Jan 24 '16 at 10:16
  • $\begingroup$ @nikie, Yes! it was the mistakes that caused the problem. $\endgroup$ – BetterEnglish Jan 24 '16 at 13:31
4
$\begingroup$

I changed your image processing code a bit, by removing the unnecessary DeleteSmallComponents and removing the white lines using Closing. I also added a line to detect the pixel above the center.

img = Import["http://i.stack.imgur.com/nQmQI.png"];   

edge = MorphologicalPerimeter[Closing[ColorNegate@img, DiskMatrix[1]]];
coordinatescontour = PixelValuePositions[edge, 1];
center = Round[ComponentMeasurements[edge, "BoundingDiskCenter"][[1, 2]]];
radius = Round[ComponentMeasurements[edge, "BoundingDiskRadius"][[1, 2]]];

top = SortBy[Select[coordinatescontour, #[[1]] == center[[1]] &], Last] // Last;

Show[
  edge, 
  Graphics[{{Red, Circle[center, radius]}, 
            {Green, Point[center]}, 
            {Blue, PointSize[0.03], Point[top]}
           }]
]

Mathematica graphics

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.