Reputation: 10996
I would like to populate an ellipse shape in OpenCV in such a way that the value it takes is the normalized distance from its center.
Typically in OpenCV, I can fill an image with an elliptical shape as follows:
cv::ellipse(image, e, cv::Scalar(255), CV_FILLED);
However, this gives the ellipse a constant scalar value of 1 and I would like to vary this value based on its distance from the center.
I guess one way would be to go through the points and manually compute this. I am quite new to OpenCV and having trouble doing this with this Mat object.
Upvotes: 0
Views: 2354
Reputation: 1845
Here is the sample code snippet to Find distance Transform of a Ellipse.
You can simply create a mask of that Ellipse region & Find Distance transform for that mask.
Mat mEllipse_Bgr(Size(640,480),CV_8UC3,Scalar(0));
Mat mEllipseMask(mEllipse_Bgr.size(),CV_8UC1,Scalar(0));
// Draw a ellipse
ellipse( mEllipse_Bgr, Point( 200, 200 ), Size( 100.0, 160.0 ), 45, 0, 360, Scalar( 255, 0, 0 ), 1, 8 );
ellipse( mEllipseMask, Point( 200, 200 ), Size( 100.0, 160.0 ), 45, 0, 360, Scalar( 255), -1, 8 );
imshow("Ellipse Image",mEllipse_Bgr);
imshow("Ellipse Mask",mEllipseMask);
// Perform the distance transform algorithm
Mat mDist;
distanceTransform(mEllipseMask, mDist, CV_DIST_L2, 3);
// Normalize the distance Transform image for range = {0.0, 1.0} to view it
normalize(mDist, mDist, 0, 1., NORM_MINMAX);
imshow("Distance Transform Image", mDist);
Upvotes: 1