Beispiel #1
0
 def set_frame(self,frame):
     #This function sets the picture to a desired frame size
     #self  - object of this class
     #frame - captured frame
     jpegPIL = Image.fromstring("RGB",(640,480),frame,"jpeg","RGB","raw")
     cv_im = cv2.createImage((640,480),cv.IPL_DEPTH_8U,3)
     cv2.setData(cv_im,jpegPIL.tostring())
     cv2.showImage(self.name,cv_im)
    def run(self):
        hist = cv2.createHist([180], cv2.CV_HIST_ARRAY, [(0,180)], 1 )
        backproject_mode = True
        
        while True:
            frame = cv2.QueryFrame( self.capture )

            # Convert to HSV and keep the hue
            hsv = cv2.createImage(cv2.GetSize(frame), 8, 3)
            cv2.cvtColor(frame, hsv, cv2.CV_BGR2HSV)
            self.hue = cv2.createImage(cv2.GetSize(frame), 8, 1)
            cv2.split(hsv, self.hue, None, None, None)

            # Compute back projection
            backproject = cv2.createImage(cv2.GetSize(frame), 8, 1)
            cv2.calcArrBackProject( [self.hue], backproject, hist )

            # Run the cam-shift (if the a window is set and != 0)
            if self.track_window and is_rect_nonzero(self.track_window):
                crit = ( cv2.CV_TERMCRIT_EPS | cv2.CV_TERMCRIT_ITER, 10, 1)
                (iters, (area, value, rect), track_box) = cv2.camShift(backproject, self.track_window, crit) #Call the camshift !!
                self.track_window = rect #Put the current rectangle as the tracked area


            # If mouse is pressed, highlight the current selected rectangle and recompute histogram
            if self.drag_start and is_rect_nonzero(self.selection):
                sub = cv2.getSubRect(frame, self.selection) #Get specified area
                
                #Make the effect of background shadow when selecting a window
                save = cv2.cloneMat(sub)
                cv2.convertScale(frame, frame, 0.5)
                cv2.copy(save, sub)
                
                #Draw temporary rectangle
                x,y,w,h = self.selection
                cv2.rectangle(frame, (x,y), (x+w,y+h), (255,255,255))

                #Take the same area but in hue image to calculate histogram
                sel = cv2.getSubRect(self.hue, self.selection ) 
                cv2.calcArrHist( [sel], hist, 0)
                
                #Used to rescale the histogram with the max value (to draw it later on)
                (_, max_val, _, _) = cv2.getMinMaxHistValue( hist)
                if max_val != 0:
                    cv2.convertScale(hist.bins, hist.bins, 255. / max_val) 
                    
            elif self.track_window and is_rect_nonzero(self.track_window): #If window set draw an elipseBox
                cv2.ellipseBox( frame, track_box, cv2.CV_RGB(255,0,0), 3, cv2.CV_AA, 0 )


            cv2.showImage( "CamShiftDemo", frame )
            cv2.showImage( "Backprojection", backproject)
            cv2.showImage( "Histogram", self.hue_histogram_as_image(hist))

            c = cv2.waitKey(7) % 0x100
            if c == 27:
                break
Beispiel #3
0
    def invoke(self, arg, from_tty):
        args = gdb.string_to_argv(arg)

        # generally, we type "plot someimage" in the GDB commandline
        # where "someimage" is an instance of cv::Mat
        v = gdb.parse_and_eval(args[0])

        # the value v is a gdb.Value object of C++
        # code's cv::Mat, we need to translate to
        # a python object under cv2.cv
        image_size = (v['cols'], v['rows'])
        # print v
        # these two below lines do not work. I don't know why
        # channel = gdb.execute("call "+ args[0] + ".channels()", False, True)
        # channel = v.channels();
        CV_8U = 0
        CV_8S = 1
        CV_16U = 2
        CV_16S = 3
        CV_32S = 4
        CV_32F = 5
        CV_64F = 6
        CV_USRTYPE1 = 7
        CV_CN_MAX = 512
        CV_CN_SHIFT = 3
        CV_MAT_CN_MASK = (CV_CN_MAX - 1) << CV_CN_SHIFT
        flags = v['flags']
        channel = (((flags) & CV_MAT_CN_MASK) >> CV_CN_SHIFT) + 1
        CV_DEPTH_MAX = (1 << CV_CN_SHIFT)
        CV_MAT_DEPTH_MASK = CV_DEPTH_MAX - 1
        depth = (flags) & CV_MAT_DEPTH_MASK
        IPL_DEPTH_SIGN = 0x80000000
        cv_elem_size = (((4 << 28) | 0x8442211) >> depth * 4) & 15
        if (depth == CV_8S or depth == CV_16S or depth == CV_32S):
            mask = IPL_DEPTH_SIGN
        else:
            mask = 0
        ipl_depth = cv_elem_size * 8 | mask
        img = cv2.createImageHeader(image_size, ipl_depth, channel)

        # conver the v['data'] type to "char*" type
        char_type = gdb.lookup_type("char")
        char_pointer_type = char_type.pointer()
        buffer = v['data'].cast(char_pointer_type)

        # read bytes from inferior's memory, because
        # we run the opencv-python module in GDB's own process
        # otherwise, we use memory corss processes
        buf = v['step']['buf']
        bytes = buf[0] * v['rows']  # buf[0] is the step? Not quite sure.
        inferior = gdb.selected_inferior()
        mem = inferior.read_memory(buffer, bytes)

        # set the img's raw data
        cv2.setData(img, mem)

        # create a window, and show the image
        cv2.startWindowThread()
        cv2.namedWindow('viewer')
        cv2.showImage('viewer', img)

        # the below statement is necessory, otherwise, the Window
        # will hang
        cv2.waitKey(0)
        cv2.destroyWindow('viewer')