Exemplo n.º 1
0
def redraw():
    global draging
    global has_roi
    global roi_x0
    global roi_y0
    global cur_mouse_x
    global cur_mouse_y
    #Redraw ROI selection
    image2 = cv.CloneImage(current_image)

    # redraw old rect
    pen_width = 4
    if rect_table.has_key(current_img_file_name):
        rects_in_table = rect_table[current_img_file_name]
        for r in rects_in_table:
            cv.Rectangle(image2, (r[0], r[1]), (r[0] + r[2], r[1] + r[3]),
                         cv.CV_RGB(0, 255, 0), pen_width)

    # redraw new rect
    if has_roi:
        cv.Rectangle(image2, (roi_x0, roi_y0), (cur_mouse_x, cur_mouse_y),
                     cv.CV_RGB(255, 0, 255), pen_width)

    # draw background
    if current_img_file_name in background_files:
        cv.Line(image2, (0, 0), (image2.width, image2.height),
                cv.CV_RGB(255, 0, 0))
        cv.Line(image2, (0, image2.height), (image2.width, 0),
                cv.CV_RGB(255, 0, 0))

    cv.ShowImage(window_name, image2)
Exemplo n.º 2
0
def Hist(image):
    a = [0] * 256
    w = image.width
    h = image.height
    iHist = cv2.CreateImage((256, 256), 8, 3)
    for i in range(h):
        for j in range(w):
            iGray = int(image[i, j])
            a[iGray] = a[iGray] + 1

    S = max(a)
    c = cv2.RGB(200, 150, 255)

    for k in range(256):
        a[k] = a[k] * 200 / S
        x = (k, 255)
        y = (k, 255 - a[k])
        cv2.Line(iHist, x, y, c)

    return iHist
#---- Standard ----
color_dst_standard = cv.CreateImage(cv.GetSize(im), 8, 3)
cv.CvtColor(im, color_dst_standard,
            cv.CV_GRAY2BGR)  #Create output image in RGB to put red lines

lines = cv.HoughLines2(dst, cv.CreateMemStorage(0), cv.CV_HOUGH_STANDARD, 1,
                       pi / 180, 100, 0, 0)
for (rho, theta) in lines[:100]:
    a = math.cos(theta)  #Calculate orientation in order to print them
    b = math.sin(theta)
    x0 = a * rho
    y0 = b * rho
    pt1 = (cv.Round(x0 + 1000 * (-b)), cv.Round(y0 + 1000 * (a)))
    pt2 = (cv.Round(x0 - 1000 * (-b)), cv.Round(y0 - 1000 * (a)))
    cv.Line(color_dst_standard, pt1, pt2, cv.CV_RGB(255, 0, 0), 2,
            4)  #Draw the line

#---- Probabilistic ----
color_dst_proba = cv.CreateImage(cv.GetSize(im), 8, 3)
cv.CvtColor(im, color_dst_proba, cv.CV_GRAY2BGR)  # idem

rho = 1
theta = pi / 180
thresh = 50
minLength = 120  # Values can be changed approximately to fit your image edges
maxGap = 20

lines = cv.HoughLines2(dst, cv.CreateMemStorage(0), cv.CV_HOUGH_PROBABILISTIC,
                       rho, theta, thresh, minLength, maxGap)
for line in lines:
    cv.Line(color_dst_proba, line[0], line[1], cv.CV_RGB(255, 0, 0), 2, 8)
Exemplo n.º 4
0
    def run(self):
        #initiate font
        font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8)
        # instantiate images
        hsv_img = cv.CreateImage(cv.GetSize(cv.QueryFrame(self.capture)), 8, 3)
        threshold_img1 = cv.CreateImage(cv.GetSize(hsv_img), 8, 1)
        threshold_img1a = cv.CreateImage(cv.GetSize(hsv_img), 8, 1)
        threshold_img2 = cv.CreateImage(cv.GetSize(hsv_img), 8, 1)
        i = 0
        writer = cv.CreateVideoWriter('angle_tracking.avi', cv.CV_FOURCC('M', 'J', 'P', 'G'), 30, cv.GetSize(hsv_img), 1)

        while True:
            # capture the image from the cam
            img = cv.QueryFrame(self.capture)

            # convert the image to HSV
            cv.CvtColor(img, hsv_img, cv.CV_BGR2HSV)

            # threshold the image to isolate two colors
            cv.InRangeS(hsv_img, (165, 145, 100), (250, 210, 160), threshold_img1)  # red
            cv.InRangeS(hsv_img, (0, 145, 100), (10, 210, 160), threshold_img1a)  # red again
            cv.Add(threshold_img1, threshold_img1a, threshold_img1)  # this is combining the two limits for red
            cv.InRangeS(hsv_img, (105, 180, 40), (120, 260, 100), threshold_img2)  # blue

            # determine the moments of the two objects
            threshold_img1 = cv.GetMat(threshold_img1)
            threshold_img2 = cv.GetMat(threshold_img2)
            moments1 = cv.Moments(threshold_img1, 0)
            moments2 = cv.Moments(threshold_img2, 0)
            area1 = cv.GetCentralMoment(moments1, 0, 0)
            area2 = cv.GetCentralMoment(moments2, 0, 0)

            # initialize x and y
            x1, y1, x2, y2 = (1, 2, 3, 4)
            coord_list = [x1, y1, x2, y2]
            for x in coord_list:
                x = 0

            # there can be noise in the video so ignore objects with small areas
            if (area1 > 200000):
                # x and y coordinates of the center of the object is found by dividing the 1,0 and 0,1 moments by the area
                x1 = int(cv.GetSpatialMoment(moments1, 1, 0) / area1)
                y1 = int(cv.GetSpatialMoment(moments1, 0, 1) / area1)

            # draw circle
            cv.Circle(img, (x1, y1), 2, (0, 255, 0), 20)

            # write x and y position
            cv.PutText(img, str(x1) +', '+str(y1), (x1, y1 + 20), font, 255)  # Draw the text

            if (area2 > 100000):
                # x and y coordinates of the center of the object is found by dividing the 1,0 and 0,1 moments by the area
                x2 = int(cv.GetSpatialMoment(moments2, 1, 0) / area2)
                y2 = int(cv.GetSpatialMoment(moments2, 0, 1) / area2)

                # draw circle
                cv.Circle(img, (x2, y2), 2, (0, 255, 0), 20)

            cv.PutText(img, str(x2) +', '+str(y2), (x2, y2 + 20), font, 255)  # Draw the text
            cv.Line(img, (x1, y1), (x2, y2), (0, 255, 0), 4, cv.CV_AA)
            # draw line and angle
            cv.Line(img, (x1, y1), (cv.GetSize(img)[0], y1), (100, 100, 100, 100), 4, cv.CV_AA)
            x1 = float(x1)
            y1 = float(y1)
            x2 = float(x2)
            y2 = float(y2)
            angle = int(math.atan((y1 - y2) / (x2 - x1)) * 180 / math.pi)
            cv.PutText(img, str(angle), (int(x1) + 50, (int(y2) + int(y1)) / 2), font, 255)

            # cv.WriteFrame(writer,img)

            # display frames to users
            cv.ShowImage('Target', img)
            cv.ShowImage('Threshold1', threshold_img1)
            cv.ShowImage('Threshold2', threshold_img2)
            cv.ShowImage('hsv', hsv_img)
            # Listen for ESC or ENTER key
            c = cv.WaitKey(7) % 0x100
            if c == 27 or c == 10:
                break
            cv.DestroyAllWindows()
Exemplo n.º 5
0
def drawGraph(arraySrc,
              nArrayLength,
              imageDst,
              minV=0,
              maxV=0,
              width=0,
              height=0,
              graphLabel='',
              showScale=True):
    w, h, b = width, height, 10
    w = nArrayLength + b * 2 if w <= 20 else w
    h = 220 if h <= 20 else h
    s, xscale = h - b * 2, 1.0
    if nArrayLength > 1:
        xscale = (w - b * 2) / float(nArrayLength - 1)

    # Assume imageDst is a numpy array
    if not imageDst:
        imageGraph = Image.new('RGB', (w, h), WHITE)
        # imageGraph.show()
    else:
        imageGraph = imageDst
    if not imageGraph:
        print 'Error in drawGraph'
        return

    colorGraph = getGraphColor()
    if abs(minV) < 1e-7 and abs(maxV) < 1e-7:
        for i in range(nArrayLength):
            v = arraySrc[i]
            minV = v if v < minV else minV
            maxV = v if v > maxV else maxV

    diffV = maxV - minV
    diffV = 1e-7 if diffV == 0 else diffV
    fscale = float(s) / diffV

    # Draw the horizontal and vertical axis
    y0 = cv2.Round(minV * fscale)
    cv2.Line(imageGraph, (b, h - (b - y0)), (w - b, h - (b - y0)), BLACK)
    cv2.Line(imageGraph, (b, h - b), (b, h - (b + s)), BLACK)

    # Write the scale of the y and x axis
    if showScale:
        #cv2.putText(frame, text, (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, size, color, thickness)
        cv2.putText(imageGraph, '%.1f' % maxV, (1, b + 4),
                    cv2.FONT_HERSHEY_PLAIN, 2, GREY, 0.1)
        cv2.putText(imageGraph, '%d' % (nArrayLength - 1),
                    (w - b + 4 - 5 * 2, h / 2 + 10), cv2.FONT_HERSHEY_PLAIN, 2,
                    GREY, 0.1)

    # Draw the values
    ptPrev = (b, h - (b - y))
    for i in range(nArrayLength):
        y = cv2.Round((arraySrc[i] - minV) * fscale)
        x = cv2.Round(i * xscale)
        ptNew = (b + x, h - (b + y))
        cv2.Line(imageGraph, ptPrev, ptNew, colorGraph, 1, cv2.CV_AA)
        ptPrev = ptNew

    # Write the graph label
    if graphLabel != None and graphLabel != '':
        cv2.putText(imageGraph, graphLabel, (30, 10), cv2.FONT_HERSHEY_PLAIN,
                    2, GREY, 0.1)

    return imageGraph
Exemplo n.º 6
0
def CamGui():
    capture = cv.VideoCapture(0)
    width = int(capture.get(cv.CAP_PROP_FRAME_WIDTH))
    height = int(capture.get(cv.CAP_PROP_FRAME_HEIGHT))
    prev_gray = cv.CreateImage((width, height), 8, 1)
    gray = cv.CreateImage((width, height), 8, 1)

    # Will hold the pyr frame at t-1
    prevPyr = cv.CreateImage((height / 3, width + 8), 8, cv.CV_8UC1)
    currPyr = cv.CreateImage((height / 3, width + 8),
                             8, cv.CV_8UC1)  # idem at t

    max_count = 500
    qLevel = 0.01
    minDist = 10
    prev_points = []  # Points at t-1
    curr_points = []  # Points at t
    lines = []  # To keep all the lines overtime

    while True:
        frame = cv.QueryFrame(capture)
        cv.CvtColor(frame, gray, cv.CV_BGR2GRAY)  # Convert to gray
        output = cv.CloneImage(frame)

        prev_points = cv.GoodFeaturesToTrack(
            gray, None, None, max_count, qLevel, minDist)
        curr_points, status, err = cv.CalcOpticalFlowPyrLK(
            prev_gray, gray, prevPyr, currPyr, prev_points, (10, 10), 3, (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03), 0)

        # If points status are ok and distance not negligible keep the point
        k = 0
        for i in range(len(curr_points)):
            nb = abs(int(prev_points[i][0]) - int(curr_points[i][0])) + \
                abs(int(prev_points[i][1]) - int(curr_points[i][1]))
            if status[i] and nb > 2:
                prev_points[k] = prev_points[i]
                curr_points[k] = curr_points[i]
                k += 1

        prev_points = prev_points[:k]
        curr_points = curr_points[:k]
        # At the end only interesting points are kept

        # Draw all the previously kept lines otherwise they would be lost the next frame
        for (pt1, pt2) in lines:
            cv.Line(frame, pt1, pt2, (255, 255, 255))

        # Draw the lines between each points at t-1 and t
        for prevpoint, point in zip(prev_points, curr_points):
            prevpoint = (int(prevpoint[0]), int(prevpoint[1]))
            cv.Circle(frame, prevpoint, 15, 0)
            point = (int(point[0]), int(point[1]))
            cv.Circle(frame, point, 3, 255)
            cv.Line(frame, prevpoint, point, (255, 255, 255))
            # Append current lines to the lines list
            lines.append((prevpoint, point))

        cv.Copy(gray, prev_gray)  # Put the current frame prev_gray
        prev_points = curr_points

        cv.ShowImage("The Video", frame)
        #cv.WriteFrame(writer, frame)
        c = cv.WaitKey(1)
        if c == 27:  # Esc on Windows
            break
Exemplo n.º 7
0
# 2 main cv2 line functions
#1) cv2.HoughLines(binarized image,(p) accuracy, (theta)theta,threshold)
# Threshold here is the minimum vote for it to be considered a line

#2) Probabilistic Hough Lines
# cv2.HoughLinesP(binarized image,(p) accuracy, (theta)theta,threshold,minimum line length,max line gap)
# Idea is that it takes only  a random subset of points suddicient enough for line detection
# Also returns the start and end points of line unlike the previous function

image = cv2.imread('../images/sudoku.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

edges = cv2.Canny(gray, 100, 170, apertureSize=3)

lines = cv2.HoughLines(edges, 1, np.pi / 1 * 180, 240)

for rho, theta in lines[0]:
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a * rho
    y0 = b * rho
    x1 = int(x0 + 1000 * (-b))
    y1 = int(y0 + 1000 * (a))
    x2 = int(x0 - 1000 * (-b))
    y2 = int(y0 - 1000 * (a))
    cv2.Line(image, (x1, y1), (x2, y2), (255, 0, 0), 2)

cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()