Python Opencv:如何在图像上绘制具有四个角的不完整矩形
在网络中搜索之后,我找不到一种方法来使用Python中的OpenCV绘制图像中的边界框。它有两个特征:前四个角彼此不连接,第二个角是透明边界框Python Opencv:如何在图像上绘制具有四个角的不完整矩形,python,opencv,draw,Python,Opencv,Draw,在网络中搜索之后,我找不到一种方法来使用Python中的OpenCV绘制图像中的边界框。它有两个特征:前四个角彼此不连接,第二个角是透明边界框 我知道我应该使用多边形,但我不能再深入到这一点。试试这段代码。可能对你有帮助 代码: import matplotlib.pyplot as plt import matplotlib.patches as patches from PIL import Image import numpy as np image = np.array(Image.o
我知道我应该使用多边形,但我不能再深入到这一点。试试这段代码。可能对你有帮助 代码:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
import numpy as np
image = np.array(Image.open('Image.jpg'), dtype=np.uint8)
# Create figure and axes
fig,ax = plt.subplots(1)
# Display the image
ax.imshow(image)
# Create a Rectangle patch
rect = patches.Rectangle((102,55),160,162,linewidth=1,edgecolor='r',facecolor='none')
# Add the patch to the Axes
ax.add_patch(rect)
plt.show()
def draw_border(img, point1, point2, point3, point4, line_length):
x1, y1 = point1
x2, y2 = point2
x3, y3 = point3
x4, y4 = point4
cv2.circle(img, (x1, y1), 3, (255, 0, 255), -1) #-- top_left
cv2.circle(img, (x2, y2), 3, (255, 0, 255), -1) #-- bottom-left
cv2.circle(img, (x3, y3), 3, (255, 0, 255), -1) #-- top-right
cv2.circle(img, (x4, y4), 3, (255, 0, 255), -1) #-- bottom-right
cv2.line(img, (x1, y1), (x1 , y1 + line_length), (0, 255, 0), 2) #-- top-left
cv2.line(img, (x1, y1), (x1 + line_length , y1), (0, 255, 0), 2)
cv2.line(img, (x2, y2), (x2 , y2 - line_length), (0, 255, 0), 2) #-- bottom-left
cv2.line(img, (x2, y2), (x2 + line_length , y2), (0, 255, 0), 2)
cv2.line(img, (x3, y3), (x3 - line_length, y3), (0, 255, 0), 2) #-- top-right
cv2.line(img, (x3, y3), (x3, y3 + line_length), (0, 255, 0), 2)
cv2.line(img, (x4, y4), (x4 , y4 - line_length), (0, 255, 0), 2) #-- bottom-right
cv2.line(img, (x4, y4), (x4 - line_length , y4), (0, 255, 0), 2)
return img
line_length = 15
img = np.zeros((512,512,3), np.uint8)
cv2.imshow('img', img)
point1, point2, point3, point4 = (280,330), (280,390), (340,330), (340,390)
fin_img = draw_border(img, point1, point2, point3, point4, line_length)
cv2.imshow('fin_img', fin_img)
cv2.waitKey()
cv2.destroyAllWindows()
图像输出:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
import numpy as np
image = np.array(Image.open('Image.jpg'), dtype=np.uint8)
# Create figure and axes
fig,ax = plt.subplots(1)
# Display the image
ax.imshow(image)
# Create a Rectangle patch
rect = patches.Rectangle((102,55),160,162,linewidth=1,edgecolor='r',facecolor='none')
# Add the patch to the Axes
ax.add_patch(rect)
plt.show()
def draw_border(img, point1, point2, point3, point4, line_length):
x1, y1 = point1
x2, y2 = point2
x3, y3 = point3
x4, y4 = point4
cv2.circle(img, (x1, y1), 3, (255, 0, 255), -1) #-- top_left
cv2.circle(img, (x2, y2), 3, (255, 0, 255), -1) #-- bottom-left
cv2.circle(img, (x3, y3), 3, (255, 0, 255), -1) #-- top-right
cv2.circle(img, (x4, y4), 3, (255, 0, 255), -1) #-- bottom-right
cv2.line(img, (x1, y1), (x1 , y1 + line_length), (0, 255, 0), 2) #-- top-left
cv2.line(img, (x1, y1), (x1 + line_length , y1), (0, 255, 0), 2)
cv2.line(img, (x2, y2), (x2 , y2 - line_length), (0, 255, 0), 2) #-- bottom-left
cv2.line(img, (x2, y2), (x2 + line_length , y2), (0, 255, 0), 2)
cv2.line(img, (x3, y3), (x3 - line_length, y3), (0, 255, 0), 2) #-- top-right
cv2.line(img, (x3, y3), (x3, y3 + line_length), (0, 255, 0), 2)
cv2.line(img, (x4, y4), (x4 , y4 - line_length), (0, 255, 0), 2) #-- bottom-right
cv2.line(img, (x4, y4), (x4 - line_length , y4), (0, 255, 0), 2)
return img
line_length = 15
img = np.zeros((512,512,3), np.uint8)
cv2.imshow('img', img)
point1, point2, point3, point4 = (280,330), (280,390), (340,330), (340,390)
fin_img = draw_border(img, point1, point2, point3, point4, line_length)
cv2.imshow('fin_img', fin_img)
cv2.waitKey()
cv2.destroyAllWindows()
以下函数在感兴趣区域周围绘制一个不完整的矩形。对于提供的每个点,我使用了两次
cv2.line()。此外,我还利用cv2.circle()
标记了4个点
有一个限制。您必须按以下顺序提供4点:左上、左下、右上、右下
另外,还有一个选项,可以改变要绘制的线的长度线的长度
代码:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
import numpy as np
image = np.array(Image.open('Image.jpg'), dtype=np.uint8)
# Create figure and axes
fig,ax = plt.subplots(1)
# Display the image
ax.imshow(image)
# Create a Rectangle patch
rect = patches.Rectangle((102,55),160,162,linewidth=1,edgecolor='r',facecolor='none')
# Add the patch to the Axes
ax.add_patch(rect)
plt.show()
def draw_border(img, point1, point2, point3, point4, line_length):
x1, y1 = point1
x2, y2 = point2
x3, y3 = point3
x4, y4 = point4
cv2.circle(img, (x1, y1), 3, (255, 0, 255), -1) #-- top_left
cv2.circle(img, (x2, y2), 3, (255, 0, 255), -1) #-- bottom-left
cv2.circle(img, (x3, y3), 3, (255, 0, 255), -1) #-- top-right
cv2.circle(img, (x4, y4), 3, (255, 0, 255), -1) #-- bottom-right
cv2.line(img, (x1, y1), (x1 , y1 + line_length), (0, 255, 0), 2) #-- top-left
cv2.line(img, (x1, y1), (x1 + line_length , y1), (0, 255, 0), 2)
cv2.line(img, (x2, y2), (x2 , y2 - line_length), (0, 255, 0), 2) #-- bottom-left
cv2.line(img, (x2, y2), (x2 + line_length , y2), (0, 255, 0), 2)
cv2.line(img, (x3, y3), (x3 - line_length, y3), (0, 255, 0), 2) #-- top-right
cv2.line(img, (x3, y3), (x3, y3 + line_length), (0, 255, 0), 2)
cv2.line(img, (x4, y4), (x4 , y4 - line_length), (0, 255, 0), 2) #-- bottom-right
cv2.line(img, (x4, y4), (x4 - line_length , y4), (0, 255, 0), 2)
return img
line_length = 15
img = np.zeros((512,512,3), np.uint8)
cv2.imshow('img', img)
point1, point2, point3, point4 = (280,330), (280,390), (340,330), (340,390)
fin_img = draw_border(img, point1, point2, point3, point4, line_length)
cv2.imshow('fin_img', fin_img)
cv2.waitKey()
cv2.destroyAllWindows()
结果:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
import numpy as np
image = np.array(Image.open('Image.jpg'), dtype=np.uint8)
# Create figure and axes
fig,ax = plt.subplots(1)
# Display the image
ax.imshow(image)
# Create a Rectangle patch
rect = patches.Rectangle((102,55),160,162,linewidth=1,edgecolor='r',facecolor='none')
# Add the patch to the Axes
ax.add_patch(rect)
plt.show()
def draw_border(img, point1, point2, point3, point4, line_length):
x1, y1 = point1
x2, y2 = point2
x3, y3 = point3
x4, y4 = point4
cv2.circle(img, (x1, y1), 3, (255, 0, 255), -1) #-- top_left
cv2.circle(img, (x2, y2), 3, (255, 0, 255), -1) #-- bottom-left
cv2.circle(img, (x3, y3), 3, (255, 0, 255), -1) #-- top-right
cv2.circle(img, (x4, y4), 3, (255, 0, 255), -1) #-- bottom-right
cv2.line(img, (x1, y1), (x1 , y1 + line_length), (0, 255, 0), 2) #-- top-left
cv2.line(img, (x1, y1), (x1 + line_length , y1), (0, 255, 0), 2)
cv2.line(img, (x2, y2), (x2 , y2 - line_length), (0, 255, 0), 2) #-- bottom-left
cv2.line(img, (x2, y2), (x2 + line_length , y2), (0, 255, 0), 2)
cv2.line(img, (x3, y3), (x3 - line_length, y3), (0, 255, 0), 2) #-- top-right
cv2.line(img, (x3, y3), (x3, y3 + line_length), (0, 255, 0), 2)
cv2.line(img, (x4, y4), (x4 , y4 - line_length), (0, 255, 0), 2) #-- bottom-right
cv2.line(img, (x4, y4), (x4 - line_length , y4), (0, 255, 0), 2)
return img
line_length = 15
img = np.zeros((512,512,3), np.uint8)
cv2.imshow('img', img)
point1, point2, point3, point4 = (280,330), (280,390), (340,330), (340,390)
fin_img = draw_border(img, point1, point2, point3, point4, line_length)
cv2.imshow('fin_img', fin_img)
cv2.waitKey()
cv2.destroyAllWindows()
如果有人需要,也可以选择:C中带有弯曲拐角的精美矩形:
然而,我需要python中的这段代码,社区对此进行了回复
void draw_border(IplImage* show_img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness, int r, int d):
// Top left
cvLine(show_img, cvPoint( pt1.x +r, pt1.y), cvPoint( pt1.x + r + d, pt1.y), color, thickness, 8, 0 );
cvLine(show_img, cvPoint( pt1.x, pt1.y + r), cvPoint( pt1.x, pt1.y + r + d), color, thickness, 8, 0 );
cvEllipse(show_img, cvPoint(pt1.x +r, pt1.y + r), cvSize( r, r ), 180, 0, 90, color, thickness, 8, 0);
// Top right
cvLine(show_img, cvPoint( pt2.x - r, pt1.y), cvPoint( pt2.x - r - d, pt1.y), color, thickness, 8, 0 );
cvLine(show_img, cvPoint( pt2.x, pt1.y + r), cvPoint( pt2.x, pt1.y + r + d), color, thickness, 8, 0 );
cvEllipse(show_img, cvPoint(pt2.x -r, pt1.y + r), cvSize( r, r ), 270, 0, 90, color, thickness, 8, 0);
// Bottom left
cvLine(show_img, cvPoint( pt1.x + r, pt2.y), cvPoint( pt1.x + r + d, pt2.y), color, thickness, 8, 0);
cvLine(show_img, cvPoint( pt1.x, pt2.y - r), cvPoint( pt1.x, pt2.y - r - d), color, thickness, 8, 0);
cvEllipse(show_img, cvPoint(pt1.x + r, pt2.y - r), cvSize( r, r ), 90, 0, 90, color, thickness, 8, 0);
// Bottom right
cvLine(show_img, cvPoint( pt2.x - r, pt2.y), cvPoint( pt2.x - r - d, pt2.y), color, thickness, 8, 0);
cvLine(show_img, cvPoint( pt2.x, pt2.y - r), cvPoint( pt2.x, pt2.y - r - d), color, thickness, 8, 0);
cvEllipse(show_img, cvPoint(pt2.x - r, pt2.y - r), cvSize( r, r ), 0, 0, 90, color, thickness, 8, 0);
你能识别出4个角吗?画8条线和一个矩形怎么样?请查看接受的回答。这是我需要的。谢谢穆罕默德,但这不是我需要的。请检查接受的答复。@MajidAzimi Cjeck out用于绘制带曲线边的矩形