代码之家  ›  专栏  ›  技术社区  ›  Strahinja

opencv python调整图像大小

  •  1
  • Strahinja  · 技术社区  · 8 年前

    我正在尝试将轮廓调整为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)
    

    我得到的错误是:

    错误: C:\项目\opencv python\opencv\modules\imgproc\src\resize.cpp:4044: 错误:(-215)ssize.width>0&ssize.height>0在函数中 简历:调整尺寸

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

    有什么建议吗?

    2 回复  |  直到 8 年前
        1
  •  0
  •   Strahinja    8 年前

    问题是我试图得到负面的协调画面,所以无法得到。我只是扩大了范围 if pt1+leng > width or pt2+leng > height or pt2 < 0 or pt1 < 0: 而且有效。

        2
  •  -1
  •   Thimira Amaratunga    8 年前

    尝试在调整大小之前创建一个roi副本,

    roiImg = roi.copy()
    roiImg = cv2.resize(roiImg, (28, 28), cv2.INTER_AREA)
    
    推荐文章