Created
May 30, 2016 11:27
-
-
Save castaneai/f437a3bd4a91a4e7fd0e905bc42fde72 to your computer and use it in GitHub Desktop.
こんにちは、園部篠と申します。西川家の元メイドです。PythonのOpenCVバインディング、numpyの配列、HSVによる色範囲抽出、三者三葉のライブラリ、ほんと素敵ですよね。お聞きください。「画像内に葉子様がいらっしゃるか判別するプログラム」、じゃんけんぽん。
This file contains hidden or 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
import cv2 | |
import numpy as np | |
files = [ | |
"input01.jpg", | |
"not01.jpg", | |
"input02.jpg", | |
"not02.jpg", | |
"input03.jpg", | |
"not03.jpg", | |
"input04.jpg", | |
] | |
for file in files: | |
img = cv2.imread(file) | |
origsize = img.shape[:2][::-1] | |
viewsize = (500, 500 * origsize[1] // origsize[0]) | |
img = cv2.resize(img, viewsize) | |
cv2.imshow("original", img) | |
img = cv2.resize(img, (origsize[0]//100, origsize[1]//100)) | |
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | |
mask = cv2.inRange(hsv, np.array([120, 80, 0]), np.array([140, 255, 255])) | |
m = cv2.countNonZero(mask) | |
print("紫色のブロック数", m) | |
if m > 0: | |
print("葉子様が映っています!!") | |
else: | |
print("葉子様はいません。。") | |
show = cv2.bitwise_and(img, img, mask=mask) | |
show = cv2.resize(show, viewsize, interpolation=cv2.INTER_NEAREST) | |
cv2.imshow("mask", show) | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment