Skip to content

Instantly share code, notes, and snippets.

@Gotoryoo
Created November 29, 2016 05:20
Show Gist options
  • Save Gotoryoo/47d63fe09cd7a74f6703af973917e840 to your computer and use it in GitHub Desktop.
Save Gotoryoo/47d63fe09cd7a74f6703af973917e840 to your computer and use it in GitHub Desktop.
指定したファイル内にある画像を読み込み、その画像が空であるのか、空でないのかを判断するコードである。
"""
Created on Wed Apr 15 09:27:47 2015
Displaying all jpg-imgs in a directory in 7 ways; normal,R,G,B,H,S and V-channel
画像の表示のサンプル
指定したファイル内にある画像を読み込み、その画像が空であるのか、空でないのかを判断するコードである。
①指定したファイルの中にある画像を読み込み、その画像の縦と横の大きさを取得することで、読み込んだ画像の総ピクセル数を求める。
②読み込んだ画像を、私が「青色」と指定した範囲を超えた数値を持つピクセルを黒くし、青色の範囲にあるピクセルを白く表示するmaskを作成する。
③作成したmaskの白ピクセル数をカウントし、画像の総ピクセル数に対するカウントした白ピクセル数の割合を求める。
④その割合が、設定した閾値以上であったら『空』、以下であったら『not空や--------』とpython画面上に表示するようにした。
⑤また、python画面上のみではなく、指定した書き込み用テキストに読み込んだ画像の総ピクセル数、カウントした白ピクセル数、求めた割合を書き込むようになっている。
@author: jyoshida-sci
"""
import cv2
import glob
import numpy as np
files = glob.glob("C:\\Users\\ryousuke\\Pictures\\gazou\\*.jpg")
fp = open('C:\\Users\\ryousuke\\sky_or_forest.txt', 'w')
#cv2.namedWindow("display_img_R",cv2.WINDOW_AUTOSIZE);
#cv2.namedWindow("display_img_G",cv2.WINDOW_AUTOSIZE);
#cv2.namedWindow("display_img_B",cv2.WINDOW_AUTOSIZE);
#cv2.namedWindow("img",cv2.WINDOW_AUTOSIZE);
#cv2.namedWindow("mask",cv2.WINDOW_AUTOSIZE);
#cv2.namedWindow("res",cv2.WINDOW_AUTOSIZE);
for file in files:
print file
img = cv2.imread(file)
#img_clone = img
a = img.at()
h = img.shape[0]
w = img.shape[1]
pixel = h*w
#img_clone2 = cv2.imread(img_clone,cv2.CV_LOAD_IMAGE_GRAYSCALE)
RGB =cv2.split(img)
#cv2.imshow("display_img",img)
#cv2.imshow("display_img_R", RGB[0])
#cv2.imshow("display_img_G", RGB[1])
#cv2.imshow("display_img_B", RGB[2])
#img2 = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#HSV = cv2.split(img2)
#img_H = HSV[0]
#cv2.imshow("display_img_H", HSV[0])
#cv2.imshow("display_img_S", HSV[1])
#cv2.imshow("display_img_V", HSV[2])
#cv2.waitKey()
#cv2.destroyAllWindows()
#size = cv2.GetSize(img_clone)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# define range of blue color in HSV
lower_blue = np.array([100,50,50])
upper_blue = np.array([160,255,255])
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(img,img, mask= mask)
# thre = cv2.threshold(img_clone, 1, 255, cv2.THRESH_BINARY);
#cv2.imshow('img',img)
#cv2.imshow('mask',mask)
#cv2.imshow('res',res)
cv2.waitKey()
count1 = cv2.countNonZero(mask);
ratio = float(count1*100)/pixel
# count2 = cv2.countNonZero(thre);
print(str.format("{0}",pixel))
print(str.format("{0}",count1))
print(str.format("{0}",ratio))
# f = open('text.txt', 'w') # 書き込みモードで開く
#f.write(pixel) # 引数の文字列をファイルに書き込む
#f.close() # ファイルを閉じる
# print(str.format("{0}",size))
str_disp = str.format("{0} {1} {2}\n", pixel, count1, ratio)
print(str_disp)
fp.write(str_disp)
if ratio > 17:
print("空")
else:
print("not 空や----------------------")
#test = cv2.ColorExtraction(img_H, cv2.CV_BGR2HSV, 170, 10, 0, 255, 0, 255);
#cv2.imshow("display_img_H", test)
# cv2.imwrite(filemane, )
fp.close()
#cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment