示例#1
0
    def _mixImageSelfMaskHue(self, wipeSettings, level, image1, image2, mixMat):
        cv.CvtColor(image1, mixMat, cv.CV_RGB2HSV)
        cv.Split(mixMat, self._mixMixMask1, None, None, None)
        cv.CvtColor(image2, mixMat, cv.CV_RGB2HSV)
        cv.Split(mixMat, self._mixMixMask2, None, None, None)
        cv.Sub(self._mixMixMask2, self._mixMixMask1, self._mixImageMask)

        cv.CmpS(self._mixImageMask, 255-int((level*254)), self._mixImageMask, cv.CV_CMP_GT)
        return self._mixImageAlphaMask(wipeSettings, level, image1, image2, self._mixImageMask, mixMat)
示例#2
0
def hs_histogram(src):
    # Convert to HSV
    hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
    cv.CvtColor(src, hsv, cv.CV_BGR2HSV)

    # Extract the H and S planes
    h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
    s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
    cv.Split(hsv, h_plane, s_plane, None, None)
    planes = [h_plane, s_plane]

    h_bins = 30
    s_bins = 32
    hist_size = [h_bins, s_bins]
    # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */
    h_ranges = [0, 180]
    # saturation varies from 0 (black-gray-white) to
    # 255 (pure spectrum color)
    s_ranges = [0, 255]
    ranges = [h_ranges, s_ranges]
    scale = 10
    hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1)
    cv.CalcHist([cv.GetImage(i) for i in planes], hist)
    (_, max_value, _, _) = cv.GetMinMaxHistValue(hist)

    hist_img = cv.CreateImage((h_bins * scale, s_bins * scale), 8, 3)

    for h in range(h_bins):
        for s in range(s_bins):
            bin_val = cv.QueryHistValue_2D(hist, h, s)
            intensity = cv.Round(bin_val * 255 / max_value)
            cv.Rectangle(hist_img, (h * scale, s * scale),
                         ((h + 1) * scale - 1, (s + 1) * scale - 1),
                         cv.RGB(intensity, intensity, intensity), cv.CV_FILLED)
    return hist_img
示例#3
0
 def get_separated_channels(self):
     ''' Split the channels of an image '''
     b = cv.CreateImage(cv.GetSize(self.image), self.image.depth, 1)
     g = cv.CloneImage(b)
     r = cv.CloneImage(b)
     cv.Split(self.image, b, g, r, None)
     return [b, g, r]
def crack(tocrack):
    im = cv.CreateImage(cv.GetSize(tocrack), 8, 1)
    cv.Split(tocrack, None, None, im, None)
    cv.Threshold(im, im, 250, 255, cv.CV_THRESH_BINARY)

    txt = pytesser.iplimage_to_string(im)
    return txt[:-2]
示例#5
0
def createHist(img):
    #cv.CvtColor(img,img,cv.CV_BGR2HSV)
    b_plane = cv.CreateImage((img.width,img.height), 8, 1)
    g_plane = cv.CreateImage((img.width,img.height), 8, 1)
    r_plane = cv.CreateImage((img.width,img.height), 8, 1)
 
 
    cv.Split(img,b_plane,g_plane,r_plane,None)
    planes = [b_plane, g_plane, r_plane]
    
    bins = 4
    b_bins = bins
    g_bins = bins
    r_bins = bins
 
    hist_size = [b_bins,g_bins,r_bins]
    b_range = [0,255]
    g_range = [0,255]
    r_range = [0,255]
 
    ranges = [b_range,g_range,r_range]
    hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, ranges, 1)
    cv.CalcHist([cv.GetImage(i) for i in planes], hist)
    cv.NormalizeHist(hist,1)
    return hist
示例#6
0
    def run(self):
        hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1)
        backproject_mode = True

        while True:
            frame = cv.QueryFrame(self.capture)

            # Convert to HSV and keep the hue
            hsv = cv.CreateImage(cv.GetSize(frame), 8, 3)
            cv.CvtColor(frame, hsv, cv.CV_BGR2HSV)
            self.hue = cv.CreateImage(cv.GetSize(frame), 8, 1)
            cv.Split(hsv, self.hue, None, None, None)

            # Compute back projection
            backproject = cv.CreateImage(cv.GetSize(frame), 8, 1)
            cv.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 = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1)
                (iters, (area, value, rect),
                 track_box) = cv.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 = cv.GetSubRect(frame, self.selection)  #Get specified area

                #Make the effect of background shadow when selecting a window
                save = cv.CloneMat(sub)
                cv.ConvertScale(frame, frame, 0.5)
                cv.Copy(save, sub)

                #Draw temporary rectangle
                x, y, w, h = self.selection
                cv.Rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255))

                #Take the same area but in hue image to calculate histogram
                sel = cv.GetSubRect(self.hue, self.selection)
                cv.CalcArrHist([sel], hist, 0)

                #Used to rescale the histogram with the max value (to draw it later on)
                (_, max_val, _, _) = cv.GetMinMaxHistValue(hist)
                if max_val != 0:
                    cv.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
                cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3,
                              cv.CV_AA, 0)

            cv.ShowImage("CamShiftDemo", frame)
            cv.ShowImage("Backprojection", backproject)
            cv.ShowImage("Histogram", self.hue_histogram_as_image(hist))

            c = cv.WaitKey(7) % 0x100
            if c == 27:
                break
    def run(self):
        hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1)
        backproject_mode = False
        while True:
            frame = 0
            frame = self.capture  #cv.QueryFrame( self.capture )

            # Convert to HSV and keep the hue
            hsv = cv.CreateImage(cv.GetSize(frame), 8, 3)
            cv.CvtColor(frame, hsv, cv.CV_BGR2HSV)
            self.hue = cv.CreateImage(cv.GetSize(frame), 8, 1)
            cv.Split(hsv, self.hue, None, None, None)

            # Compute back projection
            backproject = cv.CreateImage(cv.GetSize(frame), 8, 1)

            # Run the cam-shift
            cv.CalcArrBackProject([self.hue], backproject, hist)
            #             if self.track_window and is_rect_nonzero(self.track_window):
            #                 crit = ( cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1)
            #                 (iters, (area, value, rect), track_box) = cv.CamShift(backproject, self.track_window, crit)
            #                 self.track_window = rect

            # If mouse is pressed, highlight the current selected rectangle
            # and recompute the histogram

            if self.drag_start and is_rect_nonzero(self.selection):
                sub = cv.GetSubRect(frame, self.selection)
                save = cv.CloneMat(sub)
                #cv.ConvertScale(frame, frame, 0.5)
                cv.Copy(save, sub)
                x, y, w, h = self.selection
                cv.Rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255))

                sel = cv.GetSubRect(self.hue, self.selection)
                cv.CalcArrHist([sel], hist, 0)
                (_, max_val, _, _) = cv.GetMinMaxHistValue(hist)
                if max_val != 0:
                    cv.ConvertScale(hist.bins, hist.bins, 255. / max_val)


#             elif self.track_window and is_rect_nonzero(self.track_window):
#                 cv.EllipseBox( frame, track_box, cv.CV_RGB(255,0,0), 3, cv.CV_AA, 0 )

            if not backproject_mode:
                cv.ShowImage("SelectROI", frame)
            else:
                cv.ShowImage("SelectROI", backproject)
            cv.ShowImage("Histogram", self.hue_histogram_as_image(hist))

            c = cv.WaitKey(7) % 0x100
            if c == 27:
                f = open('newtree.yaml', "w")
                yaml.dump(self.selection, f)
                f.close()
                break
            elif c == ord("b"):
                backproject_mode = not backproject_mode
示例#8
0
def Canny(image, low_thr=50, hi_thr=150):
    # PERFETTO: le pupille (a distanza ravvicinata) sembrano essere
    # prese bene... TODO: smanettare con i parametri di canny
    yuv = cv.CreateImage(cv.GetSize(image), 8, 3)
    gray = cv.CreateImage(cv.GetSize(image), 8, 1)
    cv.CvtColor(image, yuv, cv.CV_BGR2YCrCb)
    cv.Split(yuv, gray, None, None, None)
    canny = cv.CreateImage(cv.GetSize(image), 8, 1)
    cv.Canny(gray, canny, low_thr, hi_thr)
    cv.NamedWindow('Canny')
    cv.ShowImage('Canny', canny)
示例#9
0
 def im_to_lsb(self):
     b = cv.CreateImage(cv.GetSize(self.image), self.image.depth, 1)
     g = cv.CloneImage(b)
     r = cv.CloneImage(b)
     cv.Split(self.image, b, g, r, None)
     for j in range(self.image.height):
         for i in range(self.image.width):
             pixb, pixg, pixr = self.image[j, i]
             b[j, i] = 255 if int(pixb) & 1 else 0
             g[j, i] = 255 if int(pixg) & 1 else 0
             r[j, i] = 255 if int(pixr) & 1 else 0
     return [b, g, r]
示例#10
0
    def detect_and_draw(self, imgmsg):
        if self.pause:
            return
        # frame = cv.QueryFrame( self.capture )
        frame = self.br.imgmsg_to_cv(imgmsg, "bgr8")

        # Convert to HSV and keep the hue
        hsv = cv.CreateImage(cv.GetSize(frame), 8, 3)
        cv.CvtColor(frame, hsv, cv.CV_BGR2HSV)
        self.hue = cv.CreateImage(cv.GetSize(frame), 8, 1)
        cv.Split(hsv, self.hue, None, None, None)

        # Compute back projection
        backproject = cv.CreateImage(cv.GetSize(frame), 8, 1)

        # Run the cam-shift
        cv.CalcArrBackProject([self.hue], backproject, self.hist)
        if self.track_window and is_rect_nonzero(self.track_window):
            crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1)
            (iters, (area, value, rect),
             track_box) = cv.CamShift(backproject, self.track_window, crit)
            self.track_window = rect
            x, y, w, h = rect
            self.bbpub.publish(RegionOfInterest(x, y, w, h, False))
            proba_msg = self.br.cv_to_imgmsg(backproject)
            proba_msg.header = imgmsg.header
            self.bppub.publish(proba_msg)

        # If mouse is pressed, highlight the current selected rectangle
        # and recompute the histogram

        if self.drag_start and is_rect_nonzero(self.selection):
            sub = cv.GetSubRect(frame, self.selection)
            save = cv.CloneMat(sub)
            cv.ConvertScale(frame, frame, 0.5)
            cv.Copy(save, sub)
            x, y, w, h = self.selection
            cv.Rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255))

            sel = cv.GetSubRect(self.hue, self.selection)
            cv.CalcArrHist([sel], self.hist, 0)
            (_, max_val, _, _) = cv.GetMinMaxHistValue(self.hist)
            if max_val != 0:
                cv.ConvertScale(self.hist.bins, self.hist.bins, 255. / max_val)
        elif self.track_window and is_rect_nonzero(self.track_window):
            cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3, cv.CV_AA,
                          0)

        self.frame = frame
        self.backproject = backproject
示例#11
0
def camshift(x, y, w, h, selection):
    print "Performing camshift with x:{} y:{} w:{} h:{}".format(x, y, w, h)
    print selection
    hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1)

    while True:
        print "entered loop"
        #camshift termination criteria (10 iterations without movement of 1 pixel ends camshift)

        frame = cv.QueryFrame(cap)
        cv.Flip(frame, frame, 1)

        #print "switching to HSV"
        hsv = cv.CreateImage(cv.GetSize(frame), 8, 3)
        cv.CvtColor(frame, hsv, cv.CV_BGR2HSV)
        hue = cv.CreateImage(cv.GetSize(frame), 8, 1)
        cv.Split(hsv, hue, None, None, None)

        #compute back projection
        # print "back projection"
        backproject = cv.CreateImage(cv.GetSize(frame), 8, 1)
        cv.CalcArrBackProject([hue], backproject, hist)

        #run the camshift
        #print "camshift"
        print "Selection"
        #pdb.set_trace()
        print selection
        crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1)
        (iters, (area, value, rect),
         track_box) = cv.CamShift(backproject, selection, crit)
        print "rect"
        print rect
        if rect[0] > 0 and rect[1] > 0:
            selection = rect
        print "SelectionNew"
        print selection
        print "track_box"
        print track_box

        #draw the surrounding ellipse
        # print "ellipse"
        cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3, cv.CV_AA, 0)

        #draw image
        #print "drawing image"
        cv.ShowImage("CamShift", frame)
        if cv.WaitKey(1) & 0xFF == ord('q'):
            break
示例#12
0
def detect_and_draw(img, cascade):
    # allocate temporary images
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / image_scale)), 8, 1)

    # convert color input image to grayscale
    cv.CvtColor(img, gray, cv.CV_BGR2GRAY)

    # scale input image for faster processing
    cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)

    #cv.EqualizeHist(small_img, small_img)
    image = img
    cv.SetImageCOI(image, 1)
    cv.SetImageCOI(image, 0)

    hsv = cv.CreateImage(cv.GetSize(image), 8, 3)
    h_plane = cv.CreateImage(cv.GetSize(image), 8, 1)
    s_plane = cv.CreateImage(cv.GetSize(image), 8, 1)
    cv.CvtColor(image, hsv, cv.CV_RGB2HSV)
    cv.Split(hsv, h_plane, s_plane, None, None)
    #img=s_plane;

    mergi(0, 0)
    if (cascade):
        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, cascade,
                                     cv.CreateMemStorage(0), haar_scale,
                                     min_neighbors, haar_flags, min_size)
        t = cv.GetTickCount() - t
        print "detection time = %gms" % (t / (cv.GetTickFrequency() * 1000.))
        if faces:
            mergi(-100, -100)
            mergi(-100, 100)
            for ((x, y, w, h), n) in faces:
                # the input to cv.HaarDetectObjects was resized, so scale the
                # bounding box of each face and convert it to two CvPoints
                pt1 = (int(x * image_scale), int(y * image_scale))
                pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
                cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)


#        else
#            mergi(0,0)

    cv.ShowImage("result", img)
示例#13
0
def histogram(src):
    # Set ccd sampling region.
    #     cv.SetImageROI(src, (10, 10, 100, 100))

    # Convert to HSV
    hsv = cv.CreateImage(cv.GetSize(src), 8, 3)

    cv.CvtColor(src, hsv, cv.CV_BGR2HSV)
    s_plane = cv.CreateMat(cv.GetSize(src)[1], cv.GetSize(src)[0], cv.CV_8UC1)
    h_plane = cv.CreateMat(cv.GetSize(src)[1], cv.GetSize(src)[0], cv.CV_8UC1)

    cv.Split(hsv, h_plane, s_plane, None, None)
    planes = [h_plane, s_plane]

    h_bins = 28
    s_bins = 5
    hist_size = [h_bins, s_bins]
    # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */
    h_ranges = [0, 180]
    # saturation varies from 0 (black-gray-white) to
    # 255 (pure spectrum color)
    s_ranges = [0, 255]
    ranges = [h_ranges, s_ranges]
    scale = 15

    # calculate histogram
    hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1)
    cv.CalcHist([cv.GetImage(i) for i in planes], hist)
    (_, max_value, _, _) = cv.GetMinMaxHistValue(hist)

    # Reset cv sampling region to full CCD Area
    #     cv.ResetImageROI(src)

    # plot histogram data
    hist_img = cv.CreateImage((h_bins * scale, s_bins * scale), 8, 3)

    for h in range(h_bins):
        for s in range(s_bins):
            bin_val = cv.QueryHistValue_2D(hist, h, s)
            intensity = cv.Round(bin_val * 255 / max_value)
            cv.Rectangle(hist_img, (h * scale, s * scale),
                         ((h + 1) * scale - 1, (s + 1) * scale - 1),
                         cv.RGB(intensity, intensity, intensity), cv.CV_FILLED)
    return hist_img
示例#14
0
def findImage(img):
    #Set up storage for images
    frame_size = cv.GetSize(img)
    img2 = cv.CreateImage(frame_size,8,3)
    tmp = cv.CreateImage(frame_size,8,cv.CV_8U)
    h = cv.CreateImage(frame_size,8,1)

    #copy original image to do work on
    cv.Copy(img,img2)

    #altering the image a bit for smoother processing
    cv.Smooth(img2,img2,cv.CV_BLUR,3)
    cv.CvtColor(img2,img2,cv.CV_BGR2HSV)

    #make sure temp is empty
    cv.Zero(tmp)

    #detection based on HSV value
    #30,100,90 lower limit on pic 41,255,255 on pic
    #cv.InRangeS(img2,cv.Scalar(25,100,87),cv.Scalar(50,255,255),tmp)
    #Range for green plate dot in my Living room
    #cv.InRangeS(img2,cv.Scalar(55,80,60),cv.Scalar(65,95,90),tmp)
    #classroom
    #cv.InRangeS(img2,cv.Scalar(55,80,60),cv.Scalar(70,110,70),tmp)
    #Kutztowns Gym
    cv.InRangeS(img2,cv.Scalar(65,100,112),cv.Scalar(85,107,143),tmp)

    elmt_shape=cv.CV_SHAPE_ELLIPSE
    pos = 3
    element = cv.CreateStructuringElementEx(pos*2+1, pos*2+1, pos, pos, elmt_shape)
    cv.Dilate(tmp,tmp,element,6)
    cv.Erode(tmp,tmp,element,2)

    cv.Split(tmp,h,None,None,None)
    storage = cv.CreateMemStorage()

    scan = sc.FindContours(h,storage)
    xyImage=drawCircles(scan,img)

    if xyImage != None:
            return (xyImage,tmp)
    else:
            return None
示例#15
0
文件: liner.py 项目: jgrip/xystitch
def hs_histogram(src, mask=None):
    '''Takes a cvMat and computes a hue-saturation histogram (value is dropped)'''
    # Convert to HSV
    # Allocate the 3 HSV 8 bit channels
    hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
    # Convert from the default BGR representation to HSV
    cv.CvtColor(src, hsv, cv.CV_BGR2HSV)

    # Extract the H and S planes
    h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
    s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
    v_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
    # Copy out omitting the values we don't want
    cv.Split(hsv, h_plane, s_plane, v_plane, None)
    planes = [h_plane, s_plane]

    if 0:
        pplane('H plane', h_plane, prefix='    ')
        pplane('S plane', s_plane, prefix='    ')

    h_bins = 8
    s_bins = 8
    #hist_size = [h_bins, s_bins]
    # hue varies from 0 (~0 deg red) to 180 (~360 deg red again
    # ??? My values give a hue of 0-255
    h_ranges = [0, 255]
    # saturation varies from 0 (black-gray-white) to
    # 255 (pure spectrum color)
    s_ranges = [0, 255]
    ranges = [h_ranges, s_ranges]
    # Allocate bins
    # note that we haven't put any data in yet
    #1: uniform
    hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1)
    # Convert the array planes back into images since thats what CalcHist needs?
    # Doc seems to indcate that cvMat will work as well
    # Doesn't this cross the hue and saturate values together?  That doesn't seem to make sense, like comparing red to blue
    # Guess its a 2D histogram so it deals with where they overlap
    cv.CalcHist([cv.GetImage(i) for i in planes], hist, 0, mask)
    cv.NormalizeHist(hist, 1.0)

    return hist
示例#16
0
    def run(self):
        hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1)
        backproject_mode = False
        i = 1
        o_x = 0
        o_y = 0
        while True:
            frame = cv.QueryFrame(self.capture)

            # Convert to HSV and keep the hue
            hsv = cv.CreateImage(cv.GetSize(frame), 8, 3)
            cv.CvtColor(frame, hsv, cv.CV_BGR2HSV)
            self.hue = cv.CreateImage(cv.GetSize(frame), 8, 1)
            cv.Split(hsv, self.hue, None, None, None)

            # Compute back projection
            backproject = cv.CreateImage(cv.GetSize(frame), 8, 1)

            # Run the cam-shift
            cv.CalcArrBackProject([self.hue], backproject, hist)
            if self.track_window and is_rect_nonzero(self.track_window):
                crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1)
                (iters, (area, value, rect),
                 track_box) = cv.CamShift(backproject, self.track_window, crit)
                self.track_window = rect

            # If mouse is pressed, highlight the current selected rectangle
            # and recompute the histogram

            if self.drag_start and is_rect_nonzero(self.selection):
                sub = cv.GetSubRect(frame, self.selection)
                save = cv.CloneMat(sub)
                cv.ConvertScale(frame, frame, 0.5)
                cv.Copy(save, sub)
                x, y, w, h = self.selection
                cv.Rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255))

                sel = cv.GetSubRect(self.hue, self.selection)
                cv.CalcArrHist([sel], hist, 0)
                (_, max_val, _, _) = cv.GetMinMaxHistValue(hist)
                if max_val != 0:
                    cv.ConvertScale(hist.bins, hist.bins, 255. / max_val)
            elif self.track_window and is_rect_nonzero(self.track_window):
                cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3,
                              cv.CV_AA, 0)
                #print track_box
                trace_val = track_box[0]
                f_x = trace_val[0]
                f_y = trace_val[1]
                print 'value1', f_x
                print 'value2', f_y
                if i % 10 == 0:
                    o_x = f_x
                    o_y = f_y
                if (f_x != o_x):
                    a = (f_x - o_x) / float(10)
                    round(a)
                    cam.Azimuth(-a)
                if (f_y != o_y):
                    a = (f_y - o_y) / float(10)
                    round(a)
                    cam.Elevation(-a)
                ren1.ResetCameraClippingRange()
                renWin.Render()
                i += 1

            if not backproject_mode:
                cv.ShowImage("CamShiftDemo", frame)
            else:
                cv.ShowImage("CamShiftDemo", backproject)
            cv.ShowImage("Histogram", self.hue_histogram_as_image(hist))

            c = cv.WaitKey(7) % 0x100
            if c == 27:
                break
            elif c == ord("b"):
                backproject_mode = not backproject_mode
示例#17
0
dst = cv.CreateImage(cv.GetSize(gray), cv.IPL_DEPTH_32F, 1)
cv.Laplace(gray, dst, aperture)

cv.Convert(dst, gray)

thresholded = cv.CloneImage(im)
cv.Threshold(im, thresholded, 50, 255, cv.CV_THRESH_BINARY_INV)

cv.ShowImage('Laplaced grayscale', gray)
cv2.imwrite('~/Users/horace/Documents/TensorFlow/linedetection.jpg', gray)
cv.WaitKey(0)
#------------------------------------

# Laplace on color
planes = [cv.CreateImage(cv.GetSize(im), 8, 1) for i in range(3)]
laplace = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_16S, 1)
colorlaplace = cv.CreateImage(cv.GetSize(im), 8, 3)

cv.Split(im, planes[0], planes[1], planes[2],
         None)  #Split channels to apply laplace on each
for plane in planes:
    cv.Laplace(plane, laplace, 3)
    cv.ConvertScaleAbs(laplace, plane, 1, 0)

cv.Merge(planes[0], planes[1], planes[2], None, colorlaplace)

cv.ShowImage('Laplace Color', colorlaplace)
#-------------------------------------

cv.WaitKey(0)
示例#18
0
def detectObject(filename):
    img=cv.LoadImage(filename)
    '''
    #get color histogram
    '''
   
#     im32f=np.zeros((img.shape[:2]),np.uint32)
    hist_range=[[0,256],[0,256],[0,256]]
    im32f=cv.CreateImage(cv.GetSize(img), cv2.IPL_DEPTH_32F, 3)
    cv.ConvertScale(img, im32f)
    
    
    hist=cv.CreateHist([32,32,32],cv.CV_HIST_ARRAY,hist_range,3)
    '''
    #create three histogram'''
    b=cv.CreateImage(cv.GetSize(im32f), cv2.IPL_DEPTH_32F, 1)
    g=cv.CreateImage(cv.GetSize(im32f), cv2.IPL_DEPTH_32F, 1)
    r=cv.CreateImage(cv.GetSize(im32f), cv2.IPL_DEPTH_32F, 1)
    
   
    '''
    #create image backproject 32f, 8u
    '''
    backproject32f=cv.CreateImage(cv.GetSize(img),cv2.IPL_DEPTH_32F,1)
    backproject8u=cv.CreateImage(cv.GetSize(img),cv2.IPL_DEPTH_8U,1)
    '''
    #create binary
    '''
    bw=cv.CreateImage(cv.GetSize(img),cv2.IPL_DEPTH_8U,1)
    '''
    #create kernel image
    '''
    kernel=cv.CreateStructuringElementEx(3, 3, 1, 1, cv2.MORPH_ELLIPSE)
    cv.Split(im32f, b, g, r,None)

    planes=[b,g,r]
    cv.CalcHist(planes, hist)
    '''
    #find min and max histogram bin.
    '''
    minval=maxval=0.0
    min_idx=max_idx=0
    minval, maxval, min_idx, max_idx=cv.GetMinMaxHistValue(hist)
    '''
    # threshold histogram.  this sets the bin values that are below the threshold
    to zero
    '''
    cv.ThreshHist(hist, maxval/32.0)
    '''
    #backproject the thresholded histogram, backprojection should contian higher values for
    #background and lower values for the foreground
    '''
    cv.CalcBackProject(planes, backproject32f, hist)
    '''
    #convert to 8u type
    '''
    val_min=val_max=0.0
    idx_min=idx_max=0
    val_min,val_max,idx_min,idx_max=cv.MinMaxLoc(backproject32f)
    cv.ConvertScale(backproject32f, backproject8u,255.0/maxval)
    '''
    #threshold backprojected image. this gives us the background
    '''
    cv.Threshold(backproject8u, bw, 10, 255, cv2.THRESH_BINARY)
    '''
    #some morphology on background
    '''
    cv.Dilate(bw, bw,kernel,1)
    cv.MorphologyEx(bw, bw, None,kernel, cv2.MORPH_CLOSE, 2)
    '''
    #get the foreground
    '''
    cv.SubRS(bw,cv.Scalar(255,255,255),bw)
    cv.MorphologyEx(bw, bw, None, kernel,cv2.MORPH_OPEN,2)
    cv.Erode(bw, bw, kernel, 1)
    '''
    #find contours of foreground
    #Grabcut
    '''
    size=cv.GetSize(bw)
    color=np.asarray(img[:,:])
    fg=np.asarray(bw[:,:])
#     mask=cv.CreateMat(size[1], size[0], cv2.CV_8UC1)
    '''
    #Make anywhere black in the grey_image (output from MOG) as likely background
    #Make anywhere white in the grey_image (output from MOG) as definite foreground
    '''
    rect = (0,0,0,0)
   
    mat_mask=np.zeros((size[1],size[0]),dtype='uint8')
    mat_mask[:,:]=fg
    
    mat_mask[mat_mask[:,:] == 0] = 2
    mat_mask[mat_mask[:,:] == 255] = 1
    
    '''
    #Make containers 
    '''                               
    bgdModel = np.zeros((1, 13 * 5))
    fgdModel = np.zeros((1, 13 * 5))
    cv2.grabCut(color, mat_mask, rect, bgdModel, fgdModel,cv2.GC_INIT_WITH_MASK)
    '''
    #Multiple new mask by original image to get cut
    '''
    mask2 = np.where((mat_mask==0)|(mat_mask==2),0,1).astype('uint8')  
    gcfg=np.zeros((size[1],size[0]),np.uint8)
    gcfg=mask2
    
    img_cut = color*mask2[:,:,np.newaxis]

    contours, hierarchy=cv2.findContours(gcfg ,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    
    for cnt in contours:
        print cnt
        rect_box = cv2.minAreaRect(cnt)
        box = cv2.cv.BoxPoints(rect_box)
        box = np.int0(box)
        cv2.drawContours(color,[box], 0,(0,0,255),2)
    cv2.imshow('demo', color)
    cv2.waitKey(0)
示例#19
0
    cv.Copy( complexInput, tmp, None )
    if(dft_A.width > im.width):
        tmp = cv.GetSubRect( dft_A, (im.width,0, dft_N - im.width, im.height))
        cv.Zero( tmp )

    # no need to pad bottom part of dft_A with zeros because of
    # use nonzero_rows parameter in cv.FT() call below

    cv.DFT( dft_A, dft_A, cv.CV_DXT_FORWARD, complexInput.height )

    cv.NamedWindow("win", 0)
    cv.NamedWindow("magnitude", 0)
    cv.ShowImage("win", im)

    # Split Fourier in real and imaginary parts
    cv.Split( dft_A, image_Re, image_Im, None, None )

    # Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
    cv.Pow( image_Re, image_Re, 2.0)
    cv.Pow( image_Im, image_Im, 2.0)
    cv.Add( image_Re, image_Im, image_Re, None)
    cv.Pow( image_Re, image_Re, 0.5 )

    # Compute log(1 + Mag)
    cv.AddS( image_Re, cv.ScalarAll(1.0), image_Re, None ) # 1 + Mag
    cv.Log( image_Re, image_Re ) # log(1 + Mag)


    # Rearrange the quadrants of Fourier image so that the origin is at
    # the image center
    cvShiftDFT( image_Re, image_Re )
示例#20
0
_, max_value, _, _ = cv.GetMinMaxHistValue(hist)

if max_value == 0:
    max_value = 1.0
cv.NormalizeHist(hist, 256 / max_value)

cv.ResetImageROI(im)

res = cv.CreateMat(im.height, im.width, cv.CV_8U)
cv.CalcBackProject([im], res, hist)

cv.Rectangle(im, (1, 1), (30, 30), (0, 0, 255), 2, cv.CV_FILLED)
cv.ShowImage("Original Image", im)
cv.ShowImage("BackProjected", res)
#--------------------------------------------------------
'''
# For colored pictures !
im = cv.LoadImage("../img/lena.jpg")

r = cv.CreateImage(cv.GetSize(im), 8, cv.CV_8UC1)
g = cv.CreateImage(cv.GetSize(im), 8, cv.CV_8UC1)
b = cv.CreateImage(cv.GetSize(im), 8, cv.CV_8UC1)
cv.Split(im, r, g, b, None)

cv.SetImageROI(r, (1, 1,30,30))
cv.SetImageROI(g, (1, 1,30,30))
cv.SetImageROI(b, (1, 1,30,30))
planes = [r,g,b]

histsize = [256,256,256]
示例#21
0
def rgb_image(image):
    r_image = cv.CreateImage((image.width, image.height), image.depth, 1)
    g_image = cv.CreateImage((image.width, image.height), image.depth, 1)
    b_image = cv.CreateImage((image.width, image.height), image.depth, 1)
    cv.Split(image, r_image, g_image, b_image, None)
    return (r_image, g_image, b_image)
示例#22
0
orig = cv.LoadImage("../img/road.png")
im = cv.CreateMat(orig.height / 5, orig.width / 5, cv.CV_8UC3)
cv.Resize(orig, im)  #resize the original image

src = cv.GetSubRect(im, (10, 10, 30, 30))

minSat = 65

hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
cv.CvtColor(src, hsv, cv.CV_BGR2HSV)

# Extract the H and S planes
h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
cv.Split(hsv, h_plane, s_plane, None, None)
planes = [h_plane, s_plane]

#s_plane = cv.Threshold(s_plane, s_plane, minSat, 255, cv.CV_THRESH_BINARY)

h_bins = 30
s_bins = 32
hist_size = [h_bins, s_bins]

# hue varies from 0 (~0 deg red) to 180 (~360 deg red again */
h_ranges = [0, 180]
# saturation varies from 0 (black-gray-white) to
# 255 (pure spectrum color)
s_ranges = [0, 255]
ranges = [h_ranges, s_ranges]
scale = 10
示例#23
0
#onesArray = np.ones((img.rows, img.cols), np.uint8)
#onesArray = np.copy(onesArray)
#print onesArray.flags.contiguous
 
 
#convert to hsv
#hsvImage = cv.CreateMat(rows, cols, cv.CV_8UC3)
#cv.CvtColor(originalImage, hsvImage, cv.CV_RGB2HSV);
 
#cv.ShowImage("hsv ", hsvImage)
 
#split them into channels
b8u = cv.CreateMat(rows, cols, cv.CV_8UC1)#cv.CloneMat(img)  
g8u = cv.CreateMat(rows, cols, cv.CV_8UC1)#cv.CloneMat(img)  
r8u = cv.CreateMat(rows, cols, cv.CV_8UC1)#cv.CloneMat(img)    
cv.Split(originalImage, b8u, g8u, r8u, None);
#cv.Split(originalImage, hsv_planes[0], hsv_planes[1], hsv_planes[2], None)
 
#cv.ShowImage("hue ", hueChannel)
#cv.ShowImage("saturation", saturationChannel)
 
#invert hue channel

 
#print(type(hueChannel))
 
for i in range(rows):
    for j in range(cols):
        #print valueChannel[i,j]
        #hsv_planes[2][i, j] = 180 - hsv_planes[2][i, j]
        r8u[i,j] = 255 - r8u[i,j];
示例#24
0
    def run(self):
        hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1)
        backproject_mode = False
        print "hitting run section"
        x = 0
        while True:
            #print x
            #x = x + 1
            frame = cv.QueryFrame(self.capture)
            cv.Flip(frame, frame, 1)

            # Convert to HSV and keep the hue
            hsv = cv.CreateImage(cv.GetSize(frame), 8, 3)
            cv.CvtColor(frame, hsv, cv.CV_BGR2HSV)
            self.hue = cv.CreateImage(cv.GetSize(frame), 8, 1)
            cv.Split(hsv, self.hue, None, None, None)

            # Compute back projection
            backproject = cv.CreateImage(cv.GetSize(frame), 8, 1)

            # Run the cam-shift
            cv.CalcArrBackProject([self.hue], backproject, hist)
            if self.track_window and is_rect_nonzero(self.track_window):
                crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1)
                print self.track_window
                (iters, (area, value, rect),
                 track_box) = cv.CamShift(backproject, self.track_window, crit)
                self.track_window = rect
                print self.track_window
            try:
                #prints the center x and y value of the tracked ellipse
                coord = track_box[0]
                print "center = {}".format(coord)
                if (coord[0] < 320):
                    print "move right"
                # ser.write("R")
                elif (coord[0] == 320):
                    print "do nothing"
                else:
                    print "move left"
                # ser.write("L")
            except UnboundLocalError:
                print "track_box is None"

            # If mouse is pressed, highlight the current selected rectangle
            # and recompute the histogram

            if self.drag_start and is_rect_nonzero(self.selection):
                sub = cv.GetSubRect(frame, self.selection)
                save = cv.CloneMat(sub)
                cv.ConvertScale(frame, frame, 0.5)
                cv.Copy(save, sub)
                x, y, w, h = self.selection
                cv.Rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255))

                sel = cv.GetSubRect(self.hue, self.selection)
                cv.CalcArrHist([sel], hist, 0)
                (_, max_val, _, _) = cv.GetMinMaxHistValue(hist)
                if max_val != 0:
                    cv.ConvertScale(hist.bins, hist.bins, 255. / max_val)
            elif self.track_window and is_rect_nonzero(self.track_window):
                print track_box
                cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3,
                              cv.CV_AA, 0)

            if not backproject_mode:
                cv.ShowImage("CamShiftDemo", frame)
            else:
                cv.ShowImage("CamShiftDemo", backproject)
            cv.ShowImage("Histogram", self.hue_histogram_as_image(hist))

            c = cv.WaitKey(7) % 0x100
            if c == 27:
                break
            elif c == ord("b"):
                backproject_mode = not backproject_mode
示例#25
0
        print pt

        #~ pt = [p = (round(p[0]), round(p[1])) for p in pt]
        #~ pt = [p = (round(p[0]), round(p[1])) for p in pt]
        #~ for p in pt:
        #~ print p
        #~ return int(round(p[0])), int(round(p[1]))
        #~ [p = (round(p[0]), round(p[1])) for p in pt]
        #~ print "new"
        #~ print p

        print pt
        #extract the colors
        #convert to HSV
        cv.CvtColor(sgc, hsv, cv.CV_RGB2HSV)
        cv.Split(hsv, hue, sat, val, None)

        #do the drawing. pt array should store p,p1,p2
        p3 = (pt[2][0] + pt[1][0] - pt[0][0], pt[2][1] + pt[1][1] - pt[0][1])
        cv.Line(sg, pt[0], pt[1], (0, 255, 0), 2)
        cv.Line(sg, pt[1], p3, (0, 255, 0), 2)
        cv.Line(sg, p3, pt[2], (0, 255, 0), 2)
        cv.Line(sg, pt[2], pt[0], (0, 255, 0), 2)

        #first sort the points so that 0 is BL 1 is UL and 2 is BR
        pt = winded(pt[0], pt[1], pt[2], p3)

        #find the coordinates of the 9 places we want to extract over
        v1 = (pt[1][0] - pt[0][0], pt[1][1] - pt[0][1])
        v2 = (pt[3][0] - pt[0][0], pt[3][1] - pt[0][1])
        p0 = (pt[0][0], pt[0][1])
示例#26
0
def extractEyeBall(imgMatrix):
    # img = cv2.imread("E:\\python workspace\\Charlie\\eye1.jpg")

    print type(imgMatrix)
    imageArray = np.asarray(imgMatrix)

    # (rows, cols, channels) =  img.shape

    rows = imgMatrix.rows
    cols = imgMatrix.cols

    #     print rows
    #     print cols

    cv2.imshow("input", imageArray)

    originalImage = imgMatrix

    # split them into channels
    b8u = cv.CreateMat(rows, cols, cv.CV_8UC1)  # cv.CloneMat(img)
    g8u = cv.CreateMat(rows, cols, cv.CV_8UC1)  # cv.CloneMat(img)
    r8u = cv.CreateMat(rows, cols, cv.CV_8UC1)  # cv.CloneMat(img)
    cv.Split(originalImage, b8u, g8u, r8u, None)

    # invert colors
    for i in range(rows):
        for j in range(cols):
            r8u[i, j] = 255 - r8u[i, j]
            g8u[i, j] = 255 - g8u[i, j]
            b8u[i, j] = 255 - b8u[i, j]

    cv.Merge(b8u, g8u, r8u, None, originalImage)
    if isShowImages:
        cv.ShowImage("subtraction ", originalImage)

    # convert it to grey scale
    grayScaleImage = cv.CreateMat(rows, cols, cv.CV_8UC1)
    cv.CvtColor(originalImage, grayScaleImage, cv.CV_BGR2GRAY)
    cv.ShowImage("grey ", grayScaleImage)

    greyArray = np.asarray(grayScaleImage, np.uint8, 1)
    # binaryImage = cv.CreateMat(rows, cols, cv.CV_8UC1)
    # #make it binary by making it threshold at 220
    ret, binaryImage = cv2.threshold(greyArray, 220, 255, cv2.THRESH_BINARY)

    print type(binaryImage)

    element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
    # cv2.erode(binaryImage, element, binaryImage)
    # cv.Dilate(binaryImage, binaryImage)

    # cv.Erode(binaryImage, binaryImage)
    # cv.Dilate(binaryImage, binaryImage)

    # show the image
    cv2.imshow("binary image ", binaryImage)

    params = cv2.SimpleBlobDetector_Params()
    #             params.minDistBetweenBlobs = 1.0  # minimum 10 pixels between blobs
    #             params.filterByArea = True        # filter my blobs by area of blob
    #             params.minArea = 5.0             # min 20 pixels squared
    #             params.maxArea = 500.0            # max 500 pixels squared
    params.minDistBetweenBlobs = 50.0
    params.filterByInertia = False
    params.filterByConvexity = False
    params.filterByColor = False
    params.filterByCircularity = False
    params.filterByArea = True
    params.minArea = 2.0
    params.maxArea = 500.0

    myBlobDetector = cv2.SimpleBlobDetector(params)
    keypoints = myBlobDetector.detect(binaryImage)
    print "blobs " + str(keypoints)

    # extract the x y coordinates of the keypoints:

    for i in range(0, len(keypoints) - 1):
        print keypoints[i].pt
        pt1 = (int(keypoints[i].pt[0]), int(keypoints[i].pt[1]))
        pt2 = (int(keypoints[i + 1].pt[0]), int(keypoints[i + 1].pt[1]))
        cv2.line(imageArray, pt1, pt2, (255, 0, 0))
    #                 float X=keypoints[i].pt.x;
    #                 float Y=keypoints[i].pt.y;

    cv2.imshow("eye ", imageArray)
示例#27
0
文件: opencv1.py 项目: arshibassi/UAV
image = cv.LoadImage('meinv.jpg', cv.CV_LOAD_IMAGE_COLOR)

font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8)
y = image.height / 4
x = image.width / 2

cv.PutText(image, "Hello Meinv!", (x, y), font, cv.RGB(0, 0, 0))

thumb = cv.CreateImage((image.width / 2, image.height / 2), cv.CV_8UC2, 3)
cv.Resize(image, thumb)
#cvt = cv.CreateImage(cv.GetSize(thumb), cv.CV_8UC2, 3)
#cv.CvtColor(thumb, cvt, cv.CV_RGB2BGR)
#cv.NamedWindow('Image', cv.CV_WINDOW_AUTOSIZE)

b = cv.CreateImage(cv.GetSize(thumb), thumb.depth, 1)
g = cv.CloneImage(b)
r = cv.CloneImage(b)

cv.Split(thumb, b, g, r, None)

merged = cv.CreateImage(cv.GetSize(thumb), 8, 3)
cv.Merge(g, b, r, None, merged)

cv.ShowImage('Image', thumb)
cv.ShowImage('Blue', b)
cv.ShowImage('Green', g)
cv.ShowImage('Red', r)
cv.ShowImage('Merged', merged)
cv.WaitKey(0)
import cv2.cv as cv
import numpy as np

orig = cv.LoadImage('D:/test.png')
b = cv.CreateImage(cv.GetSize(orig), orig.depth, 1)
g = cv.CloneImage(b)
r = cv.CloneImage(b)
cv.Split(orig, b, g, r, None)

merged = cv.CreateImage(cv.GetSize(orig), 8, 3)
cv.Merge(g, b, r, None, merged)

cv.ShowImage("Image", orig)
cv.ShowImage("Blue", b)
cv.ShowImage("Green", g)
cv.ShowImage("Red", r)
cv.ShowImage("Merged", merged)

cv.WaitKey(0)
示例#29
0
    elif len(sys.argv) == 2:
        capture = cv.CreateFileCapture(sys.argv[1])

    if not capture:
        print "Could not initialize capturing..."
        sys.exit(-1)

    cv.NamedWindow("Laplacian", 1)

    while True:
        frame = cv.QueryFrame(capture)
        if frame:
            if not laplace:
                planes = [cv.CreateImage((frame.width, frame.height), 8, 1) for i in range(3)]
                laplace = cv.CreateImage((frame.width, frame.height), cv.IPL_DEPTH_16S, 1)
                colorlaplace = cv.CreateImage((frame.width, frame.height), 8, 3)

            cv.Split(frame, planes[0], planes[1], planes[2], None)
            for plane in planes:
                cv.Laplace(plane, laplace, 3)
                cv.ConvertScaleAbs(laplace, plane, 1, 0)

            cv.Merge(planes[0], planes[1], planes[2], None, colorlaplace)

            cv.ShowImage("Laplacian", colorlaplace)

        if cv.WaitKey(10) != -1:
            break

    cv.DestroyWindow("Laplacian")
示例#30
0
def findSquares4(img, storage):
    N = 11
    sz = (img.width & -2, img.height & -2)
    timg = cv.CloneImage(img); # make a copy of input image
    gray = cv.CreateImage(sz, 8, 1)
    pyr = cv.CreateImage((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.CreateSeq(0, sizeof_CvSeq, sizeof_CvPoint, storage)
    squares = CvSeq_CvPoint.cast(squares)

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

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

            # find contours and store them all as a list
            count, contours = cv.FindContours(gray, storage, sizeof_CvContour,
                cv.CV_RETR_LIST, cv. CV_CHAIN_APPROX_SIMPLE, (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.ApproxPoly(contour, sizeof_CvContour, storage,
                    cv.CV_POLY_APPROX_DP, cv.ContourPerimeter(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.ContourArea(result)) > 1000 and
                    cv.CheckContourConvexity(result)):
                    s = 0
                    for i in range(5):
                        # find minimum angle between joint
                        # edges (maximum of cosine)
                        if(i >= 2):
                            t = abs(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])

    return squares