예제 #1
0
    def get_heading(self):
        if self.cap.isOpened() == True:
            ret, src_img = self.cap.read()

            if ret == True:
                #cv2.imshow('Source', src_img)
                sml_img = cv2.resize(src_img, (0, 0), fx=self.fx, fy=self.fy)

                dy = 0
                bw_img = cv2.cvtColor(sml_img, cv.CV_RGB2GRAY)
                #cv2.imshow('Black & White', bw_img)
                _, thresh_img = cv2.threshold(bw_img, self.threshold, 255,
                                              cv2.THRESH_BINARY_INV)
                #cv2.imshow('Threshold', thresh_img)

                ret, cnt = self.get_contour(thresh_img)
                if ret == True:
                    if len(cnt) >= 5:
                        ellipse = cv2.fitEllipse(cnt)
                        if self.capture_data:
                            cv2.ellipse(sml_img, ellipse, (0, 255, 255), 2)
                        box = cv.BoxPoints(ellipse)
                        box = np.int0(box)
                    else:
                        rect = cv2.minAreaRect(cnt)
                        box = cv.BoxPoints(rect)
                        box = np.int0(box)
                        if self.capture_data:
                            cv2.drawContours(sml_img, [box], 0, (255, 255, 0),
                                             2)

                    box = box[box[:, 1].argsort()]
                    p0 = ((box[0][0] + box[1][0]) / 2,
                          (box[0][1] + box[1][1]) / 2)
                    p1 = (self.width / 2, self.height - 1)
                    dx = p1[0] - p0[0]
                    dy = p1[1] - p0[1]

                    if self.capture_data:
                        cv2.line(sml_img, p0, p1, (0, 0, 255), 2)
                        self.writer.write(sml_img)

                    if dy != 0:
                        self.heading = np.arctan(float(dx) / dy)

                return (True, self.heading)
        return (False, 0)
예제 #2
0
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)
예제 #3
0
 def __init__(self, cont):
     self.cont = cont
     self.color = random_color()
     self.count = -1
     self.id = random.randint(1, 2**31)
     self.frames = None
     self.desc = None
     self.ids = None
     box = cv.MinAreaRect2(cont)
     self.box_points = [(int(x), int(y)) for x, y in cv.BoxPoints(box)]
예제 #4
0
def minAreaRectImage(image, returnPoints=True):
    points = []
    for x in xrange(image.width):
        for y in xrange(image.height):
            if image[y, x] > 128: points.append((x, y))
    box = cv.MinAreaRect2(points)
    if not returnPoints:
        return box
    box_vtx = map(roundXY, cv.BoxPoints(box))
    return box_vtx
예제 #5
0
def feature_detector(cards, contours):
    """Feature is area ratio"""
    figure_contours = []
    figure_moments = {}
    # collecting figures
    for card in cards:
        #print 'card: ', card
        for figure in card.figures:
            #print 'figure: ', figure
            figure_outer_contour_id = int(figure.id)
            figure_contours.append(figure_outer_contour_id)
            contour = contours[figure_outer_contour_id]
            #moments = cv2.moments(contours[figure_outer_contour_id])
            #moments = cv2.HuMoments(moments)
            #figure_moments[figure.id] = moments
            square = cv2.contourArea(contour)
            if square != 0:
                rect = cv2.minAreaRect(contour)
                box = cv.BoxPoints(rect)
                box = np.array([[np.int0(point)] for point in box])
                min_rect_square = cv2.contourArea(box)
                figure_moments[figure.id] = (min_rect_square - square) / square
            else:
                figure_moments[figure.id] = 0
    '''
    # adjacency matrix
    n = len(figure_contours)
    similarity_matrix = {}
    metric_type = cv.CV_CONTOURS_MATCH_I2
    for i, ci in enumerate(figure_contours):
        moments = cv2.moments(contours[ci])
        #print moments
        #print cv2.HuMoments(moments)
        for j, cj in enumerate(figure_contours[:i + 1]):
            metric_ij = cv2.matchShapes(contours[ci], contours[cj], metric_type, 0)
            metric_ji = cv2.matchShapes(contours[cj], contours[ci], metric_type, 0)
            metric = 1 - (metric_ij + metric_ji) / 2
            similarity_matrix[(i, j)] = metric
            similarity_matrix[(j, i)] = metric
    #print similarity_matrix
    #plot_heatmap(similarity_matrix, n)
    '''
    #print figure_moments
    return figure_moments
예제 #6
0
def plot_contours(src_image, dest_image):
    cv.NamedWindow("debug", cv.CV_WINDOW_AUTOSIZE)

    # Better to use HSV when doing extraction
    (rows, cols) = cv.GetSize(src_image)
    image_hsv = cv.CreateImage((rows, cols), cv.IPL_DEPTH_8U, 3)
    h_plane = cv.CreateImage((rows, cols), 8, 1)
    s_plane = cv.CreateImage((rows, cols), 8, 1)
    image_bin = cv.CreateImage((rows, cols), 8, 1)
    cv.CvtColor(src_image, image_hsv, cv.CV_RGB2HSV)
    cv.Laplace(h_plane, h_plane, 3)
    cv.Threshold(h_plane, h_plane, 40.0, 255.0, cv.CV_THRESH_BINARY)
    cv.Smooth(h_plane, h_plane, cv.CV_BLUR, 5,
              5)  # Its suggested that smoothing first gives better results
    cv.ShowImage("debug", h_plane)

    # Create binary image to be used for contour detection
    # image_gray = cv.CreateImage(cv.GetSize(src_image), 8, 1)
    # cv.CvtColor(image_hsv, image_gray, cv.CV_BGR2GRAY)
    # cv.Laplace(image_gray, image_gray, 3)
    # cv.Threshold(image_gray, image_gray, 40.0, 255.0, cv.CV_THRESH_BINARY)
    # cv.Smooth(image_gray, image_gray, cv.CV_BLUR, 5, 5) # Its suggested that smoothing first gives better results
    # cv.ShowImage("debug", image_gray)

    contours = cv.FindContours(image_gray, cv.CreateMemStorage(),
                               cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_SIMPLE)
    #cv.DrawContours(dest_image, contours, (255,0,0), (0,255,0), 1, 2)

    # Draw bounding box + mass center of shape
    while contours:
        rect = cv.MinAreaRect2(contours)
        box = cv.BoxPoints(rect)
        for i in range(4):
            cv.Line(dest_image, box[i], box[(i + 1) % 4], (0, 0, 255), 1, 8)

        mom = cv.Moments(contours)
        for j in mom:
            print j
        # momPoint = cv.CvPoint(mom.m10/mom.m00,mom.m01/mom.m00)
        # cv.Circle(dest_image, (mom.m10/mom.m00,mom.m01/mom.m00), 2, (0,255,255))
        # r0 = cv.BoundingRect(cnt)
        # Rectangle(dest_image, pt1, pt2, (0,255,0))
        contours = contours.h_next()
예제 #7
0
def filter_red(src_img, dest_img):
    cv.Smooth(src_img, src_img)

    # Histogram
    # h = np.zeros((300,256,3))
    # b,g,r = cv.Split(src_img)
    # bins = np.arange(256).reshape(256,1)
    # color = [ (255,0,0),(0,255,0),(0,0,255) ]

    # CalcHist(image, hist, accumulate=0, mask=NULL)
    # histogram = cv.CalcHist(h_plane,[0],None,[256],[0,255])
    # # cv.NormalizeHist(histogram, 1)
    # hist=np.int32(np.around(hist_item))
    # pts = np.column_stack((bins,hist))
    # cv2.polylines(h,[pts],False,col)

    # 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, cv.CV_HIST_ARRAY, ranges, 1)
    # cv.CalcHist(src_img, hist)
    # (_, max_value, _, _) = cv.GetMinMaxHistValue(hist)
    # for h in range(h_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)

    (rows, cols) = cv.GetSize(src_img)

    img_hsv = cv.CreateImage((rows, cols), cv.IPL_DEPTH_8U, 3)
    h_plane = cv.CreateImage((rows, cols), 8, 1)
    s_plane = cv.CreateImage((rows, cols), 8, 1)
    img_binary = cv.CreateImage((rows, cols), 8, 1)
    cv.CvtColor(src_img, img_hsv, cv.CV_RGB2HSV)
    # cv.Split(img_hsv, h_plane, s_plane, None, None)

    # cv.EqualizeHist(h_plane, h_plane) # Gives better result in form of less flickering

    cv.InRangeS(img_hsv, cv.Scalar(COLOR_RANGE_LOW, 100, 100),
                cv.Scalar(COLOR_RANGE_HIGH, 255, 255), h_plane)

    # create temps used by algorithm images
    # eig = cv.CreateImage(cv.GetSize(src_img), cv.IPL_DEPTH_32F, 1)
    # temp = cv.CreateImage(cv.GetSize(src_img), cv.IPL_DEPTH_32F, 1)
    # the default parameters
    # quality = 0.01
    # min_distance = 30 # min distance between detected points
    # search the good points
    # features = cv.GoodFeaturesToTrack(h_plane, eig, temp, 20, quality, min_distance)
    # for (x,y) in features:
    #     cv.Circle(dest_img, (x, y), 3, (0, 255, 0), -1, 8, 0)

    cv.Canny(h_plane, h_plane, CANNY_LOW, CANNY_HIGH)
    cv.ShowImage("thresh", h_plane)

    contours = cv.FindContours(h_plane, cv.CreateMemStorage(),
                               cv.CV_RETR_EXTERNAL, cv.CV_CHAIN_APPROX_SIMPLE)
    while contours:
        rect = cv.MinAreaRect2(contours)
        if cv.ContourArea(contours) > 1000:
            print "FOUND RECT WITH AREA: %s" % cv.ContourArea(contours)
            box = cv.BoxPoints(rect)
            for i in range(4):
                cv.Line(dest_img, box[i], box[(i + 1) % 4], (0, 255, 0), 6, 8)
        contours = contours.h_next()

    # HoughLines2(image, storage, method, rho, theta, threshold, param1=0, param2=0)
    # lines = cv.HoughLines2(h_plane, cv.CreateMemStorage(), cv.CV_HOUGH_PROBABILISTIC, cv.CV_PI/180, 1, 50, 1)
    # for l in lines:
    #     (p1, p2) = l
    #     # Line(img, pt1, pt2, color, thickness=1, lineType=8, shift=0)
    #     cv.Line(dest_img, p1, p2, cv.Scalar(0,0,255), 2, cv.CV_AA)

    cv.ShowImage("result", dest_img)
예제 #8
0
 def cont(self, value):
     self._cont = value
     box = cv.MinAreaRect2(value)
     self.box_points = [(int(x), int(y)) for x, y in cv.BoxPoints(box)]
예제 #9
0
파일: first.py 프로젝트: arjun001/project
    def detect_motion(self, sensitivity='medium'):

        #Finding Video Size from the first frame
        frame = cv.QueryFrame(self.video_handle)
        frame_size = cv.GetSize(frame)
        '''Initializing Image Variables(to be used in motion detection) with required types and sizes'''
        # Image containg instantaneous moving rectangles
        color_image = cv.CreateImage(frame_size, 8, 3)
        # Resizing to window size
        color_output = cv.CreateImage(self.window_size, 8, 3)
        # Grey Image used for contour detection
        grey_image = cv.CreateImage(frame_size, cv.IPL_DEPTH_8U, 1)
        # Image storing background (moving pixels are averaged over small time window)
        moving_average = cv.CreateImage(frame_size, cv.IPL_DEPTH_32F, 3)
        # Image for storing tracks resized to window size
        track_output = cv.CreateImage(self.window_size, cv.IPL_DEPTH_8U, 3)
        track_image, track_win = self.init_track_window(frame)

        def totuple(a):
            try:
                return tuple(totuple(i) for i in a)
            except TypeError:
                return a

        first = True
        # Infinite loop for continuous detection of motion
        while True:
            '''########## Pixelwise Detection of Motion in a frame ###########'''
            # Capturing Frame
            color_image = cv.QueryFrame(self.video_handle)

            ##### Sensitivity Control 1 #####
            if (sensitivity == 'medium') or (sensitivity == 'low'):
                # Gaussian Smoothing
                cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3, 0)

            if first:
                difference = cv.CloneImage(color_image)
                temp = cv.CloneImage(color_image)
                cv.ConvertScale(color_image, moving_average, 1.0, 0.0)
                first = False
            else:
                cv.RunningAvg(color_image, moving_average, .020, None)

            # Convert the scale of the moving average.
            cv.ConvertScale(moving_average, temp, 1, 0.0)

            # Minus the current frame from the moving average.
            cv.AbsDiff(color_image, temp, difference)
            #cv.ShowImage("BG",difference)

            # Convert the image to grayscale.
            cv.CvtColor(difference, grey_image, cv.CV_RGB2GRAY)

            ##### Sensitivity Control 2 #####
            sens_thres = 90 if (sensitivity == 'low') or (self.opt
                                                          == 'cam') else 40
            # Convert the image to black and white.
            cv.Threshold(grey_image, grey_image, sens_thres, 255,
                         cv.CV_THRESH_BINARY)
            '''### Blobing moved adjacent pixels, finding closed contours and bounding rectangles ###'''
            ##### Sensitivity Control 3 #####
            if (sensitivity == 'medium') or (sensitivity == 'low'):
                # Dilate and erode to get people blobs
                ker_size = 20 if self.opt == 'file' else 50
                cv.Dilate(grey_image, grey_image, None, ker_size)
                cv.Erode(grey_image, grey_image, None, 3)

            storage = cv.CreateMemStorage(0)
            contour = cv.FindContours(grey_image, storage, cv.CV_RETR_CCOMP,
                                      cv.CV_CHAIN_APPROX_SIMPLE)
            points = []
            while contour:
                bound_rect = cv.BoundingRect(list(contour))
                polygon_points = cv.ApproxPoly(list(contour), storage,
                                               cv.CV_POLY_APPROX_DP)

                pt1 = (bound_rect[0], bound_rect[1])
                pt2 = (bound_rect[0] + bound_rect[2],
                       bound_rect[1] + bound_rect[3])

                if (self.opt == 'file'):
                    points.append(pt1)
                    points.append(pt2)
                elif (bound_rect[0] - bound_rect[2] >
                      20) and (bound_rect[1] - bound_rect[3] > 20):
                    points.append(pt1)
                    points.append(pt2)

                box = cv.MinAreaRect2(polygon_points)
                box2 = cv.BoxPoints(box)
                box3 = np.int0(np.around(box2))
                box4 = totuple(box3)
                box5 = box4 + (box4[0], )

                # Filling the contours in the greyscale image (visual blobs instead of just contours)
                cv.FillPoly(grey_image, [
                    list(polygon_points),
                ], cv.CV_RGB(255, 255, 255), 0, 0)

                # Following line to draw detected contours as well
                #cv.PolyLine( color_image, [ polygon_points, ], 0, cv.CV_RGB(255,0,0), 1, 0, 0 )

                # Drawing Rectangle around the detected contour
                cv.PolyLine(color_image, [list(box5)], 0, (0, 255, 255), 2)

                if len(points):  # (self.opt == 'file') and
                    center1 = (pt1[0] + pt2[0]) / 2
                    center2 = (pt1[1] + pt2[1]) / 2
                    cv.Circle(color_image, (center1, center2), 5,
                              cv.CV_RGB(0, 255, 0), -1)
                    rad = 3 if self.opt == 'file' else 5
                    cv.Circle(track_image, (center1, center2), rad,
                              cv.CV_RGB(255, 128, 0), -1)

                contour = contour.h_next()

            # Uncomment to track centroid of all the moved boxes (only for WebCam)
            '''
            if (self.opt == 'cam') and len(points):
                center_point = reduce(lambda a, b: ((a[0] + b[0]) / 2, (a[1] + b[1]) / 2), points)
                cv.Circle(track_image, center_point, 15, cv.CV_RGB(255, 128, 0), -1)
            '''
            cv.Resize(color_image, color_output, cv.CV_INTER_AREA)
            cv.ShowImage("Original", color_output)

            cv.Resize(track_image, track_output, cv.CV_INTER_AREA)
            cv.ShowImage(track_win, track_output)

            # Listen for ESC key
            c = cv.WaitKey(7) % 0x100
            if (0xFF & c == 27):
                cv.SaveImage('Tracks_img_042_' + sensitivity + '.jpeg',
                             track_output)
                break