python - OpenCV Python调整图像大小
问题描述
我正在尝试将轮廓调整为 28x28 像素并将其传递给我的模型以检测数字并显示它。但是,我有一个问题 function 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)
我得到的错误是:
错误:C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:4044:错误:(-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:
并且它有效。
推荐阅读
- android - 如何设计可水平滚动的自定义视图
- html - 通过 Sahi Pro 发送时,纯 HTML/CSS 垂直字符不会出现在电子邮件内容中
- python - 使用用户输入在 Django 中创建文件
- directory - 使用 SAP BODS 脚本创建目录?
- python - 在python中创建字符串模式
- matlab - MATLAB 中的 KD-Tree 在地理数据集上出错
- python - 使用“表格视图”向表格添加按钮
- javascript - 搜索多个约束的对象数组(两个属性)
- javascript - 如果对象在JS中的数组中,则从数组中推送或删除对象
- android - 我如何知道用户在应用选择器中点击了哪些应用或按钮?