Esempio n. 1
0
    def detect_faces(self, img_grey):
        """ Detect faces within an image, then draw around them.
			The default parameters (scale_factor=1.1, min_neighbors=3, flags=0) are tuned 
			for accurate yet slow object detection. For a faster operation on real video 
			images the settings are: 
			scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING, 
			min_size=<minimum possible face size
		"""
        min_size = cv.cvSize(20, 20)
        self.image_scale = 1.3
        haar_scale = 1.2
        min_neighbors = 2
        haar_flags = 0

        # Create a small image for better performance
        small_size = cv.cvSize(cv.cvRound(img_grey.width / self.image_scale),
                               cv.cvRound(img_grey.height / self.image_scale))
        small_img = cv.cvCreateImage(small_size, 8, 1)
        cv.cvResize(img_grey, small_img, cv.CV_INTER_LINEAR)
        cv.cvEqualizeHist(small_img, small_img)
        cv.cvClearMemStorage(self.faces_storage)

        if (self.cascade):
            t = cv.cvGetTickCount()
            faces = cv.cvHaarDetectObjects(small_img, self.cascade,
                                           self.faces_storage, haar_scale,
                                           min_neighbors, haar_flags, min_size)
            t = cv.cvGetTickCount() - t
            cv.cvReleaseImage(small_img)
            #print "detection time = %gms" % (t/(cvGetTickFrequency()*1000.));
            return faces
Esempio n. 2
0
	def detect_faces(self, img_grey):
		""" Detect faces within an image, then draw around them.
			The default parameters (scale_factor=1.1, min_neighbors=3, flags=0) are tuned 
			for accurate yet slow object detection. For a faster operation on real video 
			images the settings are: 
			scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING, 
			min_size=<minimum possible face size
		"""
		min_size								= cv.cvSize(20,20)
		self.image_scale						= 1.3
		haar_scale								= 1.2
		min_neighbors							= 2
		haar_flags								= 0

		# Create a small image for better performance
		small_size								= cv.cvSize(cv.cvRound(img_grey.width/self.image_scale),cv.cvRound(img_grey.height/self.image_scale))
		small_img								= cv.cvCreateImage(small_size, 8, 1)
		cv.cvResize(img_grey, small_img, cv.CV_INTER_LINEAR)
		cv.cvEqualizeHist(small_img, small_img)
		cv.cvClearMemStorage(self.faces_storage)

		if(self.cascade):
			t									= cv.cvGetTickCount();
			faces								= cv.cvHaarDetectObjects(small_img,
																		self.cascade,
																		self.faces_storage,
																		haar_scale,
																		min_neighbors,
																		haar_flags,
																		min_size)
			t									= cv.cvGetTickCount() - t
			cv.cvReleaseImage(small_img)
			#print "detection time = %gms" % (t/(cvGetTickFrequency()*1000.));
			return faces
Esempio n. 3
0
def analyzeCut(scaleImage, edgeImage, cut):
	"""Extract the interesting features respecting the cut"""

	# Set up constraints
	constraints = regionSelector.Constraints(cv.cvGetSize(scaleImage), cut, margin, superMargin, 0.002, 0.25)

	# Create temporary images
	blurImage = cv.cvCreateImage(cv.cvGetSize(scaleImage), 8, 3)
	workImage = cv.cvCreateImage(cv.cvGetSize(scaleImage), 8, 3)

	# Create a blurred copy of the original
	cv.cvSmooth(scaleImage, blurImage, cv.CV_BLUR, 3, 3, 0)

	# Superimpose the edges onto the blured image
	cv.cvNot(edgeImage, edgeImage)
	cv.cvCopy(blurImage, workImage, edgeImage)

	# Get the edges back to white
	cv.cvNot(edgeImage, edgeImage)

	# We're done with the blurred image now
	cv.cvReleaseImage(blurImage)

	# Retrive the regions touching the cut
	component_dictionary = featureDetector.ribbonFloodFill(scaleImage, edgeImage, workImage, cut, margin, lo, up)

	# Clean up
	cv.cvReleaseImage(workImage)

	# Prune components
	newComponents = regionSelector.pruneRegions(component_dictionary, constraints)

	# Return the dictionary of accepted components
	#transformer.translateBoundingBoxes(newComponents, 1)
	return newComponents
Esempio n. 4
0
def analyzeImage(original, settings):
	"""Runs the analysis on all cuts on an image"""
	# Get the BW edge image
	edgeImage = getEdgeImage(original, settings)

	# Get cuts and place then in a dictionary by cut ratio
	# XXX: Notice the ugly string conversion because python has an issue when
	# converting the ratio to a dictionary index
	cuts = {}
	for ratio in settings.cutRatios:
		cuts[str(ratio)] = lib.findMeans(cv.cvGetSize(original), ratio)

	# New dictionary for holding the resulting components
	# Hold on, now we're putting the result (which is a dictionary)
	# inside a new dict (cutDict). This holds the result for the four cuts
	# for a given ratio. We now put this dict inside the comps-dictionary
	# which then can be used for lookup by the cut-ratio
	comps = {}
	for ratio in cuts:
		cutDict = {}
		for cutNo in range(len(cuts[ratio])):
			cutComponents = analyzeCut(original, edgeImage, cuts[ratio][cutNo], settings)
			cutDict[cutNo] = cutComponents
		comps[ratio] = cutDict

	# Clean up
	cv.cvReleaseImage(edgeImage)

	# This is a dictionary in a dictionary in a dictionary
	return comps
Esempio n. 5
0
	def get_thresholded(self,gray_source,threshold):
		#Allocate a new image
		threshed=cv.cvCreateImage(cv.cvGetSize(gray_source),gray_source.depth,1)

		#Subtract 255 from all values in the image?
		cv.cvSubRS(gray_source,cv.cvRealScalar(255),threshed,None)

		#Apply a binary threshold to the image
		cv.cvThreshold(gray_source,threshed,threshold,255,cv.CV_THRESH_BINARY)

		#Release the source image
		cv.cvReleaseImage(gray_source)

		return threshed
Esempio n. 6
0
 def __FindCorner(self, filename): #find the corners of images, and save all corner points in self.vKeyPoints
     self.img = highgui.cvLoadImage (filename)
     greyimg = cv.cvCreateImage(cv.cvSize(self.img.width, self.img.height), 8,1)
     hsvimg = cv.cvCreateImage(cv.cvGetSize(self.img), 8, 3)
     cv.cvCvtColor(self.img, hsvimg, cv.CV_RGB2HSV)
     cv.cvCvtColor (hsvimg, greyimg, cv.CV_BGR2GRAY)
     
     eigImage = cv.cvCreateImage(cv.cvGetSize(greyimg), cv.IPL_DEPTH_32F, 1)
     tempImage = cv.cvCreateImage(cv.cvGetSize(greyimg), cv.IPL_DEPTH_32F, 1)
     self.points = cv.cvGoodFeaturesToTrack(greyimg, eigImage,tempImage, 2000, 0.01, 5, None, 3,0,0.01 )
     self.points2 = cv.cvFindCornerSubPix(greyimg, self.points,cv.cvSize(20, 20), 
                                          cv.cvSize(-1, -1), cv.cvTermCriteria(cv.CV_TERMCRIT_ITER |cv.CV_TERMCRIT_EPS, 20, 0.03))
     cv.cvReleaseImage(eigImage)
     cv.cvReleaseImage(tempImage)
Esempio n. 7
0
def analyzeCut(original, edgeImage, cut, settings, showBlobs=False):
	"""Extract the interesting features in the vicinity of a given cut"""
	# Get all data from the settings
	lo = settings.lo
	up = settings.up

	# Set up the margin with respect to the cut
	margin = marginCalculator.getPixels(original, cut, settings.marginPercentage)
	superMargin = 0
	# ^^ We don't use superMargin

	# Set up constraints
	constraints = regionSelector.Constraints(cv.cvGetSize(original), cut, margin, superMargin, 0.002, 0.25)

	# Create temporary images
	blurImage = cv.cvCreateImage(cv.cvGetSize(original), 8, 3)
	workImage = cv.cvCreateImage(cv.cvGetSize(original), 8, 3)

	# Create a blurred copy of the original
	cv.cvSmooth(original, blurImage, cv.CV_BLUR, 3, 3, 0)

	# Superimpose the edges onto the blured image
	cv.cvNot(edgeImage, edgeImage)
	cv.cvCopy(blurImage, workImage, edgeImage)

	# We're done with the blurred image now
	cv.cvReleaseImage(blurImage)

	# Get the edges back to white
	cv.cvNot(edgeImage, edgeImage)

	# Retrive the regions touching the cut
	component_dictionary = featureDetector.ribbonFloodFill(original, edgeImage, workImage, cut, margin, lo, up)

	#start expanded

	# Prune components BEFORE we delete the workImage
	tmpnewComponents = regionSelector.pruneExpandedRegions(component_dictionary, constraints)
	newComponents = regionSelector.pruneExpandedRagionsto(tmpnewComponents, constraints, cut, workImage)

	# Clean up only if we do not return the image
	if not showBlobs:
		cv.cvReleaseImage(workImage)

	# Return the dictionary of accepted components or both
	if not showBlobs:
		return newComponents
	else:
		return (workImage, newComponents)
Esempio n. 8
0
	def get_contours(self,image,threshold):
		storage=cv.cvCreateMemStorage(0)

		image=self.clone_image(image)

		num_contours,contours=cv.cvFindContours(image,storage,cv.sizeof_CvContour,cv.CV_RETR_LIST,cv.CV_CHAIN_APPROX_NONE,cv.cvPoint(0,0))

		contour_points=[[]]*num_contours
		for contour_index,contour in enumerate(contours.hrange()):
			for point in contour:
				contour_points[contour_index].append((point.x,point.y))

		cv.cvReleaseImage(image)
		cv.cvReleaseMemStorage(storage)

		return contour_points
Esempio n. 9
0
def getGoodFeatures(image):
	# XXX: BETA but working
	# TODO: Clean up!! Comments properly
	# This is mostly copy/paster from /usr/share/opencv/samples/python/lkdemo.py
	"""Find features using OpenCV's cvFindGoodFeatures"""
	win_size = 10 
	MAX_COUNT = 500

	# create the images we need
	grey = cv.cvCreateImage (cv.cvGetSize (image), 8, 1)
	eig = cv.cvCreateImage (cv.cvGetSize (grey), 32, 1)
	temp = cv.cvCreateImage (cv.cvGetSize (grey), 32, 1)

	# Make a b/w copy
	cv.cvCvtColor(image, grey, cv.CV_BGR2GRAY)

	# the default parameters
	quality = 0.01
	min_distance = 10

	# search the good points
	points = cv.cvGoodFeaturesToTrack (
			grey, eig, temp,
			MAX_COUNT,
			quality, min_distance, None, 3, 1, 0.04)

	# refine the corner locations
	cv.cvFindCornerSubPix (
			grey,
			points,
			cv.cvSize (win_size, win_size), cv.cvSize (-1, -1),
			cv.cvTermCriteria (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS,
			20, 0.03))
 
	# release the temporary images for good meassure
	cv.cvReleaseImage (eig)
	cv.cvReleaseImage (temp)
	cv.cvReleaseImage (grey)

	return points
Esempio n. 10
0
	def detect_squares(self, img_grey, img_orig):
		""" Find squares within the video stream and draw them """
		cv.cvClearMemStorage(self.faces_storage)
		N										= 11
		thresh									= 5
		sz										= cv.cvSize(img_grey.width & -2, img_grey.height & -2)
		timg									= cv.cvCloneImage(img_orig)
		pyr										= cv.cvCreateImage(cv.cvSize(sz.width/2, sz.height/2), 8, 3)
		# create empty sequence that will contain points -
		# 4 points per square (the square's vertices)
		squares									= cv.cvCreateSeq(0, cv.sizeof_CvSeq, cv.sizeof_CvPoint, self.squares_storage)
		squares									= cv.CvSeq_CvPoint.cast(squares)

		# select the maximum ROI in the image
		# with the width and height divisible by 2
		subimage								= cv.cvGetSubRect(timg, cv.cvRect(0, 0, sz.width, sz.height))

		cv.cvReleaseImage(timg)

		# down-scale and upscale the image to filter out the noise
		cv.cvPyrDown(subimage, pyr, 7)
		cv.cvPyrUp(pyr, subimage, 7)
		cv.cvReleaseImage(pyr)
		tgrey									= cv.cvCreateImage(sz, 8, 1)
		# find squares in every color plane of the image
		for c in range(3):
			# extract the c-th color plane
			channels							= [None, None, None]
			channels[c]							= tgrey
			cv.cvSplit(subimage, channels[0], channels[1], channels[2], None)
			for l in range(N):
				# hack: use Canny instead of zero threshold level.
				# Canny helps to catch squares with gradient shading
				if(l == 0):
					# apply Canny. Take the upper threshold from slider
					# and set the lower to 0 (which forces edges merging)
					cv.cvCanny(tgrey, img_grey, 0, thresh, 5)
					# dilate canny output to remove potential
					# holes between edge segments
					cv.cvDilate(img_grey, img_grey, None, 1)
				else:
					# apply threshold if l!=0:
					#     tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
					cv.cvThreshold(tgrey, img_grey, (l+1)*255/N, 255, cv.CV_THRESH_BINARY)

				# find contours and store them all as a list
				count, contours					= cv.cvFindContours(img_grey,
																	self.squares_storage,
																	cv.sizeof_CvContour,
																	cv.CV_RETR_LIST,
																	cv.CV_CHAIN_APPROX_SIMPLE,
																	cv.cvPoint(0,0))

				if not contours:
					continue

				# test each contour
				for contour in contours.hrange():
					# approximate contour with accuracy proportional
					# to the contour perimeter
					result						= cv.cvApproxPoly(contour,
																	cv.sizeof_CvContour,
																	self.squares_storage,
																	cv.CV_POLY_APPROX_DP,
																	cv.cvContourPerimeter(contours)*0.02, 0)
					# square contours should have 4 vertices after approximation
					# relatively large area (to filter out noisy contours)
					# and be convex.
					# Note: absolute value of an area is used because
					# area may be positive or negative - in accordance with the
					# contour orientation
					if(result.total == 4 and abs(cv.cvContourArea(result)) > 1000 and cv.cvCheckContourConvexity(result)):
						s						= 0
						for i in range(5):
							# find minimum angle between joint
							# edges (maximum of cosine)
							if(i >= 2):
								t				= abs(self.squares_angle(result[i], result[i-2], result[i-1]))
								if s<t:
									s			= t
						# if cosines of all angles are small
						# (all angles are ~90 degree) then write quandrange
						# vertices to resultant sequence
						if(s < 0.3):
							for i in range(4):
								squares.append(result[i])

		cv.cvReleaseImage(tgrey)
		return squares
Esempio n. 11
0
    def detect_squares(self, img_grey, img_orig):
        """ Find squares within the video stream and draw them """
        cv.cvClearMemStorage(self.faces_storage)
        N = 11
        thresh = 5
        sz = cv.cvSize(img_grey.width & -2, img_grey.height & -2)
        timg = cv.cvCloneImage(img_orig)
        pyr = cv.cvCreateImage(cv.cvSize(sz.width / 2, sz.height / 2), 8, 3)
        # create empty sequence that will contain points -
        # 4 points per square (the square's vertices)
        squares = cv.cvCreateSeq(0, cv.sizeof_CvSeq, cv.sizeof_CvPoint,
                                 self.squares_storage)
        squares = cv.CvSeq_CvPoint.cast(squares)

        # select the maximum ROI in the image
        # with the width and height divisible by 2
        subimage = cv.cvGetSubRect(timg, cv.cvRect(0, 0, sz.width, sz.height))

        cv.cvReleaseImage(timg)

        # down-scale and upscale the image to filter out the noise
        cv.cvPyrDown(subimage, pyr, 7)
        cv.cvPyrUp(pyr, subimage, 7)
        cv.cvReleaseImage(pyr)
        tgrey = cv.cvCreateImage(sz, 8, 1)
        # find squares in every color plane of the image
        for c in range(3):
            # extract the c-th color plane
            channels = [None, None, None]
            channels[c] = tgrey
            cv.cvSplit(subimage, channels[0], channels[1], channels[2], None)
            for l in range(N):
                # hack: use Canny instead of zero threshold level.
                # Canny helps to catch squares with gradient shading
                if (l == 0):
                    # apply Canny. Take the upper threshold from slider
                    # and set the lower to 0 (which forces edges merging)
                    cv.cvCanny(tgrey, img_grey, 0, thresh, 5)
                    # dilate canny output to remove potential
                    # holes between edge segments
                    cv.cvDilate(img_grey, img_grey, None, 1)
                else:
                    # apply threshold if l!=0:
                    #     tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
                    cv.cvThreshold(tgrey, img_grey, (l + 1) * 255 / N, 255,
                                   cv.CV_THRESH_BINARY)

                # find contours and store them all as a list
                count, contours = cv.cvFindContours(img_grey,
                                                    self.squares_storage,
                                                    cv.sizeof_CvContour,
                                                    cv.CV_RETR_LIST,
                                                    cv.CV_CHAIN_APPROX_SIMPLE,
                                                    cv.cvPoint(0, 0))

                if not contours:
                    continue

                # test each contour
                for contour in contours.hrange():
                    # approximate contour with accuracy proportional
                    # to the contour perimeter
                    result = cv.cvApproxPoly(
                        contour, cv.sizeof_CvContour, self.squares_storage,
                        cv.CV_POLY_APPROX_DP,
                        cv.cvContourPerimeter(contours) * 0.02, 0)
                    # square contours should have 4 vertices after approximation
                    # relatively large area (to filter out noisy contours)
                    # and be convex.
                    # Note: absolute value of an area is used because
                    # area may be positive or negative - in accordance with the
                    # contour orientation
                    if (result.total == 4
                            and abs(cv.cvContourArea(result)) > 1000
                            and cv.cvCheckContourConvexity(result)):
                        s = 0
                        for i in range(5):
                            # find minimum angle between joint
                            # edges (maximum of cosine)
                            if (i >= 2):
                                t = abs(
                                    self.squares_angle(result[i],
                                                       result[i - 2],
                                                       result[i - 1]))
                                if s < t:
                                    s = t
                        # if cosines of all angles are small
                        # (all angles are ~90 degree) then write quandrange
                        # vertices to resultant sequence
                        if (s < 0.3):
                            for i in range(4):
                                squares.append(result[i])

        cv.cvReleaseImage(tgrey)
        return squares
Esempio n. 12
0
	def convert_to_gray(self,image):
		converted=cv.cvCreateImage(cv.cvGetSize(image),image.depth,1)
		cv.cvCvtColor(image,converted,cv.CV_RGB2GRAY)
		cv.cvReleaseImage(image)
		return converted
Esempio n. 13
0
	def convert_to_color(self,image):
		converted=cv.cvCreateImage(cv.cvGetSize(image),image.depth,3)
		cv.cvCvtColor(image,converted,cv.CV_GRAY2RGB)
		cv.cvReleaseImage(image)
		return converted
Esempio n. 14
0
            min_distance = 10

            # search the good points
            points[1] = cv.cvGoodFeaturesToTrack(grey, eig, temp, MAX_COUNT,
                                                 quality, min_distance, None,
                                                 3, 0, 0.04)

            # refine the corner locations
            cv.cvFindCornerSubPix(
                grey, points[1], cv.cvSize(win_size, win_size),
                cv.cvSize(-1, -1),
                cv.cvTermCriteria(cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20,
                                  0.03))

            # release the temporary images
            cv.cvReleaseImage(eig)
            cv.cvReleaseImage(temp)

        elif len(points[0]) > 0:
            # we have points, so display them

            # calculate the optical flow
            points[1], status = cv.cvCalcOpticalFlowPyrLK(
                prev_grey, grey, prev_pyramid, pyramid, points[0],
                len(points[0]), cv.cvSize(win_size, win_size), 3,
                len(points[0]), None,
                cv.cvTermCriteria(cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20,
                                  0.03), flags)

            # initializations
            point_counter = -1
            min_distance = 10

            # search the good points
            points[1] = cv.cvGoodFeaturesToTrack(grey, eig, temp, MAX_COUNT, quality, min_distance, None, 3, 0, 0.04)

            # refine the corner locations
            cv.cvFindCornerSubPix(
                grey,
                points[1],
                cv.cvSize(win_size, win_size),
                cv.cvSize(-1, -1),
                cv.cvTermCriteria(cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03),
            )

            # release the temporary images
            cv.cvReleaseImage(eig)
            cv.cvReleaseImage(temp)

        elif len(points[0]) > 0:
            # we have points, so display them

            # calculate the optical flow
            points[1], status = cv.cvCalcOpticalFlowPyrLK(
                prev_grey,
                grey,
                prev_pyramid,
                pyramid,
                points[0],
                len(points[0]),
                cv.cvSize(win_size, win_size),
                3,