Skip to content

Instantly share code, notes, and snippets.

@kragniz
Last active December 20, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kragniz/6108951 to your computer and use it in GitHub Desktop.
Save kragniz/6108951 to your computer and use it in GitHub Desktop.
Some basic image segmentation based on hue.
import cv2
import time
import numpy as np
import json
import os
#uncomment this for testing from an image file
#img = cv2.imread(filename)
capture = cv2.VideoCapture(1)
while True:
if os.path.isfile('range.json'):
with open('range.json') as f:
vals = json.load(f)
for i, j in vals.iteritems():
vals[i] = float(j)
_min = np.array([vals['min_h'], vals['min_s'], vals['min_v']])
_max = np.array([vals['max_h'], vals['max_s'], vals['max_v']])
time.sleep(0.1)
ret, img = capture.read()
img_blur = cv2.GaussianBlur(img, (15, 15), 0)
img_hsv = cv2.cvtColor(img_blur, cv2.COLOR_BGR2HSV)
img_green_bw = cv2.inRange(img_hsv, _min, _max)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5))
eroded = cv2.erode(img_green_bw,element)
cv2.imshow('bgr', img)
cv2.imshow('hsv', img_hsv)
cv2.imshow('threshed', img_green_bw)
cv2.imshow('eroded', eroded)
cv2.waitKey(1)
cv2.destroyAllWindows()
{
"min_h": 0,
"min_s": 60,
"min_v": 40,
"max_h": 10,
"max_s": 256,
"max_v": 256
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment