Python3を勉強している初学者になります。
現在、画像のA4用紙のみを囲いたいのですが、下記画像のような表示なってしまい
囲うことができないため、ご教示いただけると幸いです。
Python3
1import cv2 2import numpy as np 3 4 5img = cv2.imread("****",1) 6height = img.shape[0] 7width = img.shape[1] 8img = cv2.resize(img , (int(width*0.5), int(height*0.5))) 9 10# グレースケールに変換する。 11grray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 13# 2値化する 14ret, bin_img = cv2.threshold(grray, 155, 255, cv2.THRESH_BINARY) 15cv2.imshow("img",bin_img) 16 17# 輪郭を抽出する。 18contours, hierarchy = cv2.findContours( 19 bin_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE 20) 21 22 # 面積の大きいもののみ選別 23areas = [] 24for cnt in contours: 25 area = cv2.contourArea(cnt) 26 if area > 10000: 27 epsilon = 0.1*cv2.arcLength(cnt,True) 28 approx = cv2.approxPolyDP(cnt,epsilon,True) 29 areas.append(approx) 30 31 32# 小さい輪郭は誤検出として削除する 33#contours = list(filter(lambda x: cv2.contourArea(x) > 100, contours)) 34 35# 輪郭を描画する。 36cv2.drawContours(img,areas, -1, color=(0, 0, 255), thickness=3) 37#cv2.drawContours(img,contours,-1,(0,255,0),3) 38 39cv2.imshow("Image",img) 40cv2.waitKey(0) 41cv2.destroyAllWindows()