gpt4 book ai didi

opencv - 在OpenCV中设置矩形样式

转载 作者:行者123 更新时间:2023-12-02 17:46:00 24 4
gpt4 key购买 nike

我想知道OpenCV有多少种样式用于图形检测。我希望知道如何绘制此图像中的矩形:

enter image description here

最佳答案

OpenCV不提供样式。您只能绘制具有给定颜色,连接了4/8或具有给定厚度的抗锯齿点的矩形。

但是,您可以简单地绘制8条线以从矩形中恢复坐标:

enter image description here

该代码非常简单:

#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中设置矩形样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35929383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com