2017-05-19 24 views
0

我已經搜索了很多,但是這個錯誤主要是當圖像很大時。我的圖片不是很大,我仍然收到這個錯誤。我試圖通過openCV識別手寫數字。我檢查過我的圖像是否正在加載,所以這不是問題。這是我的代碼。小圖像OpenCV ssize.area()> 0錯誤

im = cv2.imread(filename,1) 
    # Convert to grayscale and apply Gaussian filtering 
    # Convert image from one color space to another 
    im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) 
    im_gray = cv2.GaussianBlur(im_gray, (5, 5), 0) 
    # Threshold to binary the image 
    ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV) 
    # Find contours in the image 
    _, ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 
    # Get rectangles contains each contour 
    rects = [cv2.boundingRect(ctr) for ctr in ctrs] 

    # For each rectangular region, calculate HOG features and predict 
    # the digit using Linear SVM. 
    for rect in rects: 
     # Draw the rectangles 
     cv2.rectangle(im, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 2) 
     # 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) 
     roi = im_th[pt1:pt1+leng, pt2:pt2+leng] 
     # Resize the image 
     roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA) 
     roi = cv2.dilate(roi, (3, 3)) 
     # Calculate the HOG features 
     roi_hog_fd = hog(roi, orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False) 
     nbr = clf.predict(np.array([roi_hog_fd], 'float64')) 

我對這兩條線有疑問,他們是從錯誤出現的地方。

ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV) 

roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA) 

它也不接受油漆圖象。任何幫助,將不勝感激。

回答

1

我的猜測是,每rect接近你的圖像的邊界,你的pt1pt2計算給出,因爲放大ROI(我的圖片綠盒)的結果。

ROI out of image bounds

的話,我會建議檢查pt1pt2是否是積極的。如果不是,我會將它們設置爲0並相應地重新計算ROI的大小。

相關問題