Opencv 如何通过指定矩形的4个角来绘制矩形

Opencv 如何通过指定矩形的4个角来绘制矩形,opencv,image-processing,object-detection,opencv4android,Opencv,Image Processing,Object Detection,Opencv4android,我使用的是OpenCV4Android版本2.4.11。我正在从相机读取帧,我检测到帧中有任何矩形。然后,我尝试在检测到的对象周围绘制一个半透明的矩形 我想做的是,画一个半透明的矩形,给定检测到的物体的四个角。但是,在openCV中,您可以通过只指定矩形的两个点“左上角和右下角”来绘制矩形 请让我知道如何通过指定矩形的四个角来绘制矩形,而不仅仅是指定左上角和右下角 下面发布的图片是为了向您展示我的尝试,并向您展示我想要的是在检测到的四个角“红、绿、蓝、白”周围绘制一个矩形 图像: OpenCV不

我使用的是OpenCV4Android版本2.4.11。我正在从相机读取帧,我检测到帧中有任何矩形。然后,我尝试在检测到的对象周围绘制一个半透明的矩形

我想做的是,画一个半透明的矩形,给定检测到的物体的四个角。但是,在openCV中,您可以通过只指定矩形的两个点“左上角和右下角”来绘制矩形

请让我知道如何通过指定矩形的四个角来绘制矩形,而不仅仅是指定左上角和右下角

下面发布的图片是为了向您展示我的尝试,并向您展示我想要的是在检测到的四个角“红、绿、蓝、白”周围绘制一个矩形

图像


OpenCV不提供矩形绘制功能,但您可以使用已计算的4个点生成左上角和右下角点:

假设您的4个点是-
(tlx,tly),(trx,try),(blx,bly)
(brx,bry)
,其中tl是左上角,br是右下角

然后你可以计算:

x1=min(tlx,trx,brx,blx);//top-left pt. is the leftmost of the 4 points
x2=max(tlx,trx,brx,blx);//bottom-right pt. is the rightmost of the 4 points
y1=min(tly,try,bry,bly);//top-left pt. is the uppermost of the 4 points
y2=max(tly,try,bry,bly);//bottom-right pt. is the lowermost of the 4 points
这是假设点(0,0)出现在左上角。 现在您可以使用:

rectangle(src, Point(x1,y1), Point(x2,y2),Color,Thickness,other_params);

与@Saransh的想法相同,但为我编写:

auto x1 = std::min(tlx, std::min(trx, std::min(brx, blx))); // top-left pt. is the leftmost of the 4 points
auto x2 = std::max(tlx, std::max(trx, std::max(brx, blx))); // bottom-right pt. is the rightmost of the 4 points
auto y1 = std::min(tly, std::min(try, std::min(bry, bly))); //top-left pt. is the uppermost of the 4 points
auto y2 = std::max(tly, std::max(try, std::max(bry, bly))); //bottom-right pt. is the lowermost of the 4 points

您必须绘制一个指定四个点的多边形。看一看,有一节是关于绘画的polygons@Klaus你能提供一个如何画多边形的例子吗?我已经在谷歌上搜索过了,但对于opencv的java API,我需要提供一个列表,其中列出了一些要点,而我只有一个重点。我无法找出您的问题,但是,这是一个类似问题的链接。看问题而不是答案,区别在于答案中他用了一些我认为你不需要的观点进行了翻译。