예제 #1
0
    def Get_gradient_img(self):
        '''
        calculate the intensity gradient of the image
        :return: the gradient image
        '''
        print('calculating gradient image')

        new_img_x = np.zeros([self.y, self.x], dtype=np.float)
        new_img_y = np.zeros([self.y, self.x], dtype=np.float)
        for i in range(0, self.x):
            for j in range(0, self.y):
                if j == 0:
                    new_img_y[j][i] = 1
                else:
                    new_img_y[j][i] = np.sum(
                        np.array([[self.img[j - 1][i]], [self.img[j][i]]]) *
                        self.y_kernel)
                if i == 0:
                    new_img_x[j][i] = 1
                else:
                    new_img_x[j][i] = np.sum(
                        np.array([self.img[j][i - 1], self.img[j][i]]) *
                        self.x_kernel)

        gradient_img, self.angle = cv2.cartToPolar(new_img_x, new_img_y)
        self.angle = np.tan(self.angle)
        self.img = gradient_img.astype(np.uint8)
        return self.img
예제 #2
0
    def performFourier(self):
        #to gray
        grayImg = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY)

        self.grayImg = grayImg

        #give freq transform and shift (cuz zero freq is in the cornern - we want it in the middle)
        f = np.fft.fft2(grayImg)
        fshift = np.fft.fftshift(f)

        self.fshift = fshift

        #give magnitude and phase
        mag, ang = cv2.cartToPolar(fshift.real, fshift.imag)

        self.mag = mag
        self.ang = ang

        #display it
        plt.subplot(221), plt.imshow(grayImg, cmap='gray')
        plt.title('Original Grayscale Image'), plt.xticks([]), plt.yticks([])
        plt.subplot(223), plt.imshow(20 * np.log(np.abs(mag)), cmap='gray')
        plt.title('Magnitude of Fourier Transform'), plt.xticks(
            []), plt.yticks([])
        plt.subplot(224), plt.imshow(20 * np.log(ang), cmap='gray')
        plt.title('Phase of Fourier Transform'), plt.xticks([]), plt.yticks([])
        plt.show()
예제 #3
0
 def describe(self, pattern_img, eps=1e-7):
     # Calculate Sobel gradient
     grad_x = cv2.Sobel(pattern_img, cv2.CV_32F, 1, 0, ksize=3)
     grad_y = cv2.Sobel(pattern_img, cv2.CV_32F, 0, 1, ksize=3)
     grad_mag, grad_ang = cv2.cartToPolar(grad_x,
                                          grad_y,
                                          angleInDegrees=True)
     hist = self.get_grad_features(grad_mag, grad_ang).astype(np.float32)
     return hist
예제 #4
0
def roughlyBlur(src, dsc, allflow, fps=30):
    # print("roughly blur drawing")
    scale = 1.0
    if fps > 30:
        scale = float(fps) / 30.0
    rows, cols, _ = src.shape
    test_width = int(cols / test_round_x)
    test_height = int(rows / test_round_y)
    test_x = 0
    # round_x_progress.reset(total=test_round_x)

    # load all roi's mag and ang
    arr = []
    while test_x < cols:  #round_x_progress:
        # round_x_progress.set_description('roughly blur drawing')
        startx = test_x
        endx = startx + test_width if startx + test_width < cols else cols
        test_x = endx
        if startx == endx:
            break
        test_y = 0

        while test_y < rows:
            starty = test_y
            endy = starty + test_height if starty + test_height < rows else rows
            test_y = endy
            if starty == endy:
                break

            flow_roi = allflow[starty:endy, startx:endx]
            flow_avg_0 = np.array((np.average(flow_roi[..., 0])))
            flow_avg_1 = np.array((np.average(flow_roi[..., 1])))
            mag, ang = cv2.cartToPolar(flow_avg_0, flow_avg_1)
            mag = (mag * scale)  #/ 2.0
            ang = -ang * 180.0 / math.pi  # 这原本是弧度!
            mag_using = mag[0, 0]
            ang_using = ang[0, 0]
            region = (starty, endy, startx, endx)

            item = (region, mag_using, ang_using)
            arr.append(item)
    sortedarr = sorted(arr, key=lambda iii: iii[1], reverse=True)
    count = len(sortedarr)
    for index in range(count):
        if shouldprintblurprogress and (random.randint(1, 50) % 10) == 0:
            print("test", index, count)
        region, mag, ang = sortedarr[index]
        if math.isinf(mag):
            continue
        drawRegionBlur(src=src,
                       dsc=dsc,
                       region=region,
                       mag=mag,
                       ang=ang,
                       maskvalue=1.5)
    return dsc
예제 #5
0
def hog(img):
    gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
    gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
    mag, ang = cv2.cartToPolar(gx, gy)
    bins = np.int32(bin_n*ang/(2*np.pi))    # quantizing binvalues in (0...16)
    bin_cells = bins[:10,:10], bins[10:,:10], bins[:10,10:], bins[10:,10:]
    mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
    hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
    hist = np.hstack(hists)     # hist is a 64 bit vector
    # cv2.imshow('hog',hist)
    return hist
예제 #6
0
def main():
    drone = tellopy.Tello()

    try:
        drone.connect()
        drone.wait_for_connection(60.0)
        pygame.init()
        pygame.joystick.init()

        container = av.open(drone.get_video_stream())
        frameCount = 0  # Stores the current frame being processed
        frame1 = None  # Store variables for first frame
        frame2 = None  # Store variables for second frame
        prvs = None
        hsv = None

        while True:
            for frame in container.decode(video=0):
                checkController()
                frameCount += 1
                if frameCount == 1:  # If first frame
                    frame1 = cv2.cvtColor(np.array(frame.to_image()),
                                          cv2.COLOR_RGB2BGR)
                    prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
                    hsv = np.zeros_like(frame1)
                    hsv[..., 1] = 255
                else:  # If not first frame
                    frame2 = cv2.cvtColor(np.array(frame.to_image()),
                                          cv2.COLOR_RGB2BGR)
                    next = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
                    flow = cv2.calcOpticalFlowFarneback(
                        prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0)
                    mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
                    hsv[..., 0] = ang * 180 / np.pi / 2
                    hsv[..., 2] = cv2.normalize(mag, None, 0, 255,
                                                cv2.NORM_MINMAX)
                    bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
                    cv2.imshow('frame2', bgr)
                    k = cv2.waitKey(30) & 0xff
                    if k == 27:
                        break
                    elif k == ord('s'):
                        cv2.imwrite('opticalfb.png', frame2)
                        cv2.imwrite('opticalhsv.png', bgr)
                    prvs = next
                print(frameCount)

    except Exception as ex:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        traceback.print_exception(exc_type, exc_value, exc_traceback)
        print(ex)
    finally:
        drone.quit()
        cv2.destroyAllWindows()
예제 #7
0
def optical_flow(img: np.ndarray, previous: np.ndarray) -> np.ndarray:
    mask = np.zeros(shape=img.shape, dtype=np.uint8)
    mask[..., 1] = 255

    gray = to_gray(img)
    previous = to_gray(previous)

    flow = cv2.calcOpticalFlowFarneback(previous, gray, None, 0.5, 3, 15, 3, 5,
                                        1.2, 0)

    magnitude, polar_angle = cv2.cartToPolar(flow[..., 0], flow[..., 1])
    mask[..., 0] = polar_angle * 180 / np.pi / 2
    mask[..., 2] = cv2.normalize(magnitude, None, 0, 255, cv2.NORM_MINMAX)
    rgb = cv2.cvtColor(mask, cv2.COLOR_HSV2BGR)
    return rgb
예제 #8
0
 def optical_flow(previous_, current_):
     """
     光流
     :param previous_:
     :param current_:
     :return:
     """
     previous = cv2.cvtColor(previous_, cv2.COLOR_BGR2GRAY)
     current = cv2.cvtColor(current_, cv2.COLOR_BGR2GRAY)
     flow = cv2.calcOpticalFlowFarneback(previous, current, None, 0.5, 3, 15, 3, 5, 1.2, 0)
     hsv = np.zeros_like(previous_)
     mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
     hsv[..., 0] = ang * 180 / np.pi / 2
     hsv[..., 1] = 255
     hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
     flow = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
     return flow
예제 #9
0
def Dense_Optical_Flow(image):
    image_old = image
    frame1 = image
    next = image
    hsv = np.zeros([frame1.shape[0], frame1.shape[1], 3], dtype=np.uint8)
    hsv[..., 1] = 255
    while (1):
        flow = cv2.calcOpticalFlowFarneback(image_old, next, None, 0.5, 3, 50,
                                            3, 5, 1.2, 0)
        mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
        hsv[..., 0] = ang * 180 / np.pi / 2
        hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
        bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
        image_old = next
        k = cv2.waitKey(30) & 0xff
        if k == 27:
            break
        next = (yield bgr)
예제 #10
0
def gradient(img: np.ndarray, kernel_size: int = 3) -> np.ndarray:
    """
    calculates the magnitude and polar_angle of 2D vectors using Sobel operator on both axis.
    Args:
        img: image as numpy array
        kernel_size: kernel size of Sobel operator (NxN)

    Returns:
        magnitude of gradients in image shape
    """
    img = np.float32(img) / 255.0

    sy = sobel_y(img, kernel_size=kernel_size)
    sx = sobel_x(img, kernel_size=kernel_size)

    mag, polar_angle = cv2.cartToPolar(sx, sy, angleInDegrees=True)

    mag = np.ndarray.astype(mag, dtype=np.float32)
    mag = to_bgr(mag)

    return mag
예제 #11
0
    def read_data(self):
        print("Hello")
        prvs = None
        hsv = None
        num_img = 0
        while (cap.isOpened()):
            ret, frame = cap.read()
            if frame is not None:
                if (num_img == 0):
                    num_img += 1
                    prvs = frame.copy()
                    prvs_gray = cv2.cvtColor(prvs, cv2.COLOR_RGB2GRAY)
                    continue

                curr = frame.copy()
                curr_gray = cv2.cvtColor(curr, cv2.COLOR_RGB2GRAY)
                flow = cv2.calcOpticalFlowFarneback(prvs_gray, curr_gray, None,
                                                    0.5, 3, 15, 3, 5, 1.2, 0)
                hsv = np.zeros_like(prvs)
                mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
                hsv[..., 0] = ang * 180 / np.pi / 2
                hsv[..., 1] = 255
                hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
                rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

                number_str = str(num_img)
                print("Saving file number: {}".format(number_str), end='\r')
                zero_filled_number = number_str.zfill(5)
                cv2.imwrite(
                    PATH_TO_SAVE_DIRECTORY + zero_filled_number + '.jpg', rgb)
                num_img += 1

                prvs = curr
                cv2.imshow('frame2', rgb)
                k = cv2.waitKey(30) & 0xff
                if k == 27:
                    break
            else:
                break
예제 #12
0
ret, frame1 = cap.read()
prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
hsv = np.zeros(frame1.shape, np.uint8)  # simular with np.zeros_like
hsv[..., 1] = 255

while (1):
    ret, frame2 = cap.read()
    if ret == False:
        break
    next = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)

    flow = cv2.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5,
                                        1.2, 0)

    mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
    hsv[..., 0] = ang * 180 / np.pi / 2
    hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
    rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

    cv2.imshow('frame2', rgb)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
    elif k == ord('s'):
        cv2.imwrite('opticalfb.png', frame2)
        cv2.imwrite('opticalhsv.png', rgb)
    prvs = next
cv2.waitKey(0)
cap.release()
cv2.destroyAllWindows()
예제 #13
0
def getVideo():
    global tracks
    global track_len
    global detect_interval
    global frame_idx
    global VIDEO_SCALE
    global videoLabel
    global typeOfVideo
    global connectingToDrone
    global takePicture
    frameCount = 0  # Stores the current frame being processed
    frame1Optical = None  # Store variables for first frame
    frame2Optical = None  # Store variables for second frame
    prvs = None
    hsv = None

    try:
        while connectingToDrone:
            #time.sleep(0.03)
            for frameRaw in container.decode(video=0):
                checkController()
                if takePicture:
                    frame1 = np.array(frameRaw.to_image())
                    #im = Image.fromarray(frame1, 'RGB')
                    cv.imwrite(
                        "pics/" + datetime.datetime.now().isoformat() + ".jpg",
                        frame1)
                    #imageTk = ImageTk.PhotoImage(image=im)
                    #videoLabel.configure(image=imageTk)
                    #videoLabel.image = imageTk
                    #videoLabel.update()
                    takePicture = False
                if typeOfVideo.get() == "Canny Edge Detection":
                    frame1 = np.array(frameRaw.to_image())
                    frame1 = cv.resize(frame1, (0, 0),
                                       fx=VIDEO_SCALE,
                                       fy=VIDEO_SCALE)
                    frameCanny = cv.Canny(frame1, 50, 100)
                    im = Image.fromarray(frameCanny)
                    imageTk = ImageTk.PhotoImage(image=im)
                    videoLabel.configure(image=imageTk)
                    videoLabel.image = imageTk
                    videoLabel.update()
                elif typeOfVideo.get() == "LK Optical Flow":
                    frame1 = np.array(frameRaw.to_image())
                    frame = frame1
                    frame = cv.resize(frame1, (0, 0),
                                      fx=VIDEO_SCALE,
                                      fy=VIDEO_SCALE)
                    frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
                    vis = frame.copy()
                    if len(tracks) > 0:
                        img0, img1 = prev_gray, frame_gray
                        p0 = np.float32([tr[-1]
                                         for tr in tracks]).reshape(-1, 1, 2)
                        p1, _st, _err = cv.calcOpticalFlowPyrLK(
                            img0, img1, p0, None, **lk_params)
                        p0r, _st, _err = cv.calcOpticalFlowPyrLK(
                            img1, img0, p1, None, **lk_params)
                        d = abs(p0 - p0r).reshape(-1, 2).max(-1)
                        good = d < 1
                        new_tracks = []

                        for tr, (x,
                                 y), good_flag in zip(tracks,
                                                      p1.reshape(-1, 2), good):
                            if not good_flag:
                                continue
                            tr.append((x, y))
                            if len(tr) > track_len:
                                del tr[0]
                            new_tracks.append(tr)
                            cv.circle(vis, (x, y), 2, (0, 255, 0), -1)
                        tracks = new_tracks
                        cv.polylines(vis, [np.int32(tr) for tr in tracks],
                                     False, (0, 255, 0))
                        draw_str(vis, (20, 20),
                                 'track count: %d' % len(tracks))

                    if frame_idx % detect_interval == 0:
                        mask = np.zeros_like(frame_gray)
                        mask[:] = 255
                        for x, y in [np.int32(tr[-1]) for tr in tracks]:
                            cv.circle(mask, (x, y), 5, 0, -1)
                        p = cv.goodFeaturesToTrack(frame_gray,
                                                   mask=mask,
                                                   **feature_params)
                        if p is not None:
                            for x, y in np.float32(p).reshape(-1, 2):
                                tracks.append([(x, y)])

                    frame_idx += 1
                    prev_gray = frame_gray
                    #cv.imshow('Tello Dense Optical - Middlebury Research', vis)
                    im = Image.fromarray(vis, 'RGB')
                    imageTk = ImageTk.PhotoImage(image=im)
                    videoLabel.configure(image=imageTk)
                    videoLabel.image = imageTk
                    videoLabel.update()
                elif typeOfVideo.get() == "Optical Flow":
                    frameCount += 1
                    if frameCount == 1:  # If first frame
                        frame1Optical = cv.cvtColor(
                            np.array(frameRaw.to_image()), cv.COLOR_RGB2BGR)
                        prvs = cv.cvtColor(frame1Optical, cv.COLOR_BGR2GRAY)
                        hsv = np.zeros_like(frame1Optical)
                        hsv[..., 1] = 255
                    else:  # If not first frame
                        frame2Optical = cv.cvtColor(
                            np.array(frameRaw.to_image()), cv.COLOR_RGB2BGR)
                        next = cv.cvtColor(frame2Optical, cv.COLOR_BGR2GRAY)
                        flow = cv.calcOpticalFlowFarneback(
                            prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0)
                        mag, ang = cv.cartToPolar(flow[..., 0], flow[..., 1])
                        hsv[..., 0] = ang * 180 / np.pi / 2
                        hsv[..., 2] = cv.normalize(mag, None, 0, 255,
                                                   cv.NORM_MINMAX)
                        bgr = cv.cvtColor(hsv, cv.COLOR_HSV2BGR)
                        im = Image.fromarray(
                            cv.resize(frame2Optical, (0, 0),
                                      fx=VIDEO_SCALE,
                                      fy=VIDEO_SCALE))
                        imageTk = ImageTk.PhotoImage(image=im)
                        videoLabel.configure(image=imageTk)
                        videoLabel.image = imageTk
                        videoLabel.update()
                        k = cv.waitKey(30) & 0xff
                        if k == 27:
                            break
                        prvs = next
                elif typeOfVideo.get() == "Grayscale":
                    frame = cv.cvtColor(np.array(frameRaw.to_image()),
                                        cv.COLOR_RGB2BGR)
                    frame1 = cv.resize(frame, (0, 0),
                                       fx=VIDEO_SCALE,
                                       fy=VIDEO_SCALE)

                    im = Image.fromarray(frame1, 'RGB')
                    gray = im.convert('L')

                    # Using numpy, convert pixels to pure black or white
                    bw = np.asarray(gray).copy()

                    im = Image.fromarray(bw)
                    imageTk = ImageTk.PhotoImage(image=im)
                    videoLabel.configure(image=imageTk)
                    videoLabel.image = imageTk
                    videoLabel.update()
                elif typeOfVideo.get() == "BGR":
                    frame = cv.cvtColor(np.array(frameRaw.to_image()),
                                        cv.COLOR_RGB2BGR)
                    frame1 = cv.resize(frame, (0, 0),
                                       fx=VIDEO_SCALE,
                                       fy=VIDEO_SCALE)
                    im = Image.fromarray(frame1)
                    imageTk = ImageTk.PhotoImage(image=im)
                    videoLabel.configure(image=imageTk)
                    videoLabel.image = imageTk
                    videoLabel.update()
                elif typeOfVideo.get() == "Black & White":
                    frame = cv.cvtColor(np.array(frameRaw.to_image()),
                                        cv.COLOR_RGB2BGR)
                    frame1 = cv.resize(frame, (0, 0),
                                       fx=VIDEO_SCALE,
                                       fy=VIDEO_SCALE)

                    im = Image.fromarray(frame1, 'RGB')
                    gray = im.convert('L')

                    # Using numpy, convert pixels to pure black or white
                    bw = np.asarray(gray).copy()

                    # Pixel range is 0...255, 256/2 = 128
                    bw[bw < 128] = 0  # Black
                    bw[bw >= 128] = 255  # White
                    im = Image.fromarray(bw)
                    imageTk = ImageTk.PhotoImage(image=im)
                    videoLabel.configure(image=imageTk)
                    videoLabel.image = imageTk
                    videoLabel.update()
                else:  # typeOfVideo.get() == "Normal":
                    frame1 = np.array(frameRaw.to_image())
                    frame1 = cv.resize(frame1, (0, 0),
                                       fx=VIDEO_SCALE,
                                       fy=VIDEO_SCALE)
                    im = Image.fromarray(frame1, 'RGB')
                    imageTk = ImageTk.PhotoImage(image=im)
                    videoLabel.configure(image=imageTk)
                    videoLabel.image = imageTk
                    videoLabel.update()

            ch = cv.waitKey(1)
            if ch == 27:
                break
    except Exception as ex:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        traceback.print_exception(exc_type, exc_value, exc_traceback)
        print(ex)