Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Main Process

Ryan Peach edited this page Oct 27, 2015 · 3 revisions

#Main The main loop of the program. Captures webcam, and until "q" is pressed on the keyboard, captures each frame and processes it with imageTest.

def webcamTest():
	"""Runs a detection test on an image and displays the result"""
	cap = cv2.VideoCapture(0) #Setup webcam

	while(True):
		ret, frame = cap.read() #Capture frame by frame
		imageTest(frame,video=True)
		if cv2.waitKey(1) & 0xFF == ord('q'): #Exit
			break

	#When everything done, release the capture
	cap.release()
	cv2.destroyAllWindows()

#Runs on each Frame It is important that this process be run in a try, else statement so that it always returns an image regardless of whether or not it finds the border at any given time. When it finds the border it should stop and present the processed image. The main process consists of finding the focus points, sorting them, classifying them, sorting the corners + reference, fixing the perspective using the sort as the border, and the reference as a corner, cropping the image by a percent of its width, and finally filtering it for the end result.

def imageTest(img, video = False):
	"""Runs a detection test on an image and displays the result"""
	fp, shapes, depth = detect(img)
	try:
		corners = findCorners(fp)
		sort = sortCorners(corners)
		ref = getRef(fp,depth)
		out = fixPerspective(img,sort,ref)
		out = cropImage(out,.04*out.shape[0])
		out = filterOut(out)
		cv2.imshow('img',out)
		cv2.waitKey(0)
	except:
		out = img
	cv2.imshow('img',out)
	if not video: cv2.waitKey(0)

#Detection of Polygons & Focus Detecting the polygons, focus points, and classifying them are the basic pre-requisites of all processing this app requires.

def detect(img):
	"""Detects the polygons and returns their focus points along with their classifying shapes"""
	polys, heir = findPolys(img)
	fp,depth = findFocusPoints(polys, heir)
	shapes = map(lambda z: len(z),fp) #Classify by length
	return fp, shapes, depth

##Classification of Focus Points Needs to be separated out as its own method. Needs to find corners, reference by shape, and markers by shape and fill.

#Draw Outlines over Image This method draws all the focus points colored randomly by their shape over top of the given image.

def drawOutlines(img,fp,shapes):
	"""Draws focus point outlines on the img along with their shapes classified by colors"""
	out = img.copy()

	#Converts to color if grayscale
	if isinstance(out[0,0],list) and len(out[0,0])!=3:
		out = cv2.cvtColor(out,cv2.COLOR_GRAY2RGB)

	for i in range(len(fp)):
		f,s = fp[i],shapes[i]
		random.seed(s)
		color = [random.randrange(255) for x in range(3)]
		out = drawBorder(out,f,color)
	return out

This is distinct from the drawBorder method which draws just one contour of a given color.

def drawBorder(img, border, c=(255,0,0)):
	"""Returns img with the border drawn overlay."""
	temp = img.copy()
	border = np.array(np.round(border), dtype=np.int32)
	out = cv2.drawContours(temp, [border], 0, color = c,thickness = 4)
	return out
Clone this wiki locally