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

Let*_*rIt 5 android opencv image-processing object-detection

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

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

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

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

图像

在此输入图像描述

Sar*_*wal 5

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
Run Code Online (Sandbox Code Playgroud)

这是假设点 (0,0) 出现在左上角。现在您可以使用:

rectangle(src, Point(x1,y1), Point(x2,y2),Color,Thickness,other_params);
Run Code Online (Sandbox Code Playgroud)