c语言圆角矩形代码,如何在openCV c ++中填充圆角矩形

这是我的解决方案,以帮助任何人的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" );

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值