The following image is a mask derived from an imaged feature which extends to the top of the frame, but due to some lighting effects the top portion doesn't come in. Bear in mind that this is one example, and the feature moves around to some degree in each measurement.

enter image description here

I've been dealing with this by combining this mask with it's vertically reflected cousin:

mask = ImageAdd[mask,ImageReflect[FillingTransform@Erosion[mask, DiskMatrix[2]], Top]]

But often this feature isn't quite so symmetric in the x-direction, and sometimes appears skewed from the vertical, so this solution isn't very robust.

Is there a good way to morphologically extend the mask to the top of the image?

  • $\begingroup$ Your code has a typo and does not run. The brackets are not balanced. $\endgroup$
    – yohbs
    May 12, 2016 at 19:48
  • $\begingroup$ @yohbs Thanks. Should work now. $\endgroup$
    – dionys
    May 12, 2016 at 20:01
  • $\begingroup$ I have addressed issues like this by Hough line/edge finding (this case L/R near vertical edges) and extending these to the top of the image and then using a measure of the edge variation to synthesize the edge "noise". This does assume that the L/R edge are supposed to be staright. $\endgroup$ May 12, 2016 at 20:19
  • $\begingroup$ You could use ComponentMeasurements to get the centroid, angle and caliper width and simply draw a line with the same location, angle and thickness. $\endgroup$ May 13, 2016 at 9:07
  • $\begingroup$ @nikie That's a clever approach. One could probably use the major axis of the best-fit ellipse for the component and draw a line from the center to the upper edge. $\endgroup$
    – dionys
    May 13, 2016 at 9:45

1 Answer 1


One possibility is to use Dilation with a tall vertical structuring element. For example:

mask = Import["http://i.stack.imgur.com/L60Yd.png"]
Dilation[Erosion[mask, 2], ConstantArray[1, {160, 1}]]

enter image description here

  • $\begingroup$ I like this idea a lot ... sort of a directional dilation. Unfortunately, this doesn't work so well if the feature is slanted a bit, or has a non uniform width. $\endgroup$
    – dionys
    May 12, 2016 at 21:20
  • 1
    $\begingroup$ You can choose the dilation matrix to point in the same direction as the feature you want to dilate... so if the feature is slanted, so should be the structuring element. If you want to extract that automatically, you'd need to provide more examples. $\endgroup$
    – bill s
    May 12, 2016 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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