tata色々な備忘録

データ解析、画像解析、化学分析などなど

python+Opencvによる画像処理10(輪郭検出)

import cv2
import numpy as np
import pylab as plt

im = cv2.imread("cat2.jpg")
im2= cv2.imread("cat2.jpg")

#グレースケール
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

#ガウスフィルタ
im_gray_smooth=cv2.GaussianBlur(im_gray,(11,11),0)

#2値化
ret,th1 = cv2.threshold(im_gray_smooth,130,255,cv2.THRESH_BINARY)

#輪郭検出
contours, hierarchy = cv2.findContours(th1,cv2.RETR_TREE,\
                                       cv2.CHAIN_APPROX_SIMPLE)
#輪郭描画
cv2.drawContours(im,contours,-1,(0,255,0),3)

#描画
plt.subplot(2,2,1),plt.imshow(im2,'gray')
plt.title('input image')
plt.subplot(2,2,2),plt.imshow(im,'gray')
plt.title('output image')
plt.subplot(2,2,3),plt.imshow(im_gray_smooth,'gray')
plt.title(u'グレースケール+ガウスフィルタ')
plt.subplot(2,2,4),plt.imshow(th1,'gray')
plt.title(u'2値化')
 
plt.show()

f:id:tatabox2000:20130721170741p:plain

cv2.adaptiveThresholdは細部まで行き届いている分、 輪郭検出には向いてなかった。パラメータの見直し等必要か。