def method3(glass_img, bg_img):
    """

    Args:
        glass_img: (numpy.ndarray)
        bg_img: (numpy.ndarray)

    Returns:
        result_img: (numpy.ndarray)

    """
    result_img = glass_img
    (height, width) = glass_img.shape
    height_ratio = 0.9
    width_ratio = 0.95
    x1 = int(height * (1 - height_ratio) * 0.5)
    x2 = height - x1
    y1 = int(width * (1 - width_ratio) * 0.5)
    y2 = width - y1
    inside_area = bg_img[x1:x2, y1:y2]
    kernel_1 = numpy.ones((1, 1), numpy.uint8)
    kernel_2 = numpy.ones((13, 13), numpy.uint8)
    dilate_bg_img = cv2.dilate(bg_img, kernel_1, iterations=1)
    dilate_inside_area = cv2.dilate(inside_area, kernel_2, iterations=1)
    dilate_bg_img[x1:x2, y1:y2] = dilate_inside_area
    result_img = substract_image(glass_img,
                                 dilate_bg_img,
                                 src_judge_value=0,
                                 dst_judge_value=10)
    return result_img
示例#2
0
 def __improve_image(self, image):
     _, image = cv2.threshold(image, 0, 255,
                              cv2.THRESH_BINARY + cv2.THRESH_OTSU + 2)
     image = cv2.dilate(image, np.ones((3, 2), np.uint8), iterations=1)
     image = cv2.erode(image, np.ones((4, 1), np.uint8), iterations=2)
     image = cv2.dilate(image, np.ones((3, 1), np.uint8), iterations=2)
     image = cv2.erode(image, np.ones((2, 1), np.uint8), iterations=2)
     return image
示例#3
0
def Dilate(imgSrc: np.ndarray, kernel: np.ndarray, count=1) -> np.ndarray:
    if count is 0:
        return imgSrc.copy()

    imgRes1 = imgSrc
    imgRes2 = np.empty(imgRes1.shape, dtype=np.uint8)
    for i in range(count):
        cv.dilate(imgRes1, kernel, imgRes2)
        imgRes1 = imgRes2
    return imgRes1
示例#4
0
def dilate(ary, N, iterations):
    """Dilate using an NxN '+' sign shape. ary is np.uint8."""

    kernel = np.zeros((N, N), dtype=np.uint8)
    kernel[(N - 1) // 2, :] = 1  # Bug solved with // (integer division)

    dilated_image = cv2.dilate(ary / 255, kernel, iterations=iterations)

    kernel = np.zeros((N, N), dtype=np.uint8)
    kernel[:, (N - 1) // 2] = 1  # Bug solved with // (integer division)
    dilated_image = cv2.dilate(dilated_image, kernel, iterations=iterations)
    return dilated_image
示例#5
0
def remove_background(image):
    """
    Removes background from image
    """
    # Paramters.
    BLUR = 21
    CANNY_THRESH_1 = 10
    CANNY_THRESH_2 = 30
    MASK_DILATE_ITER = 10
    MASK_ERODE_ITER = 10
    MASK_COLOR = (0.0, 0.0, 1.0)

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

    # Edge detection.
    edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2)
    edges = cv2.dilate(edges, None)
    edges = cv2.erode(edges, None)

    # Find contours in edges, sort by area
    contour_info = []
    contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)

    for c in contours:
        contour_info.append((
            c,
            cv2.isContourConvex(c),
            cv2.contourArea(c),
        ))
    contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True)
    max_contour = contour_info[0]

    # Create empty mask.
    mask = np.zeros(edges.shape)
    cv2.fillConvexPoly(mask, max_contour[0], (255))

    # Smooth mask and blur it.
    mask = cv2.dilate(mask, None, iterations=MASK_DILATE_ITER)
    mask = cv2.erode(mask, None, iterations=MASK_ERODE_ITER)
    mask = cv2.GaussianBlur(mask, (BLUR, BLUR), 0)
    mask_stack = np.dstack([mask] * 3)

    # Blend masked img into MASK_COLOR background
    mask_stack = mask_stack.astype('float32') / 255.0
    image = image.astype('float32') / 255.0

    masked = (mask_stack * image) + ((1 - mask_stack) * MASK_COLOR)
    masked = (masked * 255).astype('uint8')

    c_red, c_green, c_blue = cv2.split(image)
    img_a = cv2.merge((c_red, c_green, c_blue, mask.astype('float32') / 255.0))

    return img_a * 255
示例#6
0
def do_circles(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    _, gray = cv2.threshold(gray, 125, 255, cv2.THRESH_BINARY)
    gray = 255 - gray
    cv2.imshow('bw', gray)
    cv2.waitKey()

    kernel = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], np.uint8)

    kernel2 = np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1],
                        [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]], np.uint8)
    erosion = cv2.dilate(gray, kernel2, iterations=5)
    cv2.imshow('dilate', erosion)
    cv2.waitKey()

    erosion = cv2.erode(erosion, kernel2, iterations=5)
    cv2.imshow('rode', erosion)
    cv2.waitKey()

    erosion = cv2.dilate(erosion, kernel2, iterations=5)
    cv2.imshow('dilate2', erosion)
    cv2.waitKey()

    erosion = cv2.erode(erosion, kernel2, iterations=5)
    cv2.imshow('rode2', erosion)
    cv2.waitKey()

    #erosion = cv2.dilate(erosion, kernel, iterations=2)
    #cv2.imshow('dilate', erosion)
    #cv2.waitKey()

    canny = cv2.Canny(erosion, 50, 150)
    cv2.imshow('canny', canny)
    cv2.waitKey()

    circles = cv2.HoughCircles(erosion,
                               cv2.HOUGH_GRADIENT,
                               2,
                               50,
                               param1=150,
                               param2=100,
                               minRadius=50)

    for i in circles[0, :]:
        # draw the outer circle
        cv2.circle(erosion, (i[0], i[1]), i[2], (255, 255, 255), 2)
        # draw the center of the circle
        cv2.circle(erosion, (i[0], i[1]), 2, (255, 255, 255), 3)

    cv2.imshow('circles', erosion)
    cv2.waitKey()
 def visualizeWaypoints(self, waypoints, start_idx=None, show_wp_num=False):
     pac_dots = self._img.copy()
     pac_dots[:, :] = 0
     pac_dots[waypoints[:, 0], waypoints[:, 1]] = 1
     num_dilations = 1
     kernel = np.ones((2, 2), np.uint8)
     pac_dots = cv2.dilate(pac_dots, kernel, iterations=num_dilations)
     img = pac_dots + self._safety_img
     img_color = img[..., None] * np.array([1, 1, 1])
     if start_idx is not None:
         try:
             for agent in range(len(start_idx)):
                 color = self._path_colors[(((agent - 1) * -1) + 1) %
                                           self._num_colors]
                 cv2.circle(img_color,
                            (waypoints[start_idx[agent],
                                       1], waypoints[start_idx[agent], 0]),
                            5, color)
         except:
             print("INVALID STARTING LOCATIONS")
     plt.imshow(img_color)
     plt.xticks([])
     plt.yticks([])
     if show_wp_num:
         for ii, wp in enumerate(waypoints):
             plt.text(wp[1], wp[0], str(ii), color='red', fontsize=8)
         # cv2.putText(img_color,str(ii),(wp[1],wp[0]),cv2.FONT_HERSHEY_SIMPLEX,0.25,(255,255,255))
     plt.show()
示例#8
0
 def detect_red(self, img):
     thresh = cv2.inRange(img, (0, 0, 100), (10, 10, 255))
     if (sum(sum(thresh)) == 0):  #If it is obscured
         return None  #Return none
     kernel = np.ones((5, 5), np.uint8)
     result = cv2.dilate(thresh, kernel, iterations=3)
     return self.getCoM(result)  #Positions returned
示例#9
0
def setStandardValues(base64Image, values):

	d = []
	for i in values:
		d.append(int(i['value']))
	(h_min, h_max, s_min, s_max, v_min, v_max, threshold1, threshold2, area_min) = tuple(d)

	img_str = base64.b64decode(base64Image)

	nparr = np.fromstring(img_str, np.uint8)
	image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
	imgHSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
	lower = np.array([h_min, s_min, v_min])
	upper = np.array([h_max, s_max, v_max])
	mask = cv2.inRange(imgHSV, lower, upper)
	result = cv2.bitwise_and(image, image, mask = mask)
	mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
	imgCanny = cv2.Canny(mask, threshold1, threshold2)
	kernel = np.ones((5,5))
	imgDil = cv2.dilate(imgCanny, kernel, iterations=1)
	found, standardHeight = getContour(imgDil, image)

	img_str = cv2.imencode('.png', imgHSV)[1].tobytes()
	base64ImageReturn = base64.b64encode(img_str) 
	
	imgD_str = cv2.imencode('.png', result)[1].tobytes()
	base64ImgDilReturn = base64.b64encode(imgD_str)

	return base64ImageReturn, base64ImgDilReturn
示例#10
0
def detect_ball(frame):
    x, y, radius = -1, -1, -1
    hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv_frame, HSV_lower, HSV_upper)
    mask = cv2.erode(mask, None, iterations=0)
    mask = cv2.dilate(mask, None, iterations=12)
    im2, contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                                                cv2.CHAIN_APPROX_SIMPLE)
    center = (-1, -1)

    # only proceed if at least one contour was found
    if len(contours) > 0:
        # find the largest contour in the mask, then use
        # it to compute the minimum enclosing circle and
        # centroid
        c = max(contours, key=cv2.contourArea)
        ((x, y), radius) = cv2.minEnclosingCircle(c)
        xList.append(x)  # x coordinates
        xPath.append((x - xStart) / pathLength)  # path traveled
        M = cv2.moments(mask)
        center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

        # check that the radius is larger than some threshold
        if radius > minRadius:
            #outline ball
            cv2.circle(frame, (int(x), int(y)), int(radius), (255, 0, 0), 2)
            #show ball center
            cv2.circle(frame, center, 5, (0, 255, 0), -1)

    return center[0], center[1], radius
示例#11
0
    def detect(self, image, tVal=25):
        # compute the absolute difference between the background model
        # and the image passed in, then threshold the delta image
        delta = cv2.absdiff(self.bg.astype("uint8"), image)
        thresh = cv2.threshold(delta, tVal, 255, cv2.THRESH_BINARY)[1]

        # perform a series of erosions and dilations to remove small
        # blobs
        thresh = cv2.erode(thresh, None, iterations=2)
        thresh = cv2.dilate(thresh, None, iterations=2)

        # find contours in the thresholded image and initialize the
        # minimum and maximum bounding box regions for motion
        cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        (minX, minY) = (np.inf, np.inf)
        (maxX, maxY) = (-np.inf, -np.inf)

        # if no contours were found, return None
        if len(cnts) == 0:
            return None

        # otherwise, loop over the contours
        for c in cnts:
            # compute the bounding box of the contour and use it to
            # update the minimum and maximum bounding box regions
            (x, y, w, h) = cv2.boundingRect(c)
            (minX, minY) = (min(minX, x), min(minY, y))
            (maxX, maxY) = (max(maxX, x + w), max(maxY, y + h))

        # otherwise, return a tuple of the thresholded image along
        # with bounding box
        return (thresh, (minX, minY, maxX, maxY))
示例#12
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
示例#13
0
 def tci_gen(self):
     if self.sensor == 'S2A':
         blue_fn = [item for item in self.rhot_fns if 'rhot_492' in item][0]
         green_fn = [item for item in self.rhot_fns
                     if 'rhot_560' in item][0]
         red_fn = [item for item in self.rhot_fns if 'rhot_665' in item][0]
     else:
         blue_fn = [item for item in self.rhot_fns if 'rhot_492' in item][0]
         green_fn = [item for item in self.rhot_fns
                     if 'rhot_559' in item][0]
         red_fn = [item for item in self.rhot_fns if 'rhot_665' in item][0]
     dst_fn = os.path.join(self.res_dir, self.pre_name + 'tci.tif')
     mask = (self.vector_mask != 0) + self.cloud
     # 膨胀
     mask_uint8 = np.copy(mask).astype(np.uint8)
     kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
     dilated = cv2.dilate(mask_uint8, kernel)  # 膨胀图像
     mask = dilated == 1
     utils.rgb_generator(red_fn,
                         green_fn,
                         blue_fn,
                         dst_fn=dst_fn,
                         scale=1,
                         threshold=0.3,
                         mask=self.vector_mask != 0)
示例#14
0
def red_detect(frame):  # オレンジ色を検出し、画像加工を施す。
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    lower = (0, 230, 150)
    upper = (30, 255, 255)
    red = cv2.inRange(hsv, lower, upper)
    kernal = np.ones((5, 5), "uint8")
    red = cv2.dilate(red, kernal)
    res = cv2.bitwise_and(frame, frame, mask=red)
    (ret, contours, hierarchy) = cv2.findContours(
        red, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    x = 0
    y = 0
    w = 0
    h = 0
    for pic, contour in enumerate(contours):
        area = cv2.contourArea(contour)
        if (area > 100):
            x, y, w, h = cv2.boundingRect(contour)
            frame = cv2.rectangle(
                frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
            cv2.putText(frame, "RED color", (x, y),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255))
            cv2.drawMarker(frame, (480, 350), (255, 255, 0),
                           markerType=cv2.MARKER_SQUARE, markerSize=5, thickness=10)
            cv2.drawMarker(frame, ((x + w//2), (y + h//2)), (255, 255, 0),
                           markerType=cv2.MARKER_SQUARE, markerSize=5, thickness=10)
            cv2.arrowedLine(frame, (480, 350),
                            ((x + w//2), (y + h//2)), (255, 0, 0), 5)
            cv2.rectangle(frame, (330, 200), (630, 500), (0, 255, 0), 1)
    return frame, x, y, w, h  # 動画データとピクセル(x,y,z,h)を返す
示例#15
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 extract(image):
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    low = np.array([0, 100, 250])
    high = np.array([179, 255, 255])
    mask_fore = cv2.inRange(hsv, low, high)
    mask_back = cv2.bitwise_not(mask_fore)

    kernel = np.ones((11, 11), np.float32) / 121
    fltr1_f_dil = cv2.dilate(mask_fore, kernel, iterations=1)
    fltr1_f_bor = cv2.bitwise_and(mask_back, mask_back, mask=fltr1_f_dil)

    contours, hierarchy = cv2.findContours(fltr1_f_bor, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_NONE)
    contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0])
    i = 0

    save_path = os.path.join(os.getcwd(), IMG_DES)
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    for cnt in contours:
        x, y, w, h = cv2.boundingRect(cnt)
        if w > 50 and h > 50:
            p = os.path.join(save_path, "{}.png".format(str(i)))
            cv2.imwrite(p, pad_image(fltr1_f_bor[y:y + h, x:x + w]))
            i = i + 1
 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
示例#18
0
文件: game.py 项目: benlgb/EarnMoney
    def get_text(self, img, color=None, lowerb=None, upperb=None, reg=None):
        if color is None:
            color = 'black'

        if lowerb is None:
            lowerb = self.hsv_color[color][0]

        if upperb is None:
            upperb = self.hsv_color[color][1]

        temp = Image.fromarray(img)
        text = pytesseract.image_to_string(temp, config=self.TESSERACT_CONFIG)

        if len(img.shape) == 3 and img.shape[2] == 3:
            dilate_kernel = np.ones((2, 2), np.uint8)
            hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
            bi_img = cv2.inRange(hsv_img, lowerb, upperb)
            bi_img = cv2.dilate(bi_img, dilate_kernel, iterations=1)
            bi_img = Image.fromarray(255 - bi_img)
            text += pytesseract.image_to_string(bi_img, config=self.TESSERACT_CONFIG)

        if reg is None:
            return text
        else:
            match = re.search(reg, text)
            if match is None:
                raise NotFound()
            else:
                return match
def dilate_image(img):
    cv2.imshow('Original image', img)
    kernel = np.ones((2, 2), np.uint8)
    dilation = cv2.dilate(img, kernel, iterations=2)
    cv2.imshow('Dilated image', dilation)

    return dilation
示例#20
0
def dilation(bw, original, ime_firme):
    img = bw
    kernel = np.ones((1, 1), np.uint8)
    dilation = cv2.dilate(img, kernel, iterations=1)

    text = columns(dilation, original, ime_firme)
    return text
示例#21
0
def s2(img):  #segmentacionWarershed
    img = img
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(gray, 0, 255,
                                cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    # Eliminación del ruido
    kernel = np.ones((3, 3), np.uint8)
    opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)
    # Encuentra el área del fondo
    sure_bg = cv2.dilate(opening, kernel, iterations=3)
    # Encuentra el área del primer
    dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5)
    ret, sure_fg = cv2.threshold(dist_transform, 0.7 * dist_transform.max(),
                                 255, 0)
    # Encuentra la región desconocida (bordes)
    sure_fg = np.uint8(sure_fg)
    unknown = cv2.subtract(sure_bg, sure_fg)
    # Etiquetado
    ret, markers = cv2.connectedComponents(sure_fg)
    # Adiciona 1 a todas las etiquetas para asegurra que el fondo sea 1 en lugar de cero
    markers = markers + 1
    # Ahora se marca la región desconocida con ceros
    markers[unknown == 255] = 0
    markers = cv2.watershed(img, markers)
    img[markers == -1] = [255, 0, 0]
    return img
示例#22
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
示例#23
0
def apply_harris_corners(image):
    """run the harris corner detection function and dilate to increase point
       size.

    :param img: the loaded image
    :type img: cv2 image
    :return: copy of the image with the corner detections
    :rtype: cv2 image
    """

    # copy the image
    img = np.copy(image)

    # convert to gray before corner detection
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # get the corners
    corners = cv2.cornerHarris(gray, 2, 3, 0.04)

    # result is dilated for marking the corners, not important
    corners = cv2.dilate(corners, None)

    # put the corners on the image
    img[corners > 0.01 * corners.max()] = [0, 0, 255]

    return img
示例#24
0
    def mini_img(self,img):
        kp = self.orb.detect(img, None)
        kp, _= self.orb.compute(img, kp)
        pointlist=[]
        mat=img.copy()
        for i in range(len(kp)):
            pointlist.append(list(map(int,kp[i].pt)))
            mat = cv.circle(mat, tuple(list(map(int,kp[i].pt))), self.maskradio, (0, 0, 255),-1)
        _,_,R=cv.split(mat)

        pointlist=sorted(pointlist,key=lambda x:x[0])
        try:
            xmin,xmax=pointlist[0][0],pointlist[-1][0]
            pointlist=sorted(pointlist,key=lambda x:x[1])
            ymin,ymax=pointlist[0][1],pointlist[-1][1]
            if xmax-xmin<=5:
                xmin-=2
                xmax+=2
            if abs(ymax-ymin)<=5:
                ymin-=2
                ymax+=2
            R=R[ymin:ymax,xmin:xmax]
            R=cv.dilate(R,(13,13))
            R=cv.erode(R,(19,19))
        except Exception as Err:
            print(Err)
            # cv.imshow('wrong',img)
            # cv.waitKey(0)

            #cv.destroyAllWindows()
        return xmin,xmax,ymin,ymax,R
示例#25
0
def get_rm_shadow(img, roi):
    # convert to hsv
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # Gaussian blur
    hsv = cv2.GaussianBlur(hsv, (5, 5), 0)
    # get saturation
    _, S, _ = cv2.split(hsv)
    # threshold saturation, shadows have higher sat than road
    _, shadow_mask = cv2.threshold(S, 70, 255, cv2.THRESH_BINARY)
    # get rid of noise
    kernel = np.ones((7, 7), np.uint8)
    shadow_mask = cv2.morphologyEx(shadow_mask,
                                   cv2.MORPH_CLOSE,
                                   kernel,
                                   iterations=1)
    # mask roi
    shadow_mask = cv2.bitwise_and(shadow_mask, roi)
    # get shadows from img
    shadows = cv2.bitwise_and(img, img, mask=shadow_mask)
    # make shadows grayscale
    shadows_gray = cv2.cvtColor(shadows, cv2.COLOR_BGR2GRAY)
    # get road markings in shadow
    _, rm_mask = cv2.threshold(shadows_gray, 100, 255, cv2.THRESH_BINARY)
    # clean up noise
    kernel = np.ones((2, 2), np.uint8)
    rm_mask = cv2.morphologyEx(rm_mask, cv2.MORPH_OPEN, kernel, iterations=1)
    # increase mask size a bit
    kernel = np.ones((3, 3), np.uint8)
    rm_mask = cv2.dilate(rm_mask, kernel)

    return rm_mask
示例#26
0
def watershed_demo(image):
    blurred = cv.pyrMeanShiftFiltering(image, 10, 100)
    gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
    cv.imshow("binary", binary)

    kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
    mb = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel, iterations=2)
    sure_bg = cv.dilate(binary, kernel, iterations=3)
    cv.imshow("mor", sure_bg)

    dist = cv.distanceTransform(mb, cv.DIST_L2, 3)
    dist_output = cv.normalize(dist, 0, 1.0, cv.NORM_MINMAX)
    cv.imshow("dist", dist_output * 50)

    ret, surface = cv.threshold(dist, dist.max() * 0.6, 255, cv.THRESH_BINARY)
    cv.imshow("interface", surface)

    surface_fg = np.uint8(surface)
    unknow = cv.subtract(sure_bg, surface_fg)
    ret, markers = cv.connectedComponents(surface_fg)
    print(ret)

    markers += 1
    markers[unknow == 255] = 0
    markers = cv.watershed(image, markers=markers)
    image[markers == -1] = [0, 0, 255]
    cv.imshow("result", image)
def extract_text_lines(img, output_dir):
    """
        img - image from which the text lines are extracted
        output_dir - directory where the extracted lines should be saved 
    """
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    blur = cv2.medianBlur(gray, 5)
    thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                   cv2.THRESH_BINARY_INV, 5, 5)

    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (16, 2))
    dilate = cv2.dilate(thresh, kernel, iterations=2)
    rotated, rot_dilated = find_text_angle(dilate, img)

    cnts = cv2.findContours(rot_dilated, cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)
    if len(cnts) == 2:
        cnts = cnts[0]
    else:
        cnts = cnts[1]

    lines_path = os.path.join(output_dir, 'lines')

    if not os.path.exists(lines_path):
        os.makedirs(lines_path)

    for line_idx, line in enumerate(cnts, start=-len(cnts)):
        x, y, w, h = cv2.boundingRect(line)
        roi = rotated[y:y + h, x:x + w]
        filename = 'line' + str(line_idx) + '.jpg'
        save_img(lines_path, filename=filename, img=roi)
def extract_text_chars(img, output_dir):
    """
        img - image from which the individual chars are extracted
        output_dir - directory where the extracted lines should be saved 
    """
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    blur = cv2.medianBlur(gray, 7)

    thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                   cv2.THRESH_BINARY_INV, 7, 11)
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 7))

    dilate = cv2.dilate(thresh, kernel, iterations=1)

    cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    if len(cnts) == 2:
        cnts = cnts[0]
    else:
        cnts = cnts[1]

    chars_path = os.path.join(output_dir, 'chars')

    if not os.path.exists(chars_path):
        os.makedirs(chars_path)

    for char_idx, character in enumerate(cnts, start=-len(cnts)):
        x, y, w, h = cv2.boundingRect(character)
        roi = img[y:y + h, x:x + w]
        filename = 'char' + str(char_idx) + '.jpg'
        save_img(chars_path, filename=filename, img=roi)
示例#29
0
def _dilate(img: object):

    kernel = np.ones((2, 2), np.uint8)

    erosion = cv2.erode(img, kernel, iterations=1)
    dilate = cv2.dilate(erosion, kernel, iterations=1)

    return dilate
def preProcessing(img):
    imgGray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    imgBlur = cv.GaussianBlur(imgGray,(5,5),1)
    imgCanny = cv.Canny(imgBlur,200,200)
    kernel = np.ones((5,5))
    imgDial = cv.dilate(imgCanny,kernel,iterations=2)
    imgThres = cv.erode(imgDial,kernel,iterations=1)
    return imgThres