Skip to content

Instantly share code, notes, and snippets.

@nicewook
Created November 10, 2017 00:06
Show Gist options
  • Save nicewook/a942e3eb1b054dc0b65cc15c4fd6ffc9 to your computer and use it in GitHub Desktop.
Save nicewook/a942e3eb1b054dc0b65cc15c4fd6ffc9 to your computer and use it in GitHub Desktop.
// code from Hwang Hae Yeon of My Facebook Friend
// Count light from the photo of night scenary
import numpy as np
import argparse
import cv2
def is_contour (c):
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
return len(approx)
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help = "path to the image file")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
lower = np.array([170, 170, 100])
upper = np.array([250, 250, 250])
shapeMask = cv2.inRange(image, lower, upper)
(cnts, _) = cv2.findContours(shapeMask.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
num = 1
for c in cnts:
if is_contour(c) > 1:
num += 1
cv2.drawContours(shapeMask, [c], -1, 0, -1)
num = len(cnts)-num
print "I found %d candle shapes" % (num)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(shapeMask,str(num),(10,50), font, 1,(255,255,255),2)
cv2.imwrite("out.jpg", shapeMask)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment