python - OpenCV Python调整大小图像

标签 python python-3.x opencv computer-vision opencv3.0

我正在尝试将轮廓尺寸调整为28x28像素,并将其通过我的模型以检测数字并显示它。
但是,我对cv2.resize()函数有问题。
这是我的代码:

ret, frame = cap.read()

if ret == False:
    continue
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 50, 200, 255)

# Threshold the image
threshold = cv2.adaptiveThreshold(edged, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 4)

# Find contours in the image
_, contours, _= cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)


# ******************************************************************* #
# Get rectangles contains each contour
rects = [cv2.boundingRect(ctr) for ctr in contours]

for rect in rects:
    # Draw the rectangles
    cv2.rectangle(frame, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3) 

    # Make the rectangular region around the digit
    leng = int(rect[3] * 1.6)
    pt1 = int(rect[1] + rect[3] // 2 - leng // 2)
    pt2 = int(rect[0] + rect[2] // 2 - leng // 2)

    height, width = frame.shape[:2]

    if pt1+leng > width or pt2+leng > height:
        continue;

    roi = frame[pt1:pt1+leng, pt2:pt2+leng]

    # Resize the image
    roi = cv2.resize(roi, (28, 28), cv2.INTER_AREA)
    roi = cv2.dilate(roi, (3, 3))

    #nbr = clf.predict(np.array([roi_hog_fd], 'float64'))
    #cv2.putText(frame, str(int(nbr)), (rect[0], rect[1]),cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255), 3)
    cv2.putText(frame, "2", (rect[0], rect[1]),cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255), 3)

我得到的错误是:

error: C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:4044: error: (-215) ssize.width > 0 && ssize.height > 0 in function cv::resize



调整视频原始帧的大小(之前未应用任何其他cv2功能)效果很好。

有什么建议么?

最佳答案

问题是我试图获取负坐标图像,所以无法获取。我只是扩展了条件if pt1+leng > width or pt2+leng > height or pt2 < 0 or pt1 < 0:,它可以工作。

关于python - OpenCV Python调整大小图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50797435/

相关文章:

python - Opencv python HoughLinesP 奇怪的结果

python - 在 NumPy 中创建随机 float 和 float64 数组

Python,BeautifulSoup - 仅打印内容中包含 <img> 的链接

python - 生成一个正则表达式可以在 Python 中匹配的值列表

python - 使用 Python 连接到 Redis 集群(在 Docker 中)

python - 如何覆盖我的模板而不是 django 管理面板来重置密码?

无限生成器的Python产品

opencv - opencv hog.cpp中的 Gamma 校正

c++ - 如何找到连接一组点的平滑曲线(在 Open CV 中)?

python - 再次循环导入