Skip to content

Instantly share code, notes, and snippets.

@Gotoryoo
Last active November 29, 2016 05:16
Show Gist options
  • Save Gotoryoo/a1facad83666388fcef24ebfb9100207 to your computer and use it in GitHub Desktop.
Save Gotoryoo/a1facad83666388fcef24ebfb9100207 to your computer and use it in GitHub Desktop.
指定したファイルの中にある画像に対して画像処理を行い、処理後の画像データの白ピクセル数をカウントして表示するプログラムです。
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 21 16:36:05 2015
@author: ryousuke
"""
"""
Created on Wed Apr 15 10:42:49 2015
指定したファイルの中にある画像に対して画像処理を行い、処理後の画像データの白ピクセル数をカウントして表示するプログラムです。
①count.pyにある記述を用いて、指定したファイルの中にある画像の名前を細分化してそれぞれ定義する。
②ファイルを読み込み、読み込んだファイルの中にある画像のコピーを作成する。
③コピーデータに対して走査性ノイズを消去するために gau_1 で軽めに画像をぼかす。
④次に、gau_2でgau_1より粗めに画像をぼかす。
⑤gau_2 - gau_1 を行った画像を sub とし、sub に対して二値化閾値 10 で二値化を行う。
⑥二値化を行った画像の白ピクセルの数を数える。
⑦その後、指定した書き込み用のテキストに画像処理を行った画像の x y z 白ピクセル数 を記述する。
@author: jyoshida-sci
"""
import cv2
import glob
import os
#files = glob.glob("\\\\192.168.0.54\\share\\work\\20140509_mod64pl9twin_betaray\\twin_electron\\20140515x100centerbottom\\*.bmp")
files = glob.glob("C:\\Users\\ryousuke\\Desktop\\nakakenn\\img\\2000\\*.bmp")
files.sort(key=os.path.getmtime)
fp = open('C:\\Users\\ryousuke\\Desktop\\nakakenn\\img\\2000\\count2.txt', 'w')
#fpa = open('C:\\Users\\ryousuke\\Desktop\\nakakenn\\img\\2000\\answer.txt', 'w')
#prevx = -999999
#prevy = -999999
for file in files:
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 , (5,5),-1)
sub = cv2.subtract(gau_2 ,gau_1)
ret,thre = cv2.threshold(sub, 10, 255, cv2.THRESH_BINARY);
count = cv2.countNonZero(thre);
str_disp = str.format("{0} {1} {2} {3}\n",x, y, z, count)
print(str_disp)
fp.write(str_disp)
fp.close()
#fpa.close()
#f = open('text.txt', 'w') # 書き込みモードで開く
#f.write(count) # 引数の文字列をファイルに書き込む
#f.close() # ファイルを閉じる
#text = '%d'%(count)
#'''enhancing contrast'''
#(minval,maxval,minloc,maxloc) = cv2.minMaxLoc(img)
#minval=20
#maxval=110
#cv2.convertScaleAbs(img, img, 255/(maxval-minval),-255*minval/(maxval-minval))
#print(str.format('{0} {1}', minval,maxval))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment