def USM(src):
    val = 3
    blur = cv2.GaussianBlur(src, (7, 7), 3)
    val = 4
    blur = cv2.GaussianBlur(src, (5, 5), 0)
    res = cv2.addWeighted(src, val, blur, 1.0 - val, 0)
    return res
示例#2
0
def calculate_weight_map(tar_img, ref_img, mask):
    tar_img_YUV = cv2.GaussianBlur(cv2.cvtColor(tar_img, cv2.COLOR_BGR2YUV),
                                   (5, 5), 10)
    ref_img_YUV = cv2.GaussianBlur(cv2.cvtColor(ref_img, cv2.COLOR_BGR2YUV),
                                   (5, 5), 10)
    tar_img_Y = tar_img_YUV[:, :, 0]
    ref_img_Y = ref_img_YUV[:, :, 0]

    YUV_diff = np.abs(tar_img_YUV - ref_img_YUV)
    color_diff = cv2.convertScaleAbs(YUV_diff[:, :, 0] * 0.5 +
                                     YUV_diff[:, :, 1] * 0.25 +
                                     YUV_diff[:, :, 2] * 0.25)
    color_diff[mask.overlap == 0] = 0

    print('finish YUV_diff')

    grad_diff_mag = getGradDiff(tar_img_Y, ref_img_Y)
    grad_diff_mag[mask.overlap == 0] = 0
    print('finish grad_diff')

    color_grad_diff_sum = grad_diff_mag * 100 + color_diff

    filter_bank = getGarborFilterBank(tar_img_Y, ref_img_Y)
    # print('finish gabor filter bank')

    h, w = tar_img_Y.shape
    tar_result = np.zeros((h, w))
    for i in range(len(filter_bank)):
        temp = cv2.filter2D(tar_img_Y, cv2.CV_64FC1, filter_bank[i])
        tar_result += temp**2
    tar_result = np.sqrt(tar_result)
    tar_result[mask.overlap == 0] = 0
    print('finish gabor feature target')

    ref_result = np.zeros((h, w))
    for i in range(len(filter_bank)):
        temp = cv2.filter2D(ref_img_Y, cv2.CV_64FC1, filter_bank[i])
        ref_result += temp**2

    ref_result = np.sqrt(ref_result)
    ref_result[mask.overlap == 0] = 0
    print('finish gabor feature reference')

    gabor_result = ref_result + tar_result

    weight_map = np.multiply(gabor_result, color_grad_diff_sum)
    print('finish weight map')

    # cv2.imwrite('./YUV_diff.png',color_diff)
    # cv2.imwrite('./gradian_diff.png',(grad_diff_mag/np.max(grad_diff_mag)*255).astype(np.uint8))
    # cv2.imwrite('./color_grad_diff.png',(color_grad_diff_sum/np.max(color_grad_diff_sum)*255).astype(np.uint8))
    # cv2.imwrite(f'./gabor/ref_gobar_final.png',(ref_result/np.max(ref_result)*255).astype(np.uint8) )
    # cv2.imwrite(f'./gabor/gobar_final.png',(gabor_result/np.max(gabor_result)*255).astype(np.uint8) )
    # cv2.imwrite(f'./gabor/tar_gobar_final.png',(tar_result/np.max(tar_result)*255).astype(np.uint8) )
    # cv2.imwrite(f'./gabor/W_final.png',(weight_map-np.mean(weight_map))/np.std(weight_map)*255)
    return weight_map
示例#3
0
def compare_backgrounds(video_path, frame_n):
    background_50 = background_vid(video_path, 200, 50)
    background_90 = background_vid(video_path, 200, 90)
    background_95 = background_vid(video_path, 200, 95)

    cap = cv2.VideoCapture(video_path)

    for i in range(frame_n):
        ret, frame = cap.read()

    image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    fig, axs = plt.subplots(3, 3)
    axs[0, 0].imshow(background_50)
    axs[0, 1].imshow(background_95)
    axs[0, 2].imshow(background_90)

    axs[1, 0].imshow(cv2.absdiff(image, background_50))
    axs[1, 1].imshow(cv2.absdiff(image, background_95))
    axs[1, 2].imshow(cv2.absdiff(image, background_90))

    axs[2, 0].imshow(cv2.absdiff(image, background_50) > 35)
    axs[2, 1].imshow(cv2.absdiff(image, background_95) > 35)
    axs[2, 2].imshow(cv2.absdiff(image, background_90) > 35)

    for i in range(3):
        for j in range(3):
            axs[i, j].axis('off')

    axs[0, 0].title.set_text('percentile 50')
    axs[0, 1].title.set_text('percentile 95')
    axs[0, 2].title.set_text('percentile 90')

    image_b = cv2.GaussianBlur(image, (7, 7), 0)
    background_90_b = cv2.GaussianBlur(background_90, (7, 7), 0)

    fig, axs = plt.subplots(3, 3)
    axs[0, 0].imshow(image)
    axs[0, 1].imshow(image)
    axs[0, 2].imshow(image_b)
    axs[1, 0].imshow(cv2.absdiff(image, background_90))
    axs[1, 1].imshow(cv2.absdiff(image_b, background_90_b))
    axs[1, 2].imshow(cv2.absdiff(image, background_90_b))
    axs[2, 0].imshow(cv2.absdiff(image, background_90) > 35)
    axs[2, 1].imshow(cv2.absdiff(image, background_90_b) > 35)
    axs[2, 2].imshow(cv2.absdiff(image_b, background_90_b) > 35)

    for i in range(3):
        for j in range(3):
            axs[i, j].axis('off')

    axs[0, 0].title.set_text('no blurring')
    axs[0, 1].title.set_text('background blurred')
    axs[0, 2].title.set_text('both blurred')
示例#4
0
def correct_colours(im1, im2, landmarks1):
    blur_amount = COLOUR_CORRECT_BLUR_FRAC * numpy.linalg.norm(
        numpy.mean(landmarks1[LEFT_EYE_POINTS], axis=0) -
        numpy.mean(landmarks1[RIGHT_EYE_POINTS], axis=0))
    blur_amount = int(blur_amount)
    if blur_amount % 2 == 0:
        blur_amount += 1
    im1_blur = cv2.GaussianBlur(im1, (blur_amount, blur_amount), 0)
    im2_blur = cv2.GaussianBlur(im2, (blur_amount, blur_amount), 0)
    im2_blur += 128 * (im2_blur <= 1.0).astype(im2_blur.dtype)

    return (im2.astype(numpy.float64) * im1_blur.astype(numpy.float64) /
            im2_blur.astype(numpy.float64))
示例#5
0
def get_face_mask(img, landmarks):
    img = numpy.zeros(img.shape[:2], dtype=numpy.float64)

    for group in OVERLAY_POINTS:
        draw_convex_hull(img, landmarks[group], color=1)

    img = numpy.array([img, img, img]).transpose((1, 2, 0))

    img = (cv2.GaussianBlur(img,
                            (FEATHER_AMOUNT, FEATHER_AMOUNT), 0) > 0) * 1.0
    img = cv2.GaussianBlur(img, (FEATHER_AMOUNT, FEATHER_AMOUNT), 0)

    return img
示例#6
0
def highpassB():
    image = imageInit
    image = grayScale()

    edged = image - cv2.GaussianBlur(image, (0, 0), 3) + 127
    show_img(ImageTk.PhotoImage(Img.fromarray(edged)))
    return edged
示例#7
0
def imgReadAndConvert(imgfilename):
    original_img = cv2.imread(imgfilename)
    # inspect error object
    img_copy = original_img.copy()
    height = img_copy.shape[0]
    width = img_copy.shape[1]
    #Image resize(prevent overflow)
    if (height * width * 1000) > 2 ^ 31:
        resize = img_copy
    elif (height * width > 2 ^ 31):
        resize = cv2.resize(img_copy,
                            dsize=(0.0001, int(0.01 * height / width)),
                            interpolation=cv2.INTER_AREA)
    else:
        resize = cv2.resize(img_copy,
                            dsize=(1000, int(1000 * height / width)),
                            interpolation=cv2.INTER_AREA)
    #GaussianBlur
    blur = cv2.GaussianBlur(resize, (5, 5), 0)
    #Image graying: reduce the influence of color
    gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
    #Noises removal from outside the contours
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
    img_open = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel)
    #Covert image into binary
    bin_img = cv2.adaptiveThreshold(img_open, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                    cv2.THRESH_BINARY_INV, 21, 20)
    #Reverse image color
    bin_img = cv2.bitwise_not(bin_img)
    return bin_img
示例#8
0
def fast_corner_detect(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    gray = cv.GaussianBlur(gray, (9, 9), 25)  # sigma
    fast = cv.FastFeatureDetector_create()
    kp = fast.detect(gray, None)
    image = cv.drawKeypoints(image, kp, None, color=(255, 0, 0))
    cv.imshow('image for fast', image)
示例#9
0
def img_preprocess(img):
    img = img[60:135, :, :]
    img = cv2.cvtColor(img, cv2.COLOR_RGB2YUV)
    img = cv2.GaussianBlur(img, (3, 3), 0)
    img = cv2.resize(img, (200, 66))
    img = img / 255
    return img
示例#10
0
	def _get_balls(self, image, color, bounds, cam_id = 1):

		# converting the input stream into HSV color space
		hsv_conv_img = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

		bright_mask = cv2.inRange(hsv_conv_img, bounds[0], bounds[1])

		blurred_mask = cv2.GaussianBlur(bright_mask, (9, 9), 3, 3)

		# some morphological operations (closing) to remove small blobs
		erode_element = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
		dilate_element = cv2.getStructuringElement(cv2.MORPH_RECT, (8, 8))
		eroded_mask = cv2.erode(blurred_mask, erode_element)
		dilated_mask = cv2.dilate(eroded_mask, dilate_element)

		detected_circles = cv2.HoughCircles(dilated_mask, cv2.HOUGH_GRADIENT, **HOUGH_PARAMS)
		balls = []

		if detected_circles is not None:
			for circle in detected_circles[0, :]:
				x = circle[0]
				y = circle[1]
				r = circle[2]
				circled_orig = cv2.circle(image, (x, y), r, (0, 255, 0), thickness=2)
				
				# locate detected balls
				# https_www.pyimagesearch.com/?url=https%3A%2F%2Fwww.pyimagesearch.com%2F2015%2F01%2F19%2Ffind-distance-camera-objectmarker-using-python-opencv%2F
				distance = self.find_position(x, y, r, cam_id)
				balls.append((x, y, r, color))
			cv2.imshow('circled_orig', circled_orig)
			cv2.waitKey(0)

		return balls
def get_balls(image, color, bounds, camera):

    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    bright = cv2.inRange(hsv, bounds[0], bounds[1])
    blur = cv2.GaussianBlur(bright, **GAUSS_PARAMS)

    # detected_circles = cv2.HoughCircles(blurred_mask, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)
    detected_circles = cv2.HoughCircles(blur, cv2.HOUGH_GRADIENT,
                                        **HOUGH_PARAMS)
    balls = []

    if detected_circles is not None:
        for circle in detected_circles[0, :]:
            x = circle[0]
            y = circle[1]
            r = circle[2]

            # locate detected balls
            position, distance = find_position(x, y, r, camera)
            circled_orig = cv2.circle(image, (x, y),
                                      r, (0, 255, 0),
                                      thickness=1)

            font = cv2.FONT_HERSHEY_SIMPLEX
            # x = x - 10
            print(x, y)
            cv2.putText(circled_orig, str(format(distance, '.2f')), (x, y),
                        font, 0.5, (255, 255, 255), 2, cv2.LINE_AA)

            balls.append((x, y, r, color))

        display_image(circled_orig)

    return balls
示例#12
0
def blur(image, rect, r=3, kernelSize=(0, 0)):
    '''对图像指定区域进行处理,r为模糊半径,r为4在某些边缘会出现颜色bug
        flag:0高斯模糊,1均值模糊,2中值模糊,3双边模糊'''
    arr = pygame.surfarray.pixels3d(image)[rect.x:rect.x + rect.width,
                                           rect.y:rect.y + rect.height]
    arr[:, :, :] = cv2.GaussianBlur(src=arr, ksize=kernelSize,
                                    sigmaX=r)  # 高斯模糊
 def red_filtering(self, frame):
     # Adapted from
     # https://stackoverflow.com/questions/42840526/opencv-python-red-ball-detection-and-tracking
     #
     # convert the input stream into HSV color space
     hsv_conv_img = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
     # then the image is blurred
     hsv_blurred_img = cv2.medianBlur(hsv_conv_img, 9, 3)
     # because hue wraps up and to extract as many "red objects" as possible,
     # we define lower and upper boundaries for brighter and for darker red shades
     bright_red_mask = cv2.inRange(hsv_blurred_img,
                                   self.bright_red_lower_bounds,
                                   self.bright_red_upper_bounds)
     dark_red_mask = cv2.inRange(hsv_blurred_img,
                                 self.dark_red_lower_bounds,
                                 self.dark_red_upper_bounds)
     # after masking the red shades out, I add the two images
     weighted_mask = cv2.addWeighted(bright_red_mask, 1.0, dark_red_mask,
                                     1.0, 0.0)
     # then the result is blurred
     blurred_mask = cv2.GaussianBlur(weighted_mask, (9, 9), 3, 3)
     # some morphological operations (closing) to remove small blobs
     erode_element = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
     dilate_element = cv2.getStructuringElement(cv2.MORPH_RECT, (8, 8))
     eroded_mask = cv2.erode(blurred_mask, erode_element)
     dilated_mask = cv2.dilate(eroded_mask, dilate_element)
     return dilated_mask
示例#14
0
def image_processing(image):
    PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
    image_path = os.path.dirname(PROJECT_ROOT)+"\media"+"\\"+str(image)

    image = cv2.imread(image_path)

    angle, rotated = correct_skew(image)
    gray = cv2.cvtColor(rotated,cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

    horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (40,1))
    remove_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
    cnts = cv2.findContours(remove_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    for c in cnts:
        cv2.drawContours(rotated, [c], -1, (255,255,255), 5)

    vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,40))
    remove_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
    cnts = cv2.findContours(remove_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    for c in cnts:
        cv2.drawContours(rotated, [c], -1, (255,255,255), 5)

    gray = cv2.cvtColor(rotated,cv2.COLOR_BGR2GRAY)
    filter1 = cv2.medianBlur(gray,5)
    filter2 = cv2.GaussianBlur(filter1,(5,5),0)
    dst = cv2.fastNlMeansDenoising(filter2,None,17,9,17)
    th1 = cv2.adaptiveThreshold(dst,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)

    return th1
示例#15
0
def thresh_binary(img):
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    blur = cv.GaussianBlur(gray, (9, 9), 0)
    (ret3, th3) = cv.threshold(blur, 50, 255, cv.THRESH_OTSU)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))
    opening = cv.morphologyEx(th3, cv.MORPH_OPEN, kernel)
    return opening
def convert(now):
    print()
    img = input('Enter the filename for the output image: ')
    print()
    directory = img

    # To create the folder in sketches of that filename
    # And this folder will contain all images as transition to sketch

    parent_dir = f'.\\ImageToSketch\\sketches'
    path = os.path.join(parent_dir, directory)
    try:
        os.mkdir(path)
    except OSError as error:
        print(error)
    image = cv2.imread(now)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    cv2.imwrite(f"{path}\\{img}(gray).png", gray)
    inverted = 255 - gray
    cv2.imwrite(f"{path}\\{img}(inverted).png", inverted)
    blurred = cv2.GaussianBlur(inverted, (21, 21), 0)
    cv2.imwrite(f"{path}\\{img}(blured).png", blurred)
    inverted_blurred = 255 - blurred
    pencil_sketch = cv2.divide(gray, inverted_blurred, scale=256.0)
    cv2.imwrite(f"{path}\\{img}(final).png", pencil_sketch)
    print()
    print(f'You can find the image here: {path}\\{img}(final).png')
示例#17
0
def find_sudoku(image):
    # preprocessing
    gray_sudoku = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred_sudoku = cv2.GaussianBlur(gray_sudoku, (7, 7), 0)
    thresh = cv2.adaptiveThreshold(blurred_sudoku, 255,
                                   cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                   cv2.THRESH_BINARY_INV, 11, 2)

    # find contours
    contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
                                   cv2.CHAIN_APPROX_SIMPLE)

    # get the sudoku contour
    sudoku_area = 0
    sudoku_contour = contours[0]
    for cnt in contours:
        area = cv2.contourArea(cnt)
        x, y, w, h = cv2.boundingRect(cnt)
        if (0.7 < float(w) / h < 1.3  # aspect ratio
                and area > 150 * 150  # minimal area
                and area > sudoku_area  # biggest area on screen
                and area > .5 * w * h):  # fills bounding rect
            sudoku_area = area
            sudoku_contour = cnt

    perimeter = cv2.arcLength(sudoku_contour, True)
    # define the accuracy here
    epsilon = 0.05 * perimeter
    approx = cv2.approxPolyDP(sudoku_contour, epsilon, True)

    # if it is not a sudoku board, just return the frame without doing anything
    if len(approx) != 4:
        return None

    return sudoku_contour
    def mog(self):
        while (True):
            self.previous_frame = self.current_frame
            # Capture the video frame by frame
            _, self.current_frame = self.video_capture.read()
            # Do not initiate the processing if there's no previous frame so skip the first loop
            if (self.previous_frame is None):
                continue

            # Apply gray conversion and noise reduction (smoothening) for better and faster processing
            current_frame_gray = cv2.cvtColor(self.current_frame,
                                              cv2.COLOR_BGR2GRAY)
            current_frame_gray = cv2.GaussianBlur(
                current_frame_gray, (self.kernel_size, self.kernel_size), 0)
            cv2.imshow('gray', current_frame_gray)

            foreground_mask = self.mog2.apply(current_frame_gray,
                                              fgmask=None,
                                              learningRate=-1)
            # fmask is the output foreground mask as an 8-bit binary image.
            # Next video frame. Floating point frame will be used without scaling and should be in range [0,255].
            # learningRate	The value between 0 and 1 that indicates how fast the background model is learnt.
            # Negative parameter value makes the algorithm to use some automatically chosen learning rate.
            # 0 means that the background model is not updated at all, 1 means that the background model is completely reinitialized from the last frame.
            cv2.imshow('Foreground Mask', foreground_mask)

            white_pixels_count = np.sum(foreground_mask)
            if white_pixels_count < self.mov_detected_pixels_threshold:
                self.mark_as_removed(self.current_frame)
            else:
                self.video_writer.write(self.current_frame)

            cv2.imshow("Live Feed", self.current_frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
示例#19
0
    def _get_balls(self, filename, color, cam_id):
        # print('looking for ' + color)
        image = cv2.imread(filename)

        bounds = COLOR_BOUNDS[color]

        hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        bright = cv2.inRange(hsv, bounds[0], bounds[1])
        blur = cv2.GaussianBlur(bright, (9, 9), 3, 3)

        # cv2.imshow('circled_orig', blur)
        # cv2.waitKey(0)

        detected_circles = cv2.HoughCircles(blur, cv2.HOUGH_GRADIENT,
                                            **HOUGH_PARAMS)
        balls = []

        if detected_circles is not None:
            for circle in detected_circles[0, :]:
                x = circle[0]
                y = circle[1]
                r = circle[2]
                # circled_orig = cv2.circle(image, (x, y), r, (0, 255, 0), thickness = 2)

                # locate detected balls
                distance = self._find_distance(x, y, r, cam_id)
                balls.append(distance)
                # balls.append((x, y, r, color))
                # balls.append()
            # cv2.imshow('circled_orig', circled_orig)
            # cv2.waitKey(0)
        # print('found')
        # print(balls)
        return balls
示例#20
0
def threshholding(diff_image, image_real, image_fake, max_val, thresh):
  blur = cv2.GaussianBlur(diff_image, (5, 5), 0)
  ret3, thresh = cv2.threshold(blur, thresh, max_val, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
  # thresh = cv2.adaptiveThreshold(diff_image, max_val, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
  # thresh = cv2.threshold(diff_image, thresh, max_val, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
  contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  contours = imutils.grab_contours(contours)
  rectangle_diffs = []
  # loop over the contours
  for c in contours:
    # compute the bounding box of the contour and then draw the
    # bounding box on both input images to represent where the two
    # images differ
    tuple_rect = cv2.boundingRect(c)
    rectangle_diffs.append(tuple_rect)
    (x, y, w, h) = tuple_rect
    cv2.rectangle(image_real, (x, y), (x + w, y + h), (0, 0, 255), 2)
    # cv2.rectangle(image_fake, (x, y), (x + w, y + h), (0, 0, 255), 2)

  image_service.show_image(image_real, "Original")
  image_service.show_image(image_fake, "Modified")
  image_service.show_image(diff_image, "Diff")
  image_service.show_image(thresh, "Thresh")

  return image_real, image_fake
def data_augment(xb, yb):
    if np.random.random() < 0.25:
        xb, yb = rotate(xb, yb, 90)

    if np.random.random() < 0.25:
        xb, yb = rotate(xb, yb, 180)

    if np.random.random() < 0.25:
        xb, yb = rotate(xb, yb, 270)

    if np.random.random() < 0.25:
        xb = cv2.flip(xb, 1)  # flipcode > 0:沿y轴翻转

        yb = cv2.flip(yb, 1)

    if np.random.random() < 0.25:
        xb = random_gamma_transform(xb, 1.0)

    if np.random.random() < 0.25:
        xb = blur(xb)

    # 双边过滤
    if np.random.random() < 0.25:
        xb = cv2.bilateralFilter(xb, 9, 75, 75)

    #  高斯滤波
    if np.random.random() < 0.25:
        xb = cv2.GaussianBlur(xb, (5, 5), 1.5)

    if np.random.random() < 0.2:
        xb = add_noise(xb)

    return xb, yb
示例#22
0
def blur_(images, m=2):
    # 設置3x3矩陣每個元素為1/9
    r = 5
    kernel = np.ones((r, r), np.float32) / (r * r)
    out = []

    if m == 1:
        for i in range(len(images)):
            # 將圖片經過3x3矩陣平均模糊化去雜訊
            dst = cv2.filter2D(images[i], -1, kernel)
            out.append(dst)

    elif m == 2:
        for i in range(len(images)):
            # 將圖片經過3x3矩陣平均模糊化去雜訊
            dst = cv2.medianBlur(images[i], r)
            out.append(dst)

    else:
        for i in range(len(images)):
            # 將圖片做高斯模糊化去雜訊
            dst = cv2.GaussianBlur(images[i], (r, r), 1)
            out.append(dst)

    return out
示例#23
0
def getContours(img,cThr=[100,100],showCanny=False,minArea=1000,filter=0,draw =False):
    imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    imgBlur = cv2.GaussianBlur(imgGray,(5,5),1)
    imgCanny = cv2.Canny(imgBlur,cThr[0],cThr[1])
    kernel = np.ones((5,5))
    imgDial = cv2.dilate(imgCanny,kernel,iterations=3)
    imgThre = cv2.erode(imgDial,kernel,iterations=2)
    if showCanny:cv2.imshow('Canny',imgThre)
    contours,hiearchy = cv2.findContours(imgThre,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    finalCountours = []
    for i in contours:
        area = cv2.contourArea(i)
        if area > minArea:
            peri = cv2.arcLength(i,True)
            approx = cv2.approxPolyDP(i,0.02*peri,True)
            bbox = cv2.boundingRect(approx)
            if filter > 0:
                if len(approx) == filter:
                    finalCountours.append([len(approx),area,approx,bbox,i])
            else:
                finalCountours.append([len(approx),area,approx,bbox,i])
    finalCountours = sorted(finalCountours,key = lambda x:x[1] ,reverse= True)
    if draw:
        for con in finalCountours:
            cv2.drawContours(img,con[4],-1,(0,0,255),3)
    return img, finalCountours
示例#24
0
def main():
    img = plt.imread('datas/images/lena.png')

    img_blur = gaussianBlur(img, 7, 3)
    img_blur_with_cv = cv2.GaussianBlur(rgb2gray(img), (7, 7), 0)

    img_blur_color = gaussianBlur_color(img)

    fig = plt.figure()
    a1 = fig.add_subplot(2, 3, 1)
    a1.imshow(rgb2gray(img))
    a1.set_title('gray image')
    a2 = fig.add_subplot(2, 3, 2)
    a2.imshow(img_blur, cmap='gray')
    a2.set_title('Blur')
    a3 = fig.add_subplot(2, 3, 3)
    a3.imshow(img_blur_with_cv, cmap='gray')
    a3.set_title('Blur using OpenCV')
    a4 = fig.add_subplot(2, 3, 4)
    a4.imshow(img)
    a4.set_title('original image')
    a5 = fig.add_subplot(2, 3, 5)
    a5.imshow(img_blur_color, cmap='gray')
    a5.set_title('color Blur')
    # fig.tight_layout()
    plt.show()
示例#25
0
    def _preprocess(self, warped_img):
        '''
        Preprocess the warped and rotated image.

        @warped_img:
            np.array, it should be the output of self._polar_warp_and_rotate().
        @return:
            (s_mask, output_img), saturation mask and image after preprocessing.
        '''
        warped_img = cv.GaussianBlur(warped_img, (3, 3), 1.5)
        hsv = cv.cvtColor(warped_img, cv.COLOR_BGR2HSV)
        warped_img = cv.cvtColor(warped_img, cv.COLOR_BGR2GRAY)
        warped_img = cv.equalizeHist(warped_img)  # Enhance contrast

        _, s, _ = cv.split(hsv)
        _, s = cv.threshold(s, 0, 255, cv.THRESH_OTSU)
        s = cv.morphologyEx(s, cv.MORPH_ERODE, np.ones((5, 5)))
        _, contours, _ = cv.findContours(s, cv.RETR_TREE,
                                         cv.CHAIN_APPROX_SIMPLE)
        contours = sorted(contours, key=lambda ctr: cv.contourArea(ctr)
                          )  # Sort to choose the largest area
        mask = cv.drawContours(np.zeros((warped_img.shape), np.uint8),
                               contours,
                               len(contours) - 1, (255, 255, 255),
                               thickness=1)
        box = cv.boundingRect(get_points(mask))  # Largest area box-bouding
        mask = cv.rectangle(mask, (box[0], box[1]),
                            (box[0] + box[2], box[1] + box[3]),
                            (255, 255, 255),
                            cv.FILLED)  # Fill the area that is to be removed
        mask = cv.bitwise_not(mask)  # Ensure tooth existing area
        return mask, warped_img
def runSimpleFindContours(img):
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    Gaussian = cv2.GaussianBlur(gray, (5, 5), 0)
    canny = cv2.Canny(Gaussian, 100, 250)
    contours, hierarchy = cv2.findContours(canny, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_SIMPLE)
    return (contours, hierarchy)
示例#27
0
def apply_sobel(img, *params):
    ddepth = cv2.CV_16S
    scale = 1
    delta = 0
    kernel_size = 3

    img = cv2.GaussianBlur(img, (5, 5), cv2.BORDER_DEFAULT)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

    grad_x = cv2.Sobel(img,
                       ddepth,
                       1,
                       0,
                       ksize=kernel_size,
                       scale=scale,
                       delta=delta,
                       borderType=cv2.BORDER_DEFAULT)
    grad_y = cv2.Sobel(img,
                       ddepth,
                       0,
                       1,
                       ksize=kernel_size,
                       scale=scale,
                       delta=delta,
                       borderType=cv2.BORDER_DEFAULT)
    abs_grad_x = cv2.convertScaleAbs(grad_x)
    abs_grad_y = cv2.convertScaleAbs(grad_y)

    new_image = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
    #new_image = cv2.Sobel(img, cv2.CV_16S, 1, 0, ksize=3)

    return new_image
def build_threshhold(image):
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray_image, (9, 9), 0)
    _, threshold = cv2.threshold(blurred, 0, 255,
                                 cv2.THRESH_BINARY + cv2.THRESH_OTSU)

    return threshold
示例#29
0
 def filter(self, cvImg):
     """
     see https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_filtering/py_filtering.html
     """
     # return cv2.medianBlur(cvImg, 5)
     # tuppel positiv an odd!
     return cv2.GaussianBlur(cvImg, (3, 3), 0)
示例#30
0
def pre_process(image, threshold_value_1, threshold_value_2):

    #convert the image to gray to reduce computational complexity as
    #only dealing with one colour channel
    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    gray = np.asarray(gray)

    #blur the image, sufficiently enough to remove some of the higher frequency noise, and smooth the edges.
    gray = cv2.GaussianBlur(gray, (5, 5), 0)

    ##changing thresholds doesn't affect area proportions but can affect  to fill shape and identify
    ##edges with lower frequency change
    edges = cv2.Canny(gray, threshold_value_1, threshold_value_2)

    #closing the holes that may appear inside the edges to potentially close leaking
    #in the flood fill stage
    kernel = np.ones((5, 5), np.uint8)
    edges = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)

    #3x3 kernel  and one iteration to only fill potential gaps between exterior edges
    #so that the exterior contour points form a closed polygon and can be filled appropriately. Keep this as minimal as possible to not
    #overly amplify shape differences
    kernel = np.ones((3, 3), np.uint8)
    edges = cv2.dilate(edges, kernel, iterations=1)
    #returns the edge image

    return edges