Exemplo n.º 1
0
	def timerEvent(self, ev):
		# Fetch a frame from the video camera
		frame									= highgui.cvQueryFrame(self.cap)
		img_orig								= cv.cvCreateImage(cv.cvSize(frame.width,frame.height),cv.IPL_DEPTH_8U, frame.nChannels)
		if (frame.origin == cv.IPL_ORIGIN_TL):
			cv.cvCopy(frame, img_orig)
		else:
			cv.cvFlip(frame, img_orig, 0)

		# Create a grey frame to clarify data
		img_grey								= cv.cvCreateImage(cv.cvSize(img_orig.width,img_orig.height), 8, 1)
		cv.cvCvtColor(img_orig, img_grey, cv.CV_BGR2GRAY)
		# Detect objects within the frame
		self.faces_storage						= cv.cvCreateMemStorage(0)
		faces									= self.detect_faces(img_grey)
		self.circles_storage					= cv.cvCreateMemStorage(0)
		circles									= self.detect_circles(img_grey)
		self.squares_storage					= cv.cvCreateMemStorage(0)
		squares									= self.detect_squares(img_grey, img_orig)
		self.lines_storage						= cv.cvCreateMemStorage(0)
		lines									= self.detect_lines(img_grey, img_orig)

		# Draw faces
		if faces:
			for face in faces:
				pt1, pt2						= self.face_points(face)
				cv.cvRectangle(img_orig, pt1, pt2, cv.CV_RGB(255,0,0), 3, 8, 0)

		# Draw lines
		if lines:
			for line in lines:
				cv.cvLine(img_orig, line[0], line[1], cv.CV_RGB(255,255,0), 3, 8)
		# Draw circles
		if circles:
			for circle in circles:
				cv.cvCircle(img_orig, cv.cvPoint(cv.cvRound(circle[0]),cv.cvRound(circle[1])),cv.cvRound(circle[2]),cv.CV_RGB(0,0,255),3,8,0)

		# Draw squares
		if squares:
			i									= 0
			while i<squares.total:
					pt							= []
					# read 4 vertices
					pt.append(squares[i])
					pt.append(squares[i+1])
					pt.append(squares[i+2])
					pt.append(squares[i+3])
					## draw the square as a closed polyline
					cv.cvPolyLine(img_orig, [pt], 1, cv.CV_RGB(0,255,0), 3, cv.CV_AA, 0)
					i							+= 4

		# Resize the image to display properly within the window
		#	CV_INTER_NN - nearest-neigbor interpolation, 
		#	CV_INTER_LINEAR - bilinear interpolation (used by default) 
		#	CV_INTER_AREA - resampling using pixel area relation. (preferred for image decimation)
		#	CV_INTER_CUBIC - bicubic interpolation.
		img_display								= cv.cvCreateImage(cv.cvSize(self.width(),self.height()), 8, 3)
		cv.cvResize(img_orig, img_display, cv.CV_INTER_NN)
		img_pil									= adaptors.Ipl2PIL(img_display)
		s										= StringIO()
		img_pil.save(s, "PNG")
		s.seek(0)
		q_img									= QImage()
		q_img.loadFromData(s.read())
		bitBlt(self, 0, 0, q_img)
Exemplo n.º 2
0
    def detect_squares(self, img):
        """ Find squares within the video stream and draw them """
        N = 11
        thresh = 5
        sz = cv.cvSize(img.width & -2, img.height & -2)
        timg = cv.cvCloneImage(img)
        gray = cv.cvCreateImage(sz, 8, 1)
        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.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))

        # down-scale and upscale the image to filter out the noise
        cv.cvPyrDown(subimage, pyr, 7)
        cv.cvPyrUp(pyr, subimage, 7)
        tgray = 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] = tgray
            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(tgray, gray, 0, thresh, 5)
                    # dilate canny output to remove potential
                    # holes between edge segments
                    cv.cvDilate(gray, gray, None, 1)
                else:
                    # apply threshold if l!=0:
                    #     tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
                    cv.cvThreshold(tgray, gray, (l + 1) * 255 / N, 255,
                                   cv.CV_THRESH_BINARY)

                # find contours and store them all as a list
                count, contours = cv.cvFindContours(gray, self.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.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])

        i = 0
        while i < squares.total:
            pt = []
            # read 4 vertices
            pt.append(squares[i])
            pt.append(squares[i + 1])
            pt.append(squares[i + 2])
            pt.append(squares[i + 3])

            # draw the square as a closed polyline
            cv.cvPolyLine(img, [pt], 1, cv.CV_RGB(0, 255, 0), 3, cv.CV_AA, 0)
            i += 4

        return img
Exemplo n.º 3
0
    def timerEvent(self, ev):
        # Fetch a frame from the video camera
        frame = highgui.cvQueryFrame(self.cap)
        img_orig = cv.cvCreateImage(cv.cvSize(frame.width, frame.height),
                                    cv.IPL_DEPTH_8U, frame.nChannels)
        if (frame.origin == cv.IPL_ORIGIN_TL):
            cv.cvCopy(frame, img_orig)
        else:
            cv.cvFlip(frame, img_orig, 0)

        # Create a grey frame to clarify data
        img_grey = cv.cvCreateImage(cv.cvSize(img_orig.width, img_orig.height),
                                    8, 1)
        cv.cvCvtColor(img_orig, img_grey, cv.CV_BGR2GRAY)
        # Detect objects within the frame
        self.faces_storage = cv.cvCreateMemStorage(0)
        faces = self.detect_faces(img_grey)
        self.circles_storage = cv.cvCreateMemStorage(0)
        circles = self.detect_circles(img_grey)
        self.squares_storage = cv.cvCreateMemStorage(0)
        squares = self.detect_squares(img_grey, img_orig)
        self.lines_storage = cv.cvCreateMemStorage(0)
        lines = self.detect_lines(img_grey, img_orig)

        # Draw faces
        if faces:
            for face in faces:
                pt1, pt2 = self.face_points(face)
                cv.cvRectangle(img_orig, pt1, pt2, cv.CV_RGB(255, 0, 0), 3, 8,
                               0)

        # Draw lines
        if lines:
            for line in lines:
                cv.cvLine(img_orig, line[0], line[1], cv.CV_RGB(255, 255, 0),
                          3, 8)
        # Draw circles
        if circles:
            for circle in circles:
                cv.cvCircle(
                    img_orig,
                    cv.cvPoint(cv.cvRound(circle[0]), cv.cvRound(circle[1])),
                    cv.cvRound(circle[2]), cv.CV_RGB(0, 0, 255), 3, 8, 0)

        # Draw squares
        if squares:
            i = 0
            while i < squares.total:
                pt = []
                # read 4 vertices
                pt.append(squares[i])
                pt.append(squares[i + 1])
                pt.append(squares[i + 2])
                pt.append(squares[i + 3])
                ## draw the square as a closed polyline
                cv.cvPolyLine(img_orig, [pt], 1, cv.CV_RGB(0, 255, 0), 3,
                              cv.CV_AA, 0)
                i += 4

        # Resize the image to display properly within the window
        #	CV_INTER_NN - nearest-neigbor interpolation,
        #	CV_INTER_LINEAR - bilinear interpolation (used by default)
        #	CV_INTER_AREA - resampling using pixel area relation. (preferred for image decimation)
        #	CV_INTER_CUBIC - bicubic interpolation.
        img_display = cv.cvCreateImage(cv.cvSize(self.width(), self.height()),
                                       8, 3)
        cv.cvResize(img_orig, img_display, cv.CV_INTER_NN)
        img_pil = adaptors.Ipl2PIL(img_display)
        s = StringIO()
        img_pil.save(s, "PNG")
        s.seek(0)
        q_img = QImage()
        q_img.loadFromData(s.read())
        bitBlt(self, 0, 0, q_img)
Exemplo n.º 4
0
    # init the list of polylines
    nb_polylines = 2
    polylines_size = 3
    pt = [0,] * nb_polylines
    for a in range (nb_polylines):
        pt [a] = [0,] * polylines_size

    # draw some polylines
    for i in range (number):
        for a in range (nb_polylines):
            for b in range (polylines_size):
                pt [a][b] = cv.cvPoint (random.randrange (-width, 2 * width),
                                     random.randrange (-height, 2 * height))
        cv.cvPolyLine (image, pt, 1,
                       random_color (random),
                       random.randrange (1, 9),
                       line_type, 0)

        highgui.cvShowImage (window_name, image)
        highgui.cvWaitKey (delay)

    # draw some filled polylines
    for i in range (number):
        for a in range (nb_polylines):
            for b in range (polylines_size):
                pt [a][b] = cv.cvPoint (random.randrange (-width, 2 * width),
                                     random.randrange (-height, 2 * height))
        cv.cvFillPoly (image, pt,
                       random_color (random),
                       line_type, 0)
Exemplo n.º 5
0
	def detect_squares(self, img):
		""" Find squares within the video stream and draw them """
		N										= 11
		thresh									= 5
		sz										= cv.cvSize(img.width & -2, img.height & -2)
		timg									= cv.cvCloneImage(img)
		gray									= cv.cvCreateImage(sz, 8, 1)
		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.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))

		# down-scale and upscale the image to filter out the noise
		cv.cvPyrDown(subimage, pyr, 7)
		cv.cvPyrUp(pyr, subimage, 7)
		tgray									= 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]							= tgray
			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(tgray, gray, 0, thresh, 5)
					# dilate canny output to remove potential
					# holes between edge segments
					cv.cvDilate(gray, gray, None, 1)
				else:
					# apply threshold if l!=0:
					#     tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
					cv.cvThreshold(tgray, gray, (l+1)*255/N, 255, cv.CV_THRESH_BINARY)

				# find contours and store them all as a list
				count, contours					= cv.cvFindContours(gray,
																	self.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.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])

		i										= 0
		while i<squares.total:
			pt									= []
			# read 4 vertices
			pt.append(squares[i])
			pt.append(squares[i+1])
			pt.append(squares[i+2])
			pt.append(squares[i+3])

			# draw the square as a closed polyline
			cv.cvPolyLine(img, [pt], 1, cv.CV_RGB(0,255,0), 3, cv.CV_AA, 0)
			i									+= 4

		return img
Exemplo n.º 6
0
    # init the list of polylines
    nb_polylines = 2
    polylines_size = 3
    pt = [0,] * nb_polylines
    for a in range (nb_polylines):
        pt [a] = [0,] * polylines_size

    # draw some polylines
    for i in range (number):
        for a in range (nb_polylines):
            for b in range (polylines_size):
                pt [a][b] = cv.cvPoint (random.randrange (-width, 2 * width),
                                     random.randrange (-height, 2 * height))
        cv.cvPolyLine (image, pt, 1,
                       random_color (random),
                       random.randrange (1, 9),
                       line_type, 0)

        highgui.cvShowImage (window_name, image)
        highgui.cvWaitKey (delay)

    # draw some filled polylines
    for i in range (number):
        for a in range (nb_polylines):
            for b in range (polylines_size):
                pt [a][b] = cv.cvPoint (random.randrange (-width, 2 * width),
                                     random.randrange (-height, 2 * height))
        cv.cvFillPoly (image, pt,
                       random_color (random),
                       line_type, 0)