Skip to content

Instantly share code, notes, and snippets.

@Gotoryoo
Created December 1, 2016 09:44
Show Gist options
  • Save Gotoryoo/a035d8cabd13ca381094512d6cb7af43 to your computer and use it in GitHub Desktop.
Save Gotoryoo/a035d8cabd13ca381094512d6cb7af43 to your computer and use it in GitHub Desktop.
基本的な画像処理
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 15 11:13:08 2016
@author: GTR
"""
import cv2
path = "C:\\Users\\GTR\\Documents\\program\\wakutarou\\waku_cut.png"
img = cv2.imread(path,cv2.CV_LOAD_IMAGE_GRAYSCALE)
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("img_gray.png",img)
min = 0
max = 255
img_cont = cv2.normalize(img, alpha=min, beta=max, norm_type=cv2.NORM_MINMAX)
cv2.imshow("img_cont", img_cont)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("img_cont.png",img_cont)
kernel1 = 3
kernel2 = 51
gau_1 = cv2.GaussianBlur(img_cont, (kernel1,kernel1), -1)
gau_2 = cv2.GaussianBlur(gau_1, (kernel2,kernel2), -1)
sub = cv2.subtract(gau_2, gau_1)
cv2.imshow("gau_1", gau_1)
cv2.imshow("gau_2", gau_2)
cv2.imshow("sub", sub)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("gau_1.png",gau_1)
cv2.imwrite("gau_2.png",gau_2)
cv2.imwrite("sub.png",sub)
threshold = 70
max_bri = 255
ret,thre = cv2.threshold(sub, threshold, max_bri, cv2.THRESH_BINARY)
cv2.imshow("thre", thre)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("thre.png",thre)
erosion =cv2.erode(thre, (19,19), iterations = 1)
cv2.imshow("erosion", erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("erosion.png",erosion)
contours, hierarchy = cv2.findContours(erosion,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for i in range(len(contours)):
cnt = contours[i]
area = cv2.contourArea(cnt)
print area
#cnt = contours[0]
#area = cv2.contourArea(cnt)
#cv2.contourArea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment