Beispiel #1
0
def is_next_to(cont1, cont2):
    win, c1, r1 = cv.MinEnclosingCircle(cont1)
    if not win:
        return False
    win, c2, r2 = cv.MinEnclosingCircle(cont2)
    if not win:
        return False
    return dist(c1, c2) < r1 * 1.2 or dist(c1, c2) < r2 * 1.2
def draw_common(points):
    success, center, radius = cv.MinEnclosingCircle(points)
    if success:
        cv.Circle(img, roundxy(center), cv.Round(radius),
                  cv.CV_RGB(255, 255, 0), 1, cv.CV_AA, 0)

    box = cv.MinAreaRect2(points)
    box_vtx = [roundxy(p) for p in cv.BoxPoints(box)]
    cv.PolyLine(img, [box_vtx], 1, cv.CV_RGB(0, 255, 255), 1, cv.CV_AA)
Beispiel #3
0
 def find_limbs(self, contours):
     """ return the 3 biggest contours """
     blobs = []
     while contours:
         (i, center, radius) = cv.MinEnclosingCircle(contours)
         blobs.append(
             (int(round(radius)), tuple([int(round(c)) for c in center])))
         contours = contours.h_next()
     blobs.sort()
     blobs.reverse()
     return blobs[:3]
Beispiel #4
0
    def find_limbs(self, contours):
        """ return the 3 biggest contours """
        blobs = []
        while contours:
            (i, center, radius) = cv.MinEnclosingCircle(contours)
            blobs.append((radius, center))
            contours = contours.h_next()

        blobs.sort()
        blobs.reverse()
        return blobs[:3]
    def segment(self, cv_source_image):
        self.get_tracker().flush()

        hue = self.pre_process_source(cv_source_image)

        size = (hue.width, hue.height)
        cv_out = cv.CreateImage(size, 8, 3)
        cv.Zero(cv_out)
        bounds = self.find_color_bounds(hue)

        for bound in bounds:

            (tmp1d, avg_hue) = self.extract_hue_channel(hue, bound)

            contours = cv.FindContours(tmp1d, cv.CreateMemStorage(),
                                       cv.CV_RETR_CCOMP)

            while contours:
                cSize = abs(cv.ContourArea(contours))

                if cSize < self.min_contour_size:
                    contours = contours.h_next()
                    continue

                (_, center, radius) = cv.MinEnclosingCircle(contours)

                center = (int(center[0]), int(center[1]))
                id = self.get_tracker().add(center, radius, avg_hue, size)

                cv.DrawContours(cv_out, contours, cv.CV_RGB(255, 255, avg_hue),
                                cv.CV_RGB(255, 255, avg_hue), 1, -1, 1)

                contours = contours.h_next()

                #font    = cv.InitFont(cv.CV_FONT_HERSHEY_PLAIN, 0.8, 1, 0, 1, 4);
                #cv.Circle( cv_out, (x,y), int(radius), cv.CV_RGB(255,255,int(avg_hue)), 1 );
                #cv.PutText( cv_out, "%d:%d"%(id, avg_hue), (x,y), font, cv.CV_RGB(255,255,150));

        return cv_out
    contours2 = False
    hull = False
    blank = cv.CreateImage(cv.GetSize(image), 8, 3)

    while contours:
        contours2 = contours
        contours = contours.h_next()

    if contours2:
        hull = cv.ConvexHull2(contours2, cv.CreateMemStorage(),
                              cv.CV_CLOCKWISE, 0)
        defects = cv.ConvexityDefects(contours2, hull, cv.CreateMemStorage())
        cv.DrawContours(image, contours2, (255, 0, 0), (0, 255, 0), 0, 5)
        noOfDefects = 0
        comx, comy = 0, 0
        Z, (p, q), radius = cv.MinEnclosingCircle(contours2)

        if Z:
            cv.Circle(image, (int(p), int(q)), int(5), [0, 255, 255], -1)

        for defect in defects:
            start, end, far, d = defect
            xpos, ypos = far
            cv.Line(image, start, end, [0, 255, 0], 2)

            if d > 20 and not pointInFace(xpos, ypos, x, y, w, h):
                cv.Circle(image, far, 5, [0, 0, 255], -1)
                if noOfDefects:
                    cv.Circle(image, end, 10, [0, 0, 0], -1)
                    #cv.Line(image,end,(int(p),int(q)),[0,0,0],3)
                    x, y = end
Beispiel #7
0
    def listen(self):
        bgimg = cv.CreateImage((self.background.width, self.background.height),
                               8, 3)
        img = cv.CreateImage((self.background.width, self.background.height),
                             8, 3)
        cv.Copy(self.background, bgimg)
        smallimg = cv.CreateImage((self.background.width / self.zoom,
                                   self.background.height / self.zoom), 8, 3)
        cv.GetRectSubPix(
            bgimg, smallimg,
            (self.background.width / (2 * self.zoom) + self.offset[0],
             self.background.height / (2 * self.zoom) + self.offset[1]))
        cv.Resize(smallimg, img)

        cv.Smooth(img, img, cv.CV_GAUSSIAN)
        if (self.cp != False):
            cv.Circle(img, self.zoomPt(int(self.cp.x), int(self.cp.y)), 3,
                      cv.RGB(0, 255, 0), -1)
        mask = thresholding.threshold(img,
                                      thresholding.CUSTOM,
                                      False,
                                      crop_rect=None,
                                      cam_info=None,
                                      listener=None,
                                      hue_interval=(self.hue_low, self.hue_up))

        cv.Not(mask, mask)
        new_img = cv.CloneImage(img)
        cv.SetZero(new_img)
        cv.Copy(img, new_img, mask)
        new_img = thresholding.sat_threshold(new_img, 50)
        cv.Line(img, (self.ch_x - 25, self.ch_y), (self.ch_x + 25, self.ch_y),
                cv.RGB(255, 255, 0))
        cv.Line(img, (self.ch_x, self.ch_y - 25), (self.ch_x, self.ch_y + 25),
                cv.RGB(255, 255, 0))

        image_gray = cv.CreateImage(cv.GetSize(new_img), 8, 1)
        cv.CvtColor(new_img, image_gray, cv.CV_RGB2GRAY)
        cv.MorphologyEx(image_gray, image_gray, None, None, cv.CV_MOP_OPEN, 1)
        storage = cv.CreateMemStorage(0)
        seq = cv.FindContours(image_gray, storage)
        points = []
        contour = seq
        centers = []
        ccs = []
        while contour:
            bound_rect = cv.BoundingRect(list(contour))
            area = cv.ContourArea(contour)
            cc = contour
            contour = contour.h_next()

            if area < 50 or area > 2500:
                continue
            ccs.append(cc)
            win, center, radius = cv.MinEnclosingCircle(cc)
            cv.DrawContours(new_img, cc, (0, 255, 0), (0, 255, 0), 0, 1)
            pt1 = (bound_rect[0], bound_rect[1])
            pt2 = (bound_rect[0] + bound_rect[2],
                   bound_rect[1] + bound_rect[3])
            points.append(pt1)
            points.append(pt2)
            cv.Circle(new_img, center, radius, (0, 0, 255))
            centers.append(center)
            #cv.Rectangle(new_img, pt1, pt2, cv.CV_RGB(255,0,0), 1)

            font = cv.InitFont(cv.CV_FONT_HERSHEY_PLAIN, 1, 1)
            cv.PutText(new_img, "%.2f" % area, pt1, font, (255, 255, 255))

        for cont1, cont2 in itertools.combinations(ccs, 2):
            if is_next_to(cont1, cont2):
                win, c1, r1 = cv.MinEnclosingCircle(cont1)
                win, c2, r2 = cv.MinEnclosingCircle(cont2)
                cv.Line(new_img, c1, c2, (255, 255, 0))
        #DRAW
        cv.ShowImage(self.name, new_img)
        #Do some funny business
        imgcs = {}
        satt = thresholding.sat_threshold(img, 50)
        for color in HueRanges.__dict__:
            if color == color.upper():
                img_c = thresholding.filter_color(satt, color)
                cv.ShowImage(color, img_c)
        cv.WaitKey(25)