这是我的解决方案,以帮助任何人的id。void FilledRoundedRectangle(cv::Mat& src, //Image where rect is drawn
cv::Point topLeft, //top left corner
cv::Size rectSz, //rectangle size
const cv::Scalar fillColor, //fill color
const int lineType, //type of line
const int delta, //angle between points on the ellipse
const float cornerCurvatureRatio) //curvature of the corner
{
// corners:
// p1 - p2
// | |
// p4 - p3
//
cv::Point p1 = topLeft;
cv::Point p2 = cv::Point (p1.x + rectSz.width, p1.y);
cv::Point p3 = cv::Point (p1.x + rectSz.width, p1.y + rectSz.height);
cv::Point p4 = cv::Point (p1.x, p1.y + rectSz.height);
int cornerRadius = static_cast(rectSz.height*cornerCurvatureRatio);
std::vector<:point> points;
std::vector<:point> pts;
// Add arcs points
cv::Size rad = cv::Size(cornerRadius, cornerRadius);
// segments:
// s2____s3
// s1 s4
// | |
// s8 s5
// s7_____s6
//
//Add arc s1 to s2
cv::ellipse2Poly(p1 + cv::Point(cornerRadius, cornerRadius) , rad, 180.0, 0, 90, delta , pts);
points.insert(points.end(), pts.begin(), pts.end());
pts.clear();
//Add line s2-s3
points.push_back(cv::Point (p1.x + cornerRadius, p1.y)); points.push_back(cv::Point (p2.x - cornerRadius, p2.y));
//Add arc s3 to s4
cv::ellipse2Poly(p2 + cv::Point(-cornerRadius, cornerRadius) , rad, 270.0, 0, 90, delta, pts);
points.insert(points.end(), pts.begin(), pts.end());
pts.clear();
//Add line s4 to s5
points.push_back(cv::Point (p2.x, p2.y + cornerRadius)); points.push_back(cv::Point (p3.x, p3.y - cornerRadius));
//Add arc s5 to s6
cv::ellipse2Poly(p3 + cv::Point(-cornerRadius, -cornerRadius), rad, 0.0, 0, 90, delta, pts);
points.insert(points.end(), pts.begin(), pts.end());
pts.clear();
//Add line s7 to s8
points.push_back(cv::Point (p4.x + cornerRadius, p4.y)); points.push_back(cv::Point (p3.x - cornerRadius, p3.y));
//Add arc s7 to s8
cv::ellipse2Poly(p4 + cv::Point(cornerRadius, -cornerRadius) , rad, 90.0, 0, 90, delta, pts);
points.insert(points.end(), pts.begin(), pts.end());
//Add line s1 to s8
points.push_back(cv::Point (p1.x, p1.y + cornerRadius)); points.push_back(cv::Point (p4.x, p4.y - cornerRadius));
//fill polygon
cv::fillConvexPoly(src, points, fillColor, lineType);
}
int main(int argc, char** argv)
{
try
{
cv::Mat img = cv::Mat(600, 600,CV_8UC1,cv::Scalar(0));
cv::Point topLeft(179, 179);
cv::Size rectSz(321, 321);
cv::Scalar fillColor(255, 255, 255);
int delta = 1; //every 1 degree
int lineType = cv::LINE_AA;
float cornerCurvatureRatio = 0.1;
FilledRoundedRectangle(img,
topLeft,
rectSz,
fillColor,
lineType,
delta,
cornerCurvatureRatio);
cv::imshow("", img);
cv::waitKey(0);
return 0;
std::cin.get();
} //end try
catch ( std::exception const & ex )
{
std::string errMsg = ex.what();
printf( "%s\n", errMsg.c_str() );
}
catch ( ... )
{
printf( "Error: unknown exception\n" );
}
}