【发布时间】:2021-02-19 08:24:02
【问题描述】:
我想预测图像中的矩形并仅使用 opencv python 以框形绘制矩形。我使用下面的代码来预测和绘制矩形,但它不能正常工作。
import numpy as np
import cv2
from PIL import Image
import sys
Path='D:\Artificial intelligence\Phyton'
filename='Test.png'
# Load image, grayscale, Gaussian blur, and Otsu's threshold
image = cv2.imread('D:\Artificial intelligence\Phyton\Img21122020113231AM.Jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 190, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Find contours and sort using contour area
cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
for c in cnts:
# Highlight largest contour
cv2.drawContours(image, [c], -1, (36,255,12), 3)
break
cv2.imwrite(filename+'_Processingimage_color.jpg', image)
我的输入图像:
我的结果:
【问题讨论】:
-
您不是在预测,而是在检测轮廓。那是不同的。另外,您应该先对图像进行一些过滤。
-
“Predicting” 是利用对先前位置(可能还有习惯/行为)的了解来提前说出对象将来可能移动到的位置,即注意到一个球在视频的前 5 帧中向右移动了 10 个像素,以预测它可能在下一帧中向右移动 10 个像素。
标签: python opencv rectangles