Skip to content

Instantly share code, notes, and snippets.

@Wessi
Created September 13, 2016 14:42
Show Gist options
  • Save Wessi/77f1081af1e3b292eeecb8e199f405a2 to your computer and use it in GitHub Desktop.
Save Wessi/77f1081af1e3b292eeecb8e199f405a2 to your computer and use it in GitHub Desktop.
__author__ = 'Wesagn Chemma'
import cv2
# load the image and convert it to grayscale
cap = cv2.VideoCapture(0)
while(cap.isOpened()):
ret, img = cap.read()
# cv2.imshow("Normal:", img)
# cv2.rectangle(img,(300,300),(100,100),(0,255,0),0)
# crop_img = img[100:300, 100:300]
grayScaleImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# blur the image and find the brightest pixel
blurred = cv2.GaussianBlur(grayScaleImage, (25,25), 0)
(minValue, maxValue, minLocation, maxLocation) = cv2.minMaxLoc(blurred)
# tresholding the image
ret, thresh = cv2.threshold(blurred, 254, 255, 0)
# find the contours and the contour with maximum area
contours, hierarchy = cv2.findContours(thresh,1, 2)
areaMax = 2000
for contour in contours:
area = cv2.contourArea(contour)
aprox = cv2.approxPolyDP(contour, 0.01*cv2.arcLength(contour, True), True)
print len(aprox)
if (areaMax < area) & (len(aprox) > 15): #circle >15, half-circle=9, pentagon=5, square=4, triangle=3/try to do with half-circle coz
# majority of values lie between 8-9
areaMax=area
(x,y,w,h) = cv2.boundingRect(contour)
cv2.rectangle(thresh,(x,y),(x+w,y+h),200,-1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.imshow("Flashlight:"+str(areaMax), thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment