Created
February 23, 2013 14:59
-
-
Save kimihito/5020056 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
import cv | |
import numpy | |
import scipy.misc | |
image_name = 'hinomaru.jpg' | |
gray_image_name = 'gray.jpg' | |
binarized_image_name = 'binarized.jpg' | |
contours_image_name = 'contours.jpg' | |
edged_image_name = 'edge.jpg' | |
THREASHOLD = 120 | |
MAX = 255 | |
def grayScale(imgfile): | |
img = cv.LoadImage(imgfile, cv.CV_LOAD_IMAGE_COLOR) | |
grayscaled_img = cv.CreateImage(cv.GetSize(img),cv.IPL_DEPTH_8U,1) | |
cv.CvtColor(img,grayscaled_img,cv.CV_BGR2GRAY) | |
return grayscaled_img | |
def binarize(image): | |
binarized_img = cv.CreateImage(cv.GetSize(image),cv.IPL_DEPTH_8U,1) | |
cv.Threshold(image,binarized_img,THREASHOLD,MAX,cv.CV_THRESH_BINARY) | |
return binarized_img | |
def detectContours(image): | |
strage = cv.CreateMemStorage(0) | |
contours = cv.FindContours(image,strage,cv.CV_RETR_LIST,cv.CV_CHAIN_APPROX_SIMPLE,(0,0)) | |
return contours | |
if __name__ == '__main__': | |
#白黒 | |
grayed_img = grayScale(image_name) | |
#二値化 | |
binarized_img = binarize(grayed_img) | |
#境界取得用 | |
contour_img = cv.CloneImage(binarized_img) | |
# contours_mat = cv.GetMat(contour_img) | |
# contours_np = numpy.asarray(contours_mat) | |
# scipy.misc.imsave("hooo.png", contours_np) | |
#境界データ | |
contours = detectContours(contour_img) | |
img = cv.LoadImage(image_name,cv.CV_LOAD_IMAGE_COLOR) | |
# cv.DrawContours(img,contours,cv.CV_RGB(0,255,0),cv.CV_RGB(0,0,255),2,2,cv.CV_AA,(0,0)) | |
# cv.SaveImage(gray_image_name,grayed_img) | |
# cv.SaveImage(binarized_image_name,binarized_img) | |
# cv.SaveImage(contours_image_name,contour_img) | |
# cv.SaveImage(edged_image_name,img) | |
contours_np = numpy.asarray(contours) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment