在 OpenCV 中设置矩形样式
Set rectangle style in OpenCV
我想知道OpenCV有多少种画图检测样式。我想知道如何绘制此图中的矩形:
OpenCV 不提供样式。您只能绘制具有给定颜色、4/8 连接点或 anti-aliasing 点、给定厚度的矩形。
但是,您可以简单地绘制 8 条线来恢复矩形的坐标:
代码非常简单:
#include <opencv2/opencv.hpp>
using namespace cv;
void drawDetection(Mat3b& img, const Rect& r, Scalar color = Scalar(0,255,0), int thickness = 3)
{
int hor = r.width / 7;
int ver = r.height / 7;
// Top left corner
line(img, r.tl(), Point(r.x, r.y + ver), color, thickness);
line(img, r.tl(), Point(r.x + hor, r.y), color, thickness);
// Top right corner
line(img, Point(r.br().x - hor, r.y), Point(r.br().x, r.y), color, thickness);
line(img, Point(r.br().x, r.y + ver), Point(r.br().x, r.y), color, thickness);
// Bottom right corner
line(img, Point(r.br().x, r.br().y - ver), r.br(), color, thickness);
line(img, Point(r.br().x - hor, r.br().y), r.br(), color, thickness);
// Bottom left corner
line(img, Point(r.x, r.br().y - ver), Point(r.x, r.br().y), color, thickness);
line(img, Point(r.x + hor, r.br().y), Point(r.x, r.br().y), color, thickness);
}
int main()
{
// Load image
Mat3b img = imread("path_to_image");
// Your detection
Rect detection(180, 160, 220, 240);
// Custom draw
drawDetection(img, detection);
imshow("Detection", img);
waitKey();
return 0;
}
我想知道OpenCV有多少种画图检测样式。我想知道如何绘制此图中的矩形:
OpenCV 不提供样式。您只能绘制具有给定颜色、4/8 连接点或 anti-aliasing 点、给定厚度的矩形。
但是,您可以简单地绘制 8 条线来恢复矩形的坐标:
代码非常简单:
#include <opencv2/opencv.hpp>
using namespace cv;
void drawDetection(Mat3b& img, const Rect& r, Scalar color = Scalar(0,255,0), int thickness = 3)
{
int hor = r.width / 7;
int ver = r.height / 7;
// Top left corner
line(img, r.tl(), Point(r.x, r.y + ver), color, thickness);
line(img, r.tl(), Point(r.x + hor, r.y), color, thickness);
// Top right corner
line(img, Point(r.br().x - hor, r.y), Point(r.br().x, r.y), color, thickness);
line(img, Point(r.br().x, r.y + ver), Point(r.br().x, r.y), color, thickness);
// Bottom right corner
line(img, Point(r.br().x, r.br().y - ver), r.br(), color, thickness);
line(img, Point(r.br().x - hor, r.br().y), r.br(), color, thickness);
// Bottom left corner
line(img, Point(r.x, r.br().y - ver), Point(r.x, r.br().y), color, thickness);
line(img, Point(r.x + hor, r.br().y), Point(r.x, r.br().y), color, thickness);
}
int main()
{
// Load image
Mat3b img = imread("path_to_image");
// Your detection
Rect detection(180, 160, 220, 240);
// Custom draw
drawDetection(img, detection);
imshow("Detection", img);
waitKey();
return 0;
}