在OpenCV C++中填充圆角矩形,可以使用以下步骤:
#include <opencv2/opencv.hpp>
cv::Mat image(height, width, CV_8UC3, cv::Scalar(0, 0, 0));
其中,height和width分别表示图像的高度和宽度,CV_8UC3表示图像类型为8位无符号整数,3个通道(RGB),cv::Scalar(0, 0, 0)表示填充颜色为黑色。
cv::Scalar fill_color(255, 255, 255); // 填充颜色为白色
cv::Point2f center(center_x, center_y); // 圆角矩形的中心点坐标
cv::Size2f size(width, height); // 圆角矩形的宽度和高度
int corner_radius = radius; // 圆角的半径
cv::Rect rect(center - size / 2, center + size / 2);
cv::rectangle(image, rect, fill_color, -1);
cv::Scalar corner_fill_color(0, 0, 0); // 圆角填充颜色为黑色
cv::RNG rng;
for (int i = 0; i < corner_radius; ++i) {
cv::Point2f center_top_left(rect.x + corner_radius, rect.y + corner_radius);
cv::Size2f size_top_left(corner_radius * 2 - i * 2, corner_radius * 2 - i * 2);
cv::ellipse(image, center_top_left, size_top_left, 180.0, 0.0, 90.0, corner_fill_color, -1);
cv::Point2f center_top_right(rect.x + rect.width - corner_radius, rect.y + corner_radius);
cv::Size2f size_top_right(corner_radius * 2 - i * 2, corner_radius * 2 - i * 2);
cv::ellipse(image, center_top_right, size_top_right, 270.0, 0.0, 90.0, corner_fill_color, -1);
cv::Point2f center_bottom_left(rect.x + corner_radius, rect.y + rect.height - corner_radius);
cv::Size2f size_bottom_left(corner_radius * 2 - i * 2, corner_radius * 2 - i * 2);
cv::ellipse(image, center_bottom_left, size_bottom_left, 90.0, 0.0, 90.0, corner_fill_color, -1);
cv::Point2f center_bottom_right(rect.x + rect.width - corner_radius, rect.y + rect.height - corner_radius);
cv::Size2f size_bottom_right(corner_radius * 2 - i * 2, corner_radius * 2 - i * 2);
cv::ellipse(image, center_bottom_right, size_bottom_right, 0.0, 0.0, 90.0, corner_fill_color, -1);
}
其中,fill_color表示填充颜色,center_x和center_y表示圆角矩形的中心点坐标,width和height表示圆角矩形的宽度和高度,radius表示圆角的半径。
cv::imshow("Filled Round Rect", image);
cv::waitKey(0);
完整的代码示例:
#include <opencv2/opencv.hpp>
int main() {
int width = 640;
int height = 480;
int center_x = width / 2;
int center_y = height / 2;
int radius = 50;
cv::Mat image(height, width, CV_8UC3, cv::Scalar(0, 0, 0));
cv::Scalar fill_color(255, 255, 255); // 填充颜色为白色
cv::Point2f center(center_x, center_y); // 圆角矩形的中心点坐标
cv::Size2f size(width, height); // 圆角矩形的宽度和高度
int corner_radius = radius; // 圆角的半径
cv::Rect rect(center - size / 2, center + size / 2);
cv::rectangle(image, rect, fill_color, -1);
cv::Scalar corner_fill_color(0, 0, 0); // 圆角填充颜色为黑色
cv::RNG rng;
for (int i = 0; i < corner_radius; ++i) {
cv::Point2f center_top_left(rect.x + corner_radius, rect.y + corner_radius);
cv::Size2f size_top_left(corner_radius * 2 - i * 2, corner_radius * 2 - i * 2);
cv::ellipse(image, center_top_left, size_top_left, 180.0, 0.0, 90.0, corner_fill_color, -1);
cv::Point2f center_top_right(rect.x + rect.width - corner_radius, rect.y + corner_radius);
cv::Size2f size_top_right(corner_radius * 2 - i * 2, corner_radius * 2 - i * 2);
cv::ellipse(image, center_top_right, size_top_right, 270.0, 0.0, 90.0, corner_fill_color, -1);
cv::Point2f center_bottom_left(rect.x + corner_radius, rect.y + rect.height - corner_radius);
cv::Size2f size_bottom_left(corner_radius * 2 - i * 2, corner_radius * 2 - i * 2);
cv::ellipse(image, center_bottom_left, size_bottom_left, 90.0, 0.0, 90.0, corner_fill_color, -1);
cv::Point2f center_bottom_right(rect.x + rect.width - corner_radius, rect.y + rect.height - corner_radius);
cv::Size2f size_bottom_right(corner_radius * 2 - i * 2, corner_radius * 2 - i * 2);
cv::ellipse(image, center_bottom_right, size_bottom_right, 0.0, 0.0, 90.0, corner_fill_color, -1);
}
cv::imshow("Filled Round Rect", image);
cv::waitKey(0);
return 0;
}
推荐的腾讯云相关产品:腾讯云智图图像处理服务,可以用于图像处理和计算视觉,链接地址:https://cloud.tencent.com/product/ivp
领取专属 10元无门槛券
手把手带您无忧上云