Python OpenCVにおける輪郭(領域)
輪郭: 初めの一歩 より import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('pic.png') #cv2.imshow('img',img) #cv2.waitKey(0) imgray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) #cv2.imshow('imgray',imgray) #cv2.waitKey(0) ret,thresh = cv2.threshold(imgray,127,255,0) #cv2.imshow('thresh',thresh) #cv2.waitKey(0) image,contous = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) #cv2.imshow('image',contous) #cv2.waitKey(0) img = cv2.drawContours(img,image,-1,(0,255,0),3) cv2.imshow('img',img) cv2.waitKey(0) cv2.destroyAllWindows() 動いたが、正直良くわからない。 ここで画像を取り込んで、グレイに変換 im = cv2.imread('test.jpg') imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) ここで2値化 ret,thresh = cv2.threshold(imgray,127,255,0) ここで輪郭抽出 image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) cv2.findContours() 関数は3個の引数を必要とする関数です.第1引数は入力画像,第2引数はcontour retrieval mode,第3引数は輪郭検出方法を指定するフラグです...