私はopenCvとpythonを使用しており、構造分析と形状記述子を扱っています。私はこのブログを見つけました:http: //opencvpython.blogspot.it/2012/06/contours-2-brotherhood.html は非常に役に立ち、白黒の画像で外接する四角形を描画しようとしましたが、うまくいきました。しかし、今では画像から、たとえば黄色を抽出し、その上に外接する四角形を描きたいと思います。問題は、白黒の画像が均一ではなく、ノイズがあり、コードが形状全体を認識していないことです。
そして、これはコードです:
import numpy as np
import cv2
im = cv2.imread('shot.bmp')
hsv_img = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
COLOR_MIN = np.array([20, 80, 80],np.uint8)
COLOR_MAX = np.array([40, 255, 255],np.uint8)
frame_threshed = cv2.inRange(hsv_img, COLOR_MIN, COLOR_MAX)
imgray = frame_threshed
ret,thresh = cv2.threshold(frame_threshed,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cnt=contours[0]
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow("Show",im)
cv2.waitKey()
cv2.destroyAllWindows()