Python cv2 模块,COLOR_RGB2GRAY 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cv2.COLOR_RGB2GRAY

项目:speed    作者:keon    | 项目源码 | 文件源码
def optical_flow(one, two):
    """
    method taken from (https://chatbotslife.com/autonomous-vehicle-speed-estimation-from-dashboard-cam-ca96c24120e4)
    """
    one_g = cv2.cvtColor(one, cv2.COLOR_RGB2GRAY)
    two_g = cv2.cvtColor(two, cv2.COLOR_RGB2GRAY)
    hsv = np.zeros((120, 320, 3))
    # set saturation
    hsv[:,:,1] = cv2.cvtColor(two, cv2.COLOR_RGB2HSV)[:,:,1]
    # obtain dense optical flow paramters
    flow = cv2.calcOpticalFlowFarneback(one_g, two_g, flow=None,
                                        pyr_scale=0.5, levels=1, winsize=15,
                                        iterations=2,
                                        poly_n=5, poly_sigma=1.1, flags=0)
    # convert from cartesian to polar
    mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
    # hue corresponds to direction
    hsv[:,:,0] = ang * (180/ np.pi / 2)
    # value corresponds to magnitude
    hsv[:,:,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
    # convert HSV to int32's
    hsv = np.asarray(hsv, dtype= np.float32)
    rgb_flow = cv2.cvtColor(hsv,cv2.COLOR_HSV2RGB)
    return rgb_flow
项目:Face-Recognition    作者:irmowan    | 项目源码 | 文件源码
def img_process(im, landmark, print_img=False):
    """
    Image processing, rotate, resize, and crop the face image.

    Args:
        im: numpy array, Original image
        landmark: 5 landmark points

    Return:
        Crop face region
    """
    if landmark is None:
        im_rez = cv2.resize(im, (cfg.crop_size, cfg.crop_size))
        return im_rez
    im_rot, ang, r_landmark = im_rotate(im, landmark)
    im_rez, resize_scale, rez_landmark = im_resize(im_rot, r_landmark, ang)
    crop = im_crop(im_rez, rez_landmark, resize_scale)
    if cfg.forcegray == True:
        crop = cv2.cvtColor(crop, cv2.COLOR_RGB2GRAY)
    # print('Shapes' + str(im_rot.shape) + str(im_rez.shape) + str(crop.shape))
    # return im_rot, im_rez, crop, (crop.astype(np.float) - cfg.PIXEL_MEANS) / cfg.scale
    if print_img:
        return im, im_rot, im_rez, crop
    return crop
项目:SelfDrivingCar    作者:aguijarro    | 项目源码 | 文件源码
def dir_threshold(img, sobel_kernel=3, thresh=(0, np.pi/2)):

    # Apply the following steps to img
    # 1) Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # 2) Take the gradient in x and y separately
    sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
    sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
    # 3) Take the absolute value of the x and y gradients
    abs_sobelx = np.absolute(sobelx)
    abs_sobely = np.absolute(sobely)
    # 4) Use np.arctan2(abs_sobely, abs_sobelx) to calculate the direction of the gradient
    absgraddir = np.arctan2(abs_sobely, abs_sobelx)
    # 5) Create a binary mask where direction thresholds are met
    binary_output = np.zeros_like(absgraddir)
    binary_output[(absgraddir >= thresh[0]) & (absgraddir <= thresh[1])] = 1
    # 6) Return this mask as your binary_output image
    return binary_output


# Define a function that applies Sobel x and y,
# then computes the magnitude of the gradient
# and applies a threshold
项目:SelfDrivingCar    作者:aguijarro    | 项目源码 | 文件源码
def mag_thresh(img, sobel_kernel=3, mag_thresh=(0, 255)):
    # Apply the following steps to img
    # 1) Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # 2) Take the gradient in x and y separately
    sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
    sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
    # 3) Calculate the magnitude
    gradmag = np.sqrt(sobelx**2 + sobely**2)
    # 4) Scale to 8-bit (0 - 255) and convert to type = np.uint8
    scale_factor = np.max(gradmag)/255
    gradmag = (gradmag/scale_factor).astype(np.uint8)
    # 5) Create a binary mask where mag thresholds are met
    binary_output = np.zeros_like(gradmag)
    binary_output[(gradmag >= mag_thresh[0]) & (gradmag <= mag_thresh[1])] = 1
    # 6) Return this mask as your binary_output image
    return binary_output


# Define a function that applies Sobel x or y,
# then takes an absolute value and applies a threshold.
# Note: calling your function with orient='x', thresh_min=5, thresh_max=100
# should produce output like the example image shown above this quiz.
项目:SelfDrivingCar    作者:aguijarro    | 项目源码 | 文件源码
def abs_sobel_thresh(img, orient='x', thresh_min=0, thresh_max=255):

    # Apply the following steps to img
    # 1) Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # 2) Take the derivative in x or y given orient = 'x' or 'y'
    if orient == 'x':
        sobel = cv2.Sobel(gray, cv2.CV_64F, 1, 0)
    if orient == 'y':
        sobel = cv2.Sobel(gray, cv2.CV_64F, 0, 1)
    # 3) Take the absolute value of the derivative or gradient
    abs_sobel = np.absolute(sobel)
    # 4) Scale to 8-bit (0 - 255) then convert to type = np.uint8
    scaled_sobel = np.uint8(255*abs_sobel/np.max(abs_sobel))
    # 5) Create a mask of 1's where the scaled gradient magnitude
            # is > thresh_min and < thresh_max
    binary_output = np.zeros_like(scaled_sobel)
    binary_output[(scaled_sobel >= thresh_min) & (scaled_sobel <= thresh_max)] = 1
    # 6) Return this mask as your binary_output image
    return binary_output
项目:ConditionalGAN    作者:seungjooli    | 项目源码 | 文件源码
def detect_edges(images):
        def blur(image):
            return cv2.GaussianBlur(image, (5, 5), 0)

        def canny_otsu(image):
            scale_factor = 255
            scaled_image = np.uint8(image * scale_factor)

            otsu_threshold = cv2.threshold(
                cv2.cvtColor(scaled_image, cv2.COLOR_RGB2GRAY), 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[0]
            lower_threshold = max(0, int(otsu_threshold * 0.5))
            upper_threshold = min(255, int(otsu_threshold))
            edges = cv2.Canny(scaled_image, lower_threshold, upper_threshold)
            edges = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)

            return np.float32(edges) * (1 / scale_factor)

        blurred = [blur(image) for image in images]
        canny_applied = [canny_otsu(image) for image in blurred]

        return canny_applied
项目:cvloop    作者:shoeffner    | 项目源码 | 文件源码
def find_faces(self, image, draw_box=False):
        """Uses a haarcascade to detect faces inside an image.

        Args:
            image: The image.
            draw_box: If True, the image will be marked with a rectangle.

        Return:
            The faces as returned by OpenCV's detectMultiScale method for
            cascades.
        """
        frame_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
        faces = self.cascade.detectMultiScale(
            frame_gray,
            scaleFactor=1.3,
            minNeighbors=5,
            minSize=(50, 50),
            flags=0)

        if draw_box:
            for x, y, w, h in faces:
                cv2.rectangle(image, (x, y),
                              (x + w, y + h), (0, 255, 0), 2)
        return faces
项目:cervix-roi-segmentation-by-unet    作者:scottykwok    | 项目源码 | 文件源码
def cropCircle(img, resize=None):
    if resize:
        if (img.shape[0] > img.shape[1]):
            tile_size = (int(img.shape[1] * resize / img.shape[0]), resize)
        else:
            tile_size = (resize, int(img.shape[0] * resize / img.shape[1]))
        img = cv2.resize(img, dsize=tile_size, interpolation=cv2.INTER_CUBIC)
    else:
        tile_size = img.shape

    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY);
    _, thresh = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)

    _, contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

    main_contour = sorted(contours, key=cv2.contourArea, reverse=True)[0]

    ff = np.zeros((gray.shape[0], gray.shape[1]), 'uint8')
    cv2.drawContours(ff, main_contour, -1, 1, 15)
    ff_mask = np.zeros((gray.shape[0] + 2, gray.shape[1] + 2), 'uint8')
    cv2.floodFill(ff, ff_mask, (int(gray.shape[1] / 2), int(gray.shape[0] / 2)), 1)

    rect = maxRect(ff)
    rectangle = [min(rect[0], rect[2]), max(rect[0], rect[2]), min(rect[1], rect[3]), max(rect[1], rect[3])]
    img_crop = img[rectangle[0]:rectangle[1], rectangle[2]:rectangle[3]]
    cv2.rectangle(ff, (min(rect[1], rect[3]), min(rect[0], rect[2])), (max(rect[1], rect[3]), max(rect[0], rect[2])), 3,
                  2)

    return [img_crop, rectangle, tile_size]
项目:BlurDetection    作者:whdcumt    | 项目源码 | 文件源码
def evaluate(img_col, args):
    numpy.seterr(all='ignore')
    assert isinstance(img_col, numpy.ndarray), 'img_col must be a numpy array'
    assert img_col.ndim == 3, 'img_col must be a color image ({0} dimensions currently)'.format(img_col.ndim)
    assert isinstance(args, argparse.Namespace), 'args must be of type argparse.Namespace not {0}'.format(type(args))
    img_gry = cv2.cvtColor(img_col, cv2.COLOR_RGB2GRAY)
    rows, cols = img_gry.shape
    crow, ccol = rows/2, cols/2
    f = numpy.fft.fft2(img_gry)
    fshift = numpy.fft.fftshift(f)
    fshift[crow-75:crow+75, ccol-75:ccol+75] = 0
    f_ishift = numpy.fft.ifftshift(fshift)
    img_fft = numpy.fft.ifft2(f_ishift)
    img_fft = 20*numpy.log(numpy.abs(img_fft))
    if args.display and not args.testing:
        cv2.destroyAllWindows()
        scripts.display('img_fft', img_fft)
        scripts.display('img_col', img_col)
        cv2.waitKey(0)
    result = numpy.mean(img_fft)
    return img_fft, result, result < args.thresh
项目:trpo    作者:jjkke88    | 项目源码 | 文件源码
def deal_image(self , image):
        # index = len(self.obs_origin)
        # image_end = []
        # if index < pms.history_number:
        #     image_end = self.obs_origin[0:index]
        #     for i in range(pms.history_number - index):
        #         image_end.append(image)
        # else:
        #     image_end = self.obs_origin[index - pms.history_number:index]
        #
        # image_end = np.concatenate(image_end)
        # # image_end = image_end.reshape((pms.obs_height, pms.obs_width, pms.history_number))
        # obs = cv2.resize(cv2.cvtColor(image_end , cv2.COLOR_RGB2GRAY) / 255. , (pms.obs_height , pms.obs_width))
        obs = cv2.resize(image, (pms.obs_height, pms.obs_width))
        # obs = np.transpose(np.array(obs), (2, 0, 1))
        return obs
项目:reinforceflow    作者:dbobrenko    | 项目源码 | 文件源码
def image_preprocess(obs, resize_width, resize_height, to_gray):
    """Applies basic preprocessing for image observations.

    Args:
        obs (numpy.ndarray): 2-D or 3-D uint8 type image.
        resize_width (int): Resize width. To disable resize, pass None.
        resize_height (int): Resize height. To disable resize, pass None.
        to_gray (bool): Converts image to grayscale.

    Returns (numpy.ndarray):
        Processed 3-D float type image.
    """
    processed_obs = np.squeeze(obs)
    if to_gray:
        processed_obs = cv2.cvtColor(processed_obs, cv2.COLOR_RGB2GRAY)
    if resize_height and resize_width:
        processed_obs = cv2.resize(processed_obs, (resize_height, resize_width))
    if np.ndim(processed_obs) == 2:
        processed_obs = np.expand_dims(processed_obs, 2)
    return processed_obs
项目:deepatari    作者:cowhi    | 项目源码 | 文件源码
def step(self, action):
        """ Perform an action and observe the resulting state.

        Args:
            action (int): Selected action ID to perform in envirnoment.

        Returns:
            reward (float): The change of score after performing the action.

        """
        _logger.debug("Getting index of action %s" % (str(action)))
        action = np.where(self.avail_actions == action)[0][0]
        self.current_frame, reward, self.terminal_state, info = self.gym.step(action)
        self.current_frame = cv2.resize(cv2.cvtColor(self.current_frame, cv2.COLOR_RGB2GRAY), self.frame_dims)
        if self.counts_lives:
            self.just_lost_live = self.has_just_lost_live()
        return reward
项目:tensorlight    作者:bsautermeister    | 项目源码 | 文件源码
def to_grayscale(image):
    """Converts a colored image to a grayscaled one.
    Parameters
    ----------
    image: ndarray()
        An image with the shape of [height, width, 3].
    Returns
    ---------
    image: ndarray(uint8)
        Returns a grayscaled image with shape [height, width, 1].
    """
    img_channels = np.shape(image)[2]
    if img_channels == 3:
        image = cast(image)
        image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
        image = np.expand_dims(image, axis=2)
    # introduce a 1-channel dimension to handle the indexing
    # of color and gray images the same way
    return image
项目:self-driving-truck    作者:aleju    | 项目源码 | 文件源码
def embed_state(self, previous_states, state, volatile=False, requires_grad=True, gpu=-1):
        prev_scrs = [self.downscale_prev(s.screenshot_rs) for s in previous_states]
        prev_scrs_y = [cv2.cvtColor(scr, cv2.COLOR_RGB2GRAY) for scr in prev_scrs]

        #inputs = np.dstack([self.downscale(state.screenshot_rs)] + list(reversed(prev_scrs_y)))
        inputs = np.array(self.downscale(state.screenshot_rs), dtype=np.float32)
        inputs = inputs / 255.0
        inputs = inputs.transpose((2, 0, 1))
        inputs = inputs[np.newaxis, ...]
        inputs = to_cuda(to_variable(inputs, volatile=volatile, requires_grad=requires_grad), gpu)

        inputs_prev = np.dstack(prev_scrs_y)
        inputs_prev = inputs_prev.astype(np.float32) / 255.0
        inputs_prev = inputs_prev.transpose((2, 0, 1))
        inputs_prev = inputs_prev[np.newaxis, ...]
        inputs_prev = to_cuda(to_variable(inputs_prev, volatile=volatile, requires_grad=requires_grad), gpu)

        return self.embed(inputs, inputs_prev)
项目:self-driving-truck    作者:aleju    | 项目源码 | 文件源码
def embed_state(self, previous_states, state, volatile=False, requires_grad=True, gpu=-1):
        prev_scrs = [self.downscale_prev(s.screenshot_rs) for s in previous_states]
        prev_scrs_y = [cv2.cvtColor(scr, cv2.COLOR_RGB2GRAY) for scr in prev_scrs]

        #inputs = np.dstack([self.downscale(state.screenshot_rs)] + list(reversed(prev_scrs_y)))
        inputs = np.array(self.downscale(state.screenshot_rs), dtype=np.float32)
        inputs = inputs / 255.0
        inputs = inputs.transpose((2, 0, 1))
        inputs = inputs[np.newaxis, ...]
        inputs = to_cuda(to_variable(inputs, volatile=volatile, requires_grad=requires_grad), gpu)

        inputs_prev = np.dstack(prev_scrs_y)
        inputs_prev = inputs_prev.astype(np.float32) / 255.0
        inputs_prev = inputs_prev.transpose((2, 0, 1))
        inputs_prev = inputs_prev[np.newaxis, ...]
        inputs_prev = to_cuda(to_variable(inputs_prev, volatile=volatile, requires_grad=requires_grad), gpu)

        return self.embed(inputs, inputs_prev)
项目:lane-detection-raspberry-pi    作者:uvbakutan    | 项目源码 | 文件源码
def binary_extraction(self,image, ksize=3):
        # undistort first
        #image = self.undistort(image)

        color_bin = self.color_thresh(image,thresh=(90, 150))              # initial values 110, 255

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

        sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize)
        sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize)

        gradx = self.abs_sobel_thresh(sobelx, thresh=(100, 190))             # initial values 40, 160
        grady = self.abs_sobel_thresh(sobely, thresh=(100, 190))             # initial values 40, 160
        mag_binary = self.mag_thresh(sobelx, sobely, mag_thresh=(100, 190))  # initial values 40, 160
        #dir_binary = self.dir_threshold(sobelx, sobely, thresh=(0.7, 1.3))

        combined = np.zeros_like(gradx)
        #combined[(((gradx == 1) & (grady == 1)) | ((mag_binary == 1) & (dir_binary == 1))) | (color_bin==1) ] = 1
        combined[(((gradx == 1) & (grady == 1)) | (mag_binary == 1)) | (color_bin==1) ] = 1
        #combined[(((gradx == 1) & (grady == 1)) | (mag_binary == 1)) ] = 1

        return combined

    # transform perspective
项目:Farmbot_GeneralAP    作者:SpongeYao    | 项目源码 | 文件源码
def get_contour(self, arg_frame, arg_export_index, arg_export_path, arg_export_filename, arg_binaryMethod):
        # Otsu's thresholding after Gaussian filtering
        tmp = cv2.cvtColor(arg_frame, cv2.COLOR_RGB2GRAY)
        blur = cv2.GaussianBlur(tmp,(5,5),0)
        if arg_binaryMethod== 0:
            ret, thresholdedImg= cv2.threshold(blur.copy() , self.threshold_graylevel, 255 , 0)
        elif arg_binaryMethod == 1:
            ret,thresholdedImg = cv2.threshold(blur.copy(),0 ,255 ,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
        elif arg_binaryMethod== 2:
            thresholdedImg = cv2.adaptiveThreshold(blur.copy(),255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,5,0)

        result = cv2.cvtColor(thresholdedImg, cv2.COLOR_GRAY2RGB)
        ctrs, hier = cv2.findContours(thresholdedImg, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        ctrs = filter(lambda x : cv2.contourArea(x) > self.threshold_size , ctrs)

        rects = [[cv2.boundingRect(ctr) , ctr] for ctr in ctrs]

        for rect , cntr in rects:
            cv2.drawContours(result, [cntr], 0, (0, 128, 255), 3)
        if arg_export_index:
            cv2.imwrite(arg_export_path+ arg_export_filename+'.jpg', result)
        print "Get Contour success"
        return result
项目:Brewereader    作者:ceafdc    | 项目源码 | 文件源码
def find_lines(img, acc_threshold=0.25, should_erode=True):
    if len(img.shape) == 3 and img.shape[2] == 3:  # if it's color
        img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    img = cv2.GaussianBlur(img, (11, 11), 0)
    img = cv2.adaptiveThreshold(
            img,
            255,
            cv2.ADAPTIVE_THRESH_MEAN_C,
            cv2.THRESH_BINARY,
            5,
            2)

    img = cv2.bitwise_not(img)

    # thresh = 127
    # edges = cv2.threshold(img, thresh, 255, cv2.THRESH_BINARY)[1]
    # edges = cv2.Canny(blur, 500, 500, apertureSize=3)

    if should_erode:
        element = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4))
        img = cv2.erode(img, element)

    theta = np.pi/2000
    angle_threshold = 2
    horizontal = cv2.HoughLines(
            img,
            1,
            theta,
            int(acc_threshold * img.shape[1]),
            min_theta=np.radians(90 - angle_threshold),
            max_theta=np.radians(90 + angle_threshold))
    vertical = cv2.HoughLines(
            img,
            1,
            theta,
            int(acc_threshold * img.shape[0]),
            min_theta=np.radians(-angle_threshold),
            max_theta=np.radians(angle_threshold),
            )

    horizontal = list(horizontal) if horizontal is not None else []
    vertical = list(vertical) if vertical is not None else []

    horizontal = [line[0] for line in horizontal]
    vertical = [line[0] for line in vertical]

    horizontal = np.asarray(horizontal)
    vertical = np.asarray(vertical)

    return horizontal, vertical
项目:SDcarsLaneDetection    作者:Nazanin1369    | 项目源码 | 文件源码
def abs_sobel_thresh(img, orient='x', thresh_min=0, thresh_max=255):
    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Apply x or y gradient with the OpenCV Sobel() function
    # and take the absolute value
    if orient == 'x':
        abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 1, 0))
    if orient == 'y':
        abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 0, 1))
    # Rescale back to 8 bit integer
    scaled_sobel = np.uint8(255*abs_sobel/np.max(abs_sobel))
    # Create a copy and apply the threshold
    binary_output = np.zeros_like(scaled_sobel)
    # Here I'm using inclusive (>=, <=) thresholds, but exclusive is ok too
    binary_output[(scaled_sobel >= thresh_min) & (scaled_sobel <= thresh_max)] = 1

    # Return the result
    return binary_output
项目:face-to-emotion    作者:mhw32    | 项目源码 | 文件源码
def classify_image(image):
    """Call ResXception to return emotion probabilities
    from Keras model.

    @param image: RGB cropped face image
    @return: map from emotion to probability
    """
    nn_input_shape = nn.input_shape[1:3]
    gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    gray_image = cv2.resize(gray_image, (nn_input_shape))
    gray_image = clean_image(gray_image)
    gray_image = np.expand_dims(gray_image, 0)
    gray_image = np.expand_dims(gray_image, -1)

    with graph.as_default():
        emotion_proba = nn.predict(gray_image)
    emotion_map = index_to_emotion(emotion_proba[0].tolist())
    return emotion_map
项目:intel-cervical-cancer    作者:wangg12    | 项目源码 | 文件源码
def cropCircle(img):
    '''
    there many imaged taken thresholded, which means many images is
    present as a circle with black surrounded. This function is to
    find the largest inscribed rectangle to the thresholed image and
    then crop the image to the rectangle.

    input: img - the cv2 module

    return: img_crop, rectangle, tile_size
    '''
    if(img.shape[0] > img.shape[1]):
        tile_size = (int(img.shape[1]*256/img.shape[0]),256)
    else:
        tile_size = (256, int(img.shape[0]*256/img.shape[1]))

    img = cv2.resize(img, dsize=tile_size)

    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY);
    _, thresh = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)

    _, contours, _ = cv2.findContours(thresh.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

    main_contour = sorted(contours, key = cv2.contourArea, reverse = True)[0]

    ff = np.zeros((gray.shape[0],gray.shape[1]), 'uint8')
    cv2.drawContours(ff, main_contour, -1, 1, 15)
    ff_mask = np.zeros((gray.shape[0]+2,gray.shape[1]+2), 'uint8')
    cv2.floodFill(ff, ff_mask, (int(gray.shape[1]/2), int(gray.shape[0]/2)), 1)

    rect = maxRect(ff)
    rectangle = [min(rect[0],rect[2]), max(rect[0],rect[2]), min(rect[1],rect[3]), max(rect[1],rect[3])]
    img_crop = img[rectangle[0]:rectangle[1], rectangle[2]:rectangle[3]]
    cv2.rectangle(ff,(min(rect[1],rect[3]),min(rect[0],rect[2])),(max(rect[1],rect[3]),max(rect[0],rect[2])),3,2)

    return [img_crop, rectangle, tile_size]
项目:intel-cervical-cancer    作者:wangg12    | 项目源码 | 文件源码
def cropCircle(img):
    '''
    there many imaged taken thresholded, which means many images is
    present as a circle with black surrounded. This function is to
    find the largest inscribed rectangle to the thresholed image and
    then crop the image to the rectangle.

    input: img - the cv2 module

    return: img_crop, rectangle, tile_size
    '''
    if(img.shape[0] > img.shape[1]):
        tile_size = (int(img.shape[1]*256/img.shape[0]),256)
    else:
        tile_size = (256, int(img.shape[0]*256/img.shape[1]))

    img = cv2.resize(img, dsize=tile_size)

    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY);
    _, thresh = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)

    _, contours, _ = cv2.findContours(thresh.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

    main_contour = sorted(contours, key = cv2.contourArea, reverse = True)[0]

    ff = np.zeros((gray.shape[0],gray.shape[1]), 'uint8')
    cv2.drawContours(ff, main_contour, -1, 1, 15)
    ff_mask = np.zeros((gray.shape[0]+2,gray.shape[1]+2), 'uint8')
    cv2.floodFill(ff, ff_mask, (int(gray.shape[1]/2), int(gray.shape[0]/2)), 1)

    rect = maxRect(ff)
    rectangle = [min(rect[0],rect[2]), max(rect[0],rect[2]), min(rect[1],rect[3]), max(rect[1],rect[3])]
    img_crop = img[rectangle[0]:rectangle[1], rectangle[2]:rectangle[3]]
    cv2.rectangle(ff,(min(rect[1],rect[3]),min(rect[0],rect[2])),(max(rect[1],rect[3]),max(rect[0],rect[2])),3,2)

    return [img_crop, rectangle, tile_size]
项目:Robo-Plot    作者:JackBuck    | 项目源码 | 文件源码
def process_image(image):
    """

    Args:
        image: The image to process

    Returns:
        sub_image: The rotated and extracted.

    """

    # Convert image to black and white - we cannot take the photos in black and white as we
    # must first search for the red triangle.

    if len(image.shape) == 3:
        processed_img = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    else:
        processed_img = image

    if config.real_hardware:
        num_iterations = 8
    else:
        num_iterations = 8

        processed_img = cv2.GaussianBlur(processed_img, (21, 21), 0)
    _, processed_img = cv2.threshold(processed_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

    # Put a border around the image to stop the edges of the images creating artifacts.
    padded_image = np.zeros((processed_img.shape[0] + 10, processed_img.shape[1] + 10), np.uint8)
    padded_image[5:processed_img.shape[0]+5, 5:processed_img.shape[1]+5] = processed_img

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

    padded_image = cv2.erode(padded_image, kernel, iterations=num_iterations)
    processed_img = padded_image[25:padded_image.shape[0] - 25, 25:padded_image.shape[1] - 25]

    #cv2.imshow('Padded Image', padded_image)
    #cv2.imshow('Processed image', processed_img)
    #cv2.waitKey(0)



    # Debugging code - useful to show the images are being eroded correctly.
    #spacer = processed_img[:, 0:2].copy()
    #spacer.fill(100)
    #combined_image = np.concatenate((processed_img, spacer), axis=1)
    #combined_image = np.concatenate((combined_image, image), axis=1)
    #cv2.imshow('PreProcessed and Processed Image', combined_image)
    #cv2.waitKey(0)

    # Save sub_image to debug folder if required.
    if __debug__:
        iadebug.save_processed_image(processed_img)

    return processed_img
项目:PicFilter    作者:dhuadaar    | 项目源码 | 文件源码
def render(self,frame):
        numDownSamples = 2
        img_rgb = frame
        # number of downscaling steps
        numBilateralFilters = 7
        # number of bilateral filtering steps
        # -- STEP 1 --
        # downsample image using Gaussian pyramid
        img_color = img_rgb
        for _ in xrange(numDownSamples):
            img_color = cv2.pyrDown(img_color)
        # repeatedly apply small bilateral filter instead of applying
        # one large filter
        for _ in xrange(numBilateralFilters):
            img_color = cv2.bilateralFilter(img_color, 9, 9, 7)

        # upsample image to original size
        for _ in xrange(numDownSamples):
            img_color = cv2.pyrUp(img_color)
        # convert to grayscale and apply median blur
        img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
        img_blur = cv2.medianBlur(img_gray, 7)

        # detect and enhance edges
        img_edge = cv2.adaptiveThreshold(img_blur, 255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 9, 2)
        # -- STEP 5 --
        # convert back to color so that it can be bit-ANDed with color image
        img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)
        final = cv2.bitwise_and(img_color, img_edge)
        return cv2.medianBlur(final,7)
项目:PicFilter    作者:dhuadaar    | 项目源码 | 文件源码
def render(self,frame):
        canvas = cv2.imread("pen.jpg", cv2.CV_8UC1)
        numDownSamples = 2
        img_rgb = frame
        # number of downscaling steps
        numBilateralFilters = 3
        # number of bilateral filtering steps
        # -- STEP 1 --
        # downsample image using Gaussian pyramid
        img_color = img_rgb
        for _ in xrange(numDownSamples):
            img_color = cv2.pyrDown(img_color)
        # repeatedly apply small bilateral filter instead of applying
        # one large filter
        for _ in xrange(numBilateralFilters):
            img_color = cv2.bilateralFilter(img_color, 9, 9, 3)

        # upsample image to original size
        for _ in xrange(numDownSamples):
            img_color = cv2.pyrUp(img_color)
        # convert to grayscale and apply median blur
        img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
        img_blur = cv2.medianBlur(img_gray, 3)

        # detect and enhance edges
        img_edge = cv2.adaptiveThreshold(img_blur, 255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 9, 2)
        return  cv2.multiply(cv2.medianBlur(img_edge,7), canvas, scale=1./256)
项目:spiking-ratslam    作者:bjkomer    | 项目源码 | 文件源码
def run(self):
        while not rospy.is_shutdown():
            if self.im_data:
                im = self.im_data.popleft()
                self.im_im.set_data(im)
                self.im_fig.canvas.draw()

                # Get grayscale image for converting to DVS
                gray_im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
                diff_im = gray_im.astype(np.int16) - self.prev_im
                diff_im[np.where((diff_im < self.tol) & (diff_im > -self.tol))] = 0
                diff_im[np.where(diff_im >= self.tol)] = 1
                diff_im[np.where(diff_im <= -self.tol)] = -1
                self.prev_im = np.copy(gray_im)
                self.dv_im.set_data(diff_im)
                self.dv_fig.canvas.draw()
项目:distributional_perspective_on_RL    作者:Kiwoo    | 项目源码 | 文件源码
def process(frame):
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
        frame = cv2.resize(frame, (84, 84), interpolation=cv2.INTER_AREA)
        return frame.reshape(84, 84, 1)
项目:shenlan    作者:vector-1127    | 项目源码 | 文件源码
def split_input(img):
    """
    img: an 512x256x3 image
    :return: [input, output]
    """
    input, output = img[:,:img_cols,:], img[:,img_cols:,:]

    if args.mode == 'BtoA':
        input, output = output, input
    if IN_CH == 1:
        input = cv2.cvtColor(input, cv2.COLOR_RGB2GRAY)
    if OUT_CH == 1:
        output = cv2.cvtColor(output, cv2.COLOR_RGB2GRAY)
    return [input, output]
项目:speed    作者:keon    | 项目源码 | 文件源码
def optical_flow(one, two):
    """
    method taken from https://chatbotslife.com/autonomous-vehicle-speed-estimation-from-dashboard-cam-ca96c24120e4
    input: image_current, image_next (RGB images)
    calculates optical flow magnitude and angle and places it into HSV image
    """
    one_g = cv2.cvtColor(one, cv2.COLOR_RGB2GRAY)
    two_g = cv2.cvtColor(two, cv2.COLOR_RGB2GRAY)
    hsv = np.zeros((120, 320, 3))
    # set saturation
    hsv[:,:,1] = cv2.cvtColor(two, cv2.COLOR_RGB2HSV)[:,:,1]
    # obtain dense optical flow paramters
    flow = cv2.calcOpticalFlowFarneback(one_g, two_g, flow=None,
                                        pyr_scale=0.5,
                                        levels=1,
                                        winsize=10,
                                        iterations=2,
                                        poly_n=5,
                                        poly_sigma=1.1,
                                        flags=0)
    # convert from cartesian to polar
    mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
    # hue corresponds to direction
    hsv[:,:,0] = ang * (180/ np.pi / 2)
    # value corresponds to magnitude
    hsv[:,:,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
    # convert HSV to int32's
    hsv = np.asarray(hsv, dtype= np.float32)
    rgb_flow = cv2.cvtColor(hsv,cv2.COLOR_HSV2RGB)
    return rgb_flow
项目:Speedy-TSLSR    作者:talhaHavadar    | 项目源码 | 文件源码
def recognizeDigit(digit, method = REC_METHOD_TEMPLATE_MATCHING, threshold= 55):
    """
        Finds the best match for the given digit(RGB or gray color scheme). And returns the result and percentage as an integer.
        @threshold percentage of similarity
    """
    __readDigitTemplates()
    digit = digit.copy()
    if digit.shape[2] == 3:
        digit = cv2.cvtColor(digit, cv2.COLOR_RGB2GRAY)
    ret, digit = cv2.threshold(digit, 90, 255, cv2.THRESH_BINARY_INV)
    bestDigit = -1
    if method == REC_METHOD_TEMPLATE_MATCHING:
        bestMatch = None
        for i in range(len(__DIGIT_TEMPLATES)):
            template = __DIGIT_TEMPLATES[i].copy()

            if digit.shape[1] < template.shape[1]:
                template = cv2.resize(template, (digit.shape[1], digit.shape[0]))
            else:
                digit = cv2.resize(digit, (template.shape[1], template.shape[0]))

            result = cv2.matchTemplate(digit, template, cv2.TM_CCORR_NORMED)#cv2.TM_CCOEFF_NORMED)
            (_, max_val, _, max_loc) = cv2.minMaxLoc(result)
            if bestMatch is None or max_val > bestMatch:
                bestMatch = max_val
                bestDigit = i
                print("New Best Match:", bestMatch, bestDigit)

    if (bestMatch * 100) >= threshold:
        return (bestDigit, bestMatch * 100)

    return (-1, 0)
项目:combine-DT-with-NN-in-RL    作者:Burning-Bear    | 项目源码 | 文件源码
def process(frame):
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
        frame = cv2.resize(frame, (84, 84), interpolation=cv2.INTER_AREA)
        return frame.reshape(84, 84, 1)
项目:combine-DT-with-NN-in-RL    作者:Burning-Bear    | 项目源码 | 文件源码
def process(frame):
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
        frame = cv2.resize(frame, (84, 84), interpolation=cv2.INTER_AREA)
        return frame.reshape(84, 84, 1)
项目:reconstruction    作者:microelly2    | 项目源码 | 文件源码
def animpingpong(self):
        obj=self.Object
        img=None
        if not obj.imageFromNode:
            img = cv2.imread(obj.imageFile)
        else:
            img = obj.imageNode.ViewObject.Proxy.img.copy()

        print (obj.blockSize,obj.ksize,obj.k)
        try:
            gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
            gray = np.float32(gray)
            print "normale"
        except:
            im2=cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
            gray = cv2.cvtColor(im2,cv2.COLOR_RGB2GRAY)
            print "except"

        dst = cv2.cornerHarris(gray,obj.blockSize,obj.ksize*2+1,obj.k/10000)
        dst = cv2.dilate(dst,None)

        img[dst>0.01*dst.max()]=[0,0,255]

        dst2=img.copy()
        dst2[dst<0.01*dst.max()]=[255,255,255]
        dst2[dst>0.01*dst.max()]=[0,0,255]

        if not obj.matplotlib:
            cv2.imshow(obj.Label,img)
        else:
            from matplotlib import pyplot as plt
            plt.subplot(121),plt.imshow(img,cmap = 'gray')
            plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
            plt.subplot(122),plt.imshow(dst2,cmap = 'gray')
            plt.title('Corner Image'), plt.xticks([]), plt.yticks([])
            plt.show()

        self.img=img
项目:tensorflow-action-conditional-video-prediction    作者:williamd4112    | 项目源码 | 文件源码
def _transform_frame_color_space_np(x):
    return cv2.cvtColor(x, cv2.COLOR_RGB2GRAY)[:, :, np.newaxis]
项目:tensorflow-action-conditional-video-prediction    作者:williamd4112    | 项目源码 | 文件源码
def _transform_state_color_space_np(s):
    # s: [h, w, c*num_frame]
    num_splits = int(s.shape[-1] / 3)    
    return np.concatenate([cv2.cvtColor(x, cv2.COLOR_RGB2GRAY)[:,:,np.newaxis] for x in np.split(s, num_splits, axis=2)], axis=2)
项目:rl-attack-detection    作者:yenchenlin    | 项目源码 | 文件源码
def _transform_frame_color_space_np(x):
    return cv2.cvtColor(x, cv2.COLOR_RGB2GRAY)[:, :, np.newaxis]
项目:rl-attack-detection    作者:yenchenlin    | 项目源码 | 文件源码
def _transform_state_color_space_np(s):
    # s: [h, w, c*num_frame]
    num_splits = int(s.shape[-1] / 3)
    return np.concatenate([cv2.cvtColor(x, cv2.COLOR_RGB2GRAY)[:,:,np.newaxis] for x in np.split(s, num_splits, axis=2)], axis=2)
项目:rl-attack-detection    作者:yenchenlin    | 项目源码 | 文件源码
def process(frame):
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
        frame = cv2.resize(frame, (84, 84), interpolation=cv2.INTER_AREA)
        return frame.reshape(84, 84, 1)
项目:baselines    作者:openai    | 项目源码 | 文件源码
def _observation(self, frame):
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
        frame = cv2.resize(frame, (self.width, self.height), interpolation=cv2.INTER_AREA)
        return frame[:, :, None]
项目:identidade_ocr    作者:Ronald-TR    | 项目源码 | 文件源码
def binarize(img):  #definindo binarização dos cortes
    a = np.asarray(img)
    a = cv2.cvtColor(a, cv2.COLOR_RGB2GRAY)
    ret, imbin = cv2.threshold(a, 127, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY)
    return Image.fromarray(imbin)
项目:pyVSR    作者:georgesterpu    | 项目源码 | 文件源码
def _get_rois_opencv(self, file, mode='gray'):
        cap = cv2.VideoCapture(file)
        vidframes = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        rois = self._read_roi_file(file)
        totalframes = rois.shape[0]

        if totalframes != vidframes:
            print('Roi Frames: %d\n' % totalframes)
            print('Vid Frames: %d\n' % vidframes)
            raise Exception('Mismatch between the actual number of video frames and the provided ROI _labels')

        if mode == 'gray':
            roi_seq = np.zeros((totalframes, self._yres, self._xres), dtype=np.float32)
        elif mode == 'rgb':
            roi_seq = np.zeros((totalframes, self._yres, self._xres, 3), dtype=np.float32)
        else:
            raise Exception('gray or rgb')

        this_frame = 0
        while cap.isOpened():
            ret, frame = cap.read()
            if ret is False:
                break

            if mode == 'gray':
                gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
                gray_roi = _crop_roi(gray, rois[this_frame, :])
                resized = self._resize_frame(gray_roi)

            elif mode == 'rgb':
                rgb_roi = _crop_roi(frame, rois[this_frame, :])
                resized = self._resize_frame(rgb_roi)
            else:
                raise Exception('gray or rgb')

            roi_seq[this_frame, :, :] = resized

            this_frame += 1

        return roi_seq
项目:malmo-challenge    作者:Kaixhin    | 项目源码 | 文件源码
def rgb2gray(img):
    """
    Convert an RGB image to grayscale
    :param img: image to convert
    :return:
    """
    if OPENCV_AVAILABLE:
        from cv2 import cvtColor, COLOR_RGB2GRAY
        return cvtColor(img, COLOR_RGB2GRAY)
    elif PILLOW_AVAILABLE:
        from PIL import Image
        return np.array(Image.fromarray(img).convert('L'))
项目:hourglasstensorlfow    作者:wbenbihi    | 项目源码 | 文件源码
def test(self, toWait = 0.2):
        """ TESTING METHOD
        You can run it to see if the preprocessing is well done.
        Wait few seconds for loading, then diaporama appears with image and highlighted joints
        /!\ Use Esc to quit
        Args:
            toWait : In sec, time between pictures
        """
        self._create_train_table()
        self._create_sets()
        for i in range(len(self.train_set)):
            img = self.open_img(self.train_set[i])
            w = self.data_dict[self.train_set[i]]['weights']
            padd, box = self._crop_data(img.shape[0], img.shape[1], self.data_dict[self.train_set[i]]['box'], self.data_dict[self.train_set[i]]['joints'], boxp= 0.0)
            new_j = self._relative_joints(box,padd, self.data_dict[self.train_set[i]]['joints'], to_size=256)
            rhm = self._generate_hm(256, 256, new_j,256, w)
            rimg = self._crop_img(img, padd, box)
            # See Error in self._generator
            #rimg = cv2.resize(rimg, (256,256))
            rimg = scm.imresize(rimg, (256,256))
            #rhm = np.zeros((256,256,16))
            #for i in range(16):
            #   rhm[:,:,i] = cv2.resize(rHM[:,:,i], (256,256))
            grimg = cv2.cvtColor(rimg, cv2.COLOR_RGB2GRAY)
            cv2.imshow('image', grimg / 255 + np.sum(rhm,axis = 2))
            # Wait
            time.sleep(toWait)
            if cv2.waitKey(1) == 27:
                print('Ended')
                cv2.destroyAllWindows()
                break



    # ------------------------------- PCK METHODS-------------------------------
项目:dist-dqn    作者:viswanathgs    | 项目源码 | 文件源码
def resize_image(image, width, height):
  """
  Resize the image screen to the configured width and height and
  convert it to grayscale.
  """
  grayscale = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
  return cv2.resize(grayscale, (width, height))
项目:malmo-challenge    作者:Microsoft    | 项目源码 | 文件源码
def rgb2gray(img):
    """
    Convert an RGB image to grayscale
    :param img: image to convert
    :return:
    """
    if OPENCV_AVAILABLE:
        from cv2 import cvtColor, COLOR_RGB2GRAY
        return cvtColor(img, COLOR_RGB2GRAY)
    elif PILLOW_AVAILABLE:
        from PIL import Image
        return np.array(Image.fromarray(img).convert('L'))
项目:Comicolorization    作者:DwangoMediaVillage    | 项目源码 | 文件源码
def get_example(self, i):
        # type: (any) -> typing.Tuple[str, Image]
        path, image = super().get_example(i)
        image_array = numpy.asarray(image)
        image_height, image_width = image_array.shape[:2]
        if len(image_array.shape) == 2:  # gray image
            gray = image_array
        else:
            gray = cv2.cvtColor(image_array, cv2.COLOR_RGB2GRAY)
        facerects = self.classifier.detectMultiScale(
            gray,
            scaleFactor=1.1,
            minNeighbors=5,
            minSize=(64, 64))
        if len(facerects) == 0:
            return path, None  # more sophisticated way to handle errors?
        x, y, width, _ = facerects[0]
        margin = int(width * self.margin_ratio)
        if min(
                y, image_height - y - width,
                x, image_width - x - width,
        ) < margin:  # cannot crop
            return path, None

        cropped = image_array[y - margin:y + width + margin, x - margin:x + width + margin]
        if self.output_resize is None:
            return path, Image.fromarray(cropped)
        else:
            return path, Image.fromarray(cropped).resize(self.output_resize)
项目:party-pi    作者:JustinShenk    | 项目源码 | 文件源码
def take_photo(self):
        """ Take photo and prepare to write, then send to PyImgur (optional).

        """
        faces = detect_faces(face_detection, self.photo)
        gray_image = cv2.cvtColor(self.photo, cv2.COLOR_RGB2GRAY)
        self.draw_hats(self.photo, faces)
        player_data = self.predict_emotions(faces, gray_image)
        self.rank_players(player_data)
        self.save_photo()
项目:saliency    作者:shuuchen    | 项目源码 | 文件源码
def intensity(image):
    """
        Converts a color image into grayscale.
        Used as `channel' argument to function `features'
    """
    return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
项目:DeepLearning    作者:Wanwannodao    | 项目源码 | 文件源码
def _preprocess(self, obs):
        #gray = cv2.cvtColor(obs, cv2.COLOR_RGB2GRAY)
        # for breakout
        #gray = cv2.cvtColor(obs, cv2.COLOR_RGB2GRAY)[34:210]
        gray = cv2.cvtColor(obs, cv2.COLOR_RGB2GRAY)
        resized_gray = cv2.resize(gray, (84, 84))
        """
        cv2.namedWindow("window")
        cv2.imshow("window", gray)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        """
        del obs, gray

        return (resized_gray - 127.5) / 127.5
项目:DeepLearning    作者:Wanwannodao    | 项目源码 | 文件源码
def _preprocess(self, obs):
        gray = cv2.cvtColor(obs, cv2.COLOR_RGB2GRAY)
        # for breakout
        #gray = cv2.cvtColor(obs, cv2.COLOR_RGB2GRAY)[34:210]
        gray = cv2.resize(gray, (84, 84))
        """
        cv2.namedWindow("window")
        cv2.imshow("window", gray)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        """
        return (gray - 127.5) / 127.5