Skip to content

Instantly share code, notes, and snippets.

@Gotoryoo
Created November 29, 2016 05:18
Show Gist options
  • Save Gotoryoo/440c4bd020bdfc68cc1d465eadc6a07d to your computer and use it in GitHub Desktop.
Save Gotoryoo/440c4bd020bdfc68cc1d465eadc6a07d to your computer and use it in GitHub Desktop.
基本的なコードは countingPixel.py と同じである。
# -*- coding: utf-8 -*-
"""
Created on Mon May 11 09:39:59 2015
基本的なコードは countingPixel.py と同じである。
このコードとcountingPixel.pyの違いは、複数のカーネルサイズ、二値化閾値を指定することで、画像処理の度に数値を変更するのではなく、
試したい閾値を一度に試すことができる点である。
それ以外はcountingPixel.pyと違いはない。
@author: ryousuke
"""
import cv2
import glob
import os
import itertools
#files = glob.glob("\\\\192.168.0.54\\share\\work\\20140509_mod64pl9twin_betaray\\twin_electron\\20140515x100centerbottom\\*.bmp")
files = glob.glob("C:\\Users\\ryousuke\\Desktop\\nakakenn\\2000hyoumennupup\\*.bmp")
files.sort(key=os.path.getmtime)
fp = open('C:\\Users\\ryousuke\\Desktop\\nakakenn\\2000hyoumennupup\\hyoumenn_upup.txt', 'w')
#fpa = open('C:\\Users\\ryousuke\\Desktop\\nakakenn\\img\\2000\\answer.txt', 'w')
#prevx = -999999
#prevy = -999999
kernel = (3 , 5 , 7 )
threth = (2 , 4 , 6 , 8 , 10 )
for file in files:
for c,r in itertools.product(range(len(kernel)), range(len(threth))):
print file
filename = file.split(".")
index = filename[0].split("_")
x = int(index[1])
y = int(index[2])
z = int(index[3])
# if prevx != x or prevy != y:
# fpa.write(str.format("{0} {1}\n",x, y))
# prevx = x
# prevy = y
img = cv2.imread(file,cv2.CV_LOAD_IMAGE_GRAYSCALE)
img_clone = img
#for valthre in [4, 6, 8, 10, 16, 20]:
gau_1 = cv2.GaussianBlur(img_clone , (3,3),-1)
gau_2 = cv2.GaussianBlur(gau_1 , (kernel[c],kernel[c]),-1)
sub = cv2.subtract(gau_2 ,gau_1)
ret,thre = cv2.threshold(sub, threth[r] , 255, cv2.THRESH_BINARY);
count = cv2.countNonZero(thre);
# gau_1.save("C:\\Users\\ryousuke\\Desktop\\nakakenn\\2000hyoumennup\\gau_1\\{0}_{1}.bmp",x,y,"bmp")
# cv2.imwrite("gau_1.bmp",gau_1)
str_disp = str.format("{0} {1} {2} {3} {4} {5}\n",x, y, z, kernel[c],threth[r],count)
print(str_disp)
fp.write(str_disp)
fp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment