OpenCV Python 在图像上查找和绘制对象的极点
要在输入图像中查找和绘制对象的极点,可以按照以下步骤进行:
- 首先需要导入所需的库。在以下所有 Python 示例中,所需的 Python 库是 OpenCV 。确保您已经安装了它。
-
下一步是使用 cv2.imread() 函数读取输入图像。指定图像路径和图像类型(.jpg 或 .png)。将输入图像转换为灰度图像。
-
在灰度图像上应用阈值处理以创建二值图像。调整第二个参数以获得更好的轮廓检测结果。
ret,thresh = cv2.threshold(gray,150,255,0)
- 使用 cv2.findContours() 函数在图像中找到轮廓。
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
-
选择一个轮廓(比如第一个轮廓) cnt 从轮廓的列表中。或者遍历所有的轮廓。
-
找到最左、最右、最上和最下的极值点。
leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
topmost = tuple(cnt[cnt[:,:,1].argmin()][0])
bottommost = tuple(cnt[cnt[:,:,1].argmax()][0])
-
在图像上绘制这些极端点。要在图像上绘制一个点,我们可以绘制一个半径较小的填充圆。
-
在图像上显示绘制的轮廓和近似轮廓。
让我们通过一些Python示例来了解如何在图像中找到并绘制对象的极端点。
示例
以下是Python 3程序,展示了如何在图像中找到并绘制对象的极端点。
# import required libraries
import cv2
# load the input image
img = cv2.imread('four-point-star.png')
# convert the input image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# apply thresholding to convert grayscale to binary image
ret,thresh = cv2.threshold(gray,150,255,0)
# find the contours
contours,hierarchy = cv2.findContours(thresh,
cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
print("Number of contours detected:", len(contours))
# select the first contour
cnt = contours[0]
# find the extreme points
leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
topmost = tuple(cnt[cnt[:,:,1].argmin()][0])
bottommost = tuple(cnt[cnt[:,:,1].argmax()][0])
points = [leftmost, rightmost, topmost, bottommost]
# draw the points on th image
for point in points:
cv2.circle(img, point, 4, (0, 0, 255), -1)
# display the image with drawn extreme points
while True:
cv2.imshow("Extreme Points", img)
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
我们将使用以下图像作为该程序的输入文件:
输出
当我们执行上述代码时,它将产生以下输出:
Number of contours detected: 1
以下是我们得到的窗口显示的输出−
请注意,左侧最左、最右、最上和最下的四个极点都以红色绘制。
示例
在这个Python程序中,我们绘制了输入图像中对象的极点。
# import required libraries
import cv2
# load the input image
img = cv2.imread('two-shapes.png')
# convert the input image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# apply thresholding to convert grayscale to binary image
ret,thresh = cv2.threshold(gray,150,255,0)
# find the contours
contours,hierarchy = cv2.findContours(thresh,
cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
print("Number of contours detected:", len(contours))
# loop over all the contours
for cnt in contours:
# extreme points
leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
topmost = tuple(cnt[cnt[:,:,1].argmin()][0])
bottommost = tuple(cnt[cnt[:,:,1].argmax()][0])
points = [leftmost, rightmost, topmost, bottommost]
# draw the points on th image
for point in points:
cv2.circle(img, point, 4, (0, 0, 255), -1)
# display the image with drawn extreme points
while True:
cv2.imshow("Extreme Points", img)
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
我们将使用下面的图像作为此程序的输入文件。
Number of contours detected: 2
我们得到如下窗口显示输出-
注意,最左、最右、最上和最下的极点以红色绘制。在上述输出中,绘制了两种不同对象(形状)的极点。