OpenCV-Python Tutorials (7) ~Image Processing in OpenCV~
OpenCV-Python Tutorialsã®è¨äºï¼Image Processing in OpenCV ã®ç« ã® Contours in OpenCV ãè¦ã¦ããï¼ ä»åã¯ï¼Contours : Contour Properties ã®å 容ã®ã¡ã¢ï¼
å ¬å¼ï¼Image Processing in OpenCV — OpenCV-Python Tutorials 1 documentation
ãã®ä¸ã§è©¦ããã³ã¼ãã¯GitHubã«ç½®ãã¦ãããã¨ã«ï¼
輪éã®ããããã£
ããã§ã¯ï¼é »åºããSolidity, Equivalent Diameter, Mask image, Mean Intensityãªã©ã®ããããã£ãå¦ã¶ï¼ä»ã®ç¹å¾´ã«ã¤ãã¦ã¯ï¼Matlab regionprops documentationãè¦ãããããï¼
1. Aspect Ratio
ããã¯ï¼å¯¾è±¡ç©ãå²ãåè§å½¢ã®æ¨ªã¨ç¸¦ã®æ¯ã ï¼
x, y, w, h = cv2.boundingRect(cnt)
aspect_ratio = float(w)/h
2. Extent
Extentã¯è¼ªéã§å²ã¾ããé åã®é¢ç©ã¨å¤æ¥é·æ¹å½¢ã®é¢ç©ã®æ¯ã ï¼
area = cv2.contourArea(cnt)
x, y, w, h = cv2.boundingRect(cnt)
rect_area = w*h
extent = float(area)/rect_area
3. Solidity
Solidityã¯è¼ªéã§å²ã¾ããé åã®é¢ç©ã¨å¸å ã®é¢ç©ã®æ¯ã ï¼
area = cv2.contourArea(cnt) hull = cv2.convexHull(cnt) hull_area = cv2.contourArea(hull) solidity = float(area)/hull_area print("solidity:" + str(solidity))
4. Equivalent Diameter
Equivalent Diameterã¯è¼ªéã§å²ã¾ããé åã¨åãé¢ç©ãæã¤åã®ç´å¾ã§ããï¼
area = cv2.contourArea(cnt) equi_diameter = np.sqrt(4*area/np.pi) print("equi_diameter:" + str(equi_diameter))
5. Orientation
Orientationã¯å¯¾è±¡ç©ãåãã¦ããæ¹åã ï¼æ¬¡ã®ææ³ã¯é·è»¸ã¨ç軸ã®é·ããä¸ãã¦ãããï¼
(x,y),(MA,ma),angle = cv2.fitEllipse(cnt)
6. Mask and Pixel Points
ããå ´åã§ã¯ï¼å¯¾è±¡ç©ãæ§æããå ¨ã¦ã®ç¹ãå¿ è¦ã¨ãªããããããªãï¼ããã¯æ¬¡ã®ããã«ãã¦ã§ããï¼
mask = np.zeros(imgray.shape, np.uint8) cv2.drawContours(mask, [cnt], 0, 255, -1) pixelpoints = np.transpose(np.nonzero(mask)) #pixelpoints = cv2.findNonZero(mask)
ããã§ã¯ï¼2ã¤ã®ææ³ãããï¼1ã¤ã¯Numpyã®é¢æ°ã使ããã®ï¼ãã1ã¤ã¯OpenCVã®é¢æ°ã使ããã®ï¼æå¾ã®è¡ã®ãã®ï¼ã§ï¼åããã¨ãããããã«ãªã£ã¦ããï¼çµæã¯ã»ã¼åãã ãï¼å°ãéããæãï¼Numpyã¯(row, column)ã®æ¹å¼ã§ã®åº§æ¨ã§ï¼ä¸æ¹OpenCVã¯(x,y)ã®æ¹å¼ã§ã®åº§æ¨ã ï¼ãã£ã¦åºæ¬çã«çãã¯äº¤æå¯è½ã§ããï¼row = x, column = yã§ãããã¨ã«æ³¨æãã¦ãããï¼
7. Maximum Value, Minimum Value and their locations
ãããã®ãã©ã¡ã¼ã¿ã¼ã¯maskç»åãç¨ãã¦æ±ãããã¨ãã§ããï¼
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(imgray,mask = mask)
8. Mean Color or Mean Intensity
ããã§ã¯å¯¾è±¡ç©ã®è²ã®å¹³åãæ±ããï¼ã¾ãï¼ã°ã¬ã¼ã¹ã±ã¼ã«ã®ã¢ã¼ãã§ã¯å¯¾è±¡ç©ã®å¼·åº¦ã®å¹³åã¨ãªãï¼ããã§ãmaskã使ã£ã¦è¡ãï¼
mean_val = cv2.mean(im,mask = mask)
9. Extreme Points
Extreme Pointsã¨ã¯ï¼å¯¾è±¡ç©ã®æä¸ç¹ï¼æä¸ç¹ï¼æå³ç¹ï¼æå·¦ç¹ã®ãã¨ã§ããï¼
leftmost = tuple(cnt[cnt[:,:,0].argmin()][0]) rightmost = tuple(cnt[cnt[:,:,0].argmax()][0]) topmost = tuple(cnt[cnt[:,:,1].argmin()][0]) bottommost = tuple(cnt[cnt[:,:,1].argmax()][0])
ç·´ç¿
ä»ã«ãããã¤ãã®ç¹å¾´éãmatlabã«ããï¼å®è¡ãã¦è©¦ãã¦ã¿ããï¼