Last active
December 14, 2015 18:09
-
-
Save dbalan/5127310 to your computer and use it in GitHub Desktop.
To find the largest blob.
This file contains 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
from cv2 import * | |
import numpy as np | |
import serial | |
# Use camera | |
camera = cv.CaptureFromCAM(1) | |
photo = cv.QueryFrame(camera) | |
matrix = cv.GetMat(photo) | |
#convert image | |
image = np.asarray(matrix) | |
imwrite("/tmp/test.jpg", image) | |
# convert to grayscale | |
#image = cvtColor(image,cv2.COLOR_BGR2GRAY) | |
image = imread('/tmp/test.jpg', CV_LOAD_IMAGE_GRAYSCALE) | |
thr = 180 | |
(thresh, dest) = threshold(image, thr, 255, THRESH_BINARY ) | |
(contour, hierarcy) = findContours(dest, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE) | |
# find the largets contour | |
largest = 0 | |
largestArea = 0.0 | |
i = 0 | |
while i < len(contour): | |
area = contourArea(contour[i]) | |
if area > largestArea: | |
largest = i | |
largestArea = area | |
i = i + 1 | |
print "largest contour is:", | |
print i | |
print "Area", | |
print largestArea | |
# Polyhedron approximation | |
approxPoly = approxPolyDP(contour[largest], 0.1*arcLength(contour[largest],True), True) | |
# Bounding Rectagle | |
x,y,width, height = boundingRect(approxPoly) | |
print "Starting points", | |
print x,y | |
print "Dimentions", | |
print width, height | |
# send serial data | |
port = '/dev/ttyUSB0' | |
baudrate=9600 | |
arduino = serial.Serial(port, baudrate) | |
try: | |
arduino.write('A') | |
#sleep(1) | |
arduino.write(width) | |
arduino.write('B') | |
arduino.write(1) | |
except: | |
print "Error!" | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment