我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用cv2.CV_8UC1。
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)
def to_binary_mask(mask, t=0.00001): mask = inverse_preprocessing(mask) ### Threshold the RGB image - This step increase sensitivity mask[mask > t] = 255 mask[mask <= t] = 0 ### To grayscale and normalize mask_gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY) mask_gray = cv2.normalize(src=mask_gray, dst=None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1) ### Auto binary threshold (thresh, mask_binary) = cv2.threshold(mask_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) return mask_binary
def process_output(self, disparity): cv8uc = cv2.normalize(disparity, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1) if self.args.preview: cv2.imshow("disparity", cv8uc) cv2.waitKey(0) cv2.imwrite(os.path.join(self.args.folder, self.args.output), cv8uc)
def render(self,frame): canvas = cv2.imread("pen.jpg", cv2.CV_8UC1) #convert frame to gray scale. img_gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #perform binary threshold. With different values of threshold, we get different mozaic patterns ret,img_thr=cv2.threshold(img_gray,70,255,cv2.THRESH_BINARY) #apply gaussian blur img_blur = cv2.GaussianBlur(img_thr, (3, 3), 0) #invert image img_invert= 255-img_blur img_blur=cv2.GaussianBlur(img_invert, ksize=(15, 15),sigmaX=0, sigmaY=0) #generate final mozaic effect final =255-cv2.divide(255-img_thr, 255-img_blur, scale=256) #render image over a canvas return cv2.multiply(final, canvas, scale=1./256)
def find_contours(img): ''' :param img: (numpy array) :return: all possible rectangles (contours) ''' img_blurred = cv2.GaussianBlur(img, (5, 5), 1) # remove noise img_gray = cv2.cvtColor(img_blurred, cv2.COLOR_BGR2GRAY) # greyscale image # cv2.imshow('', img_gray) # cv2.waitKey(0) # Apply Sobel filter to find the vertical edges # Find vertical lines. Car plates have high density of vertical lines img_sobel_x = cv2.Sobel(img_gray, cv2.CV_8UC1, dx=1, dy=0, ksize=3, scale=1, delta=0, borderType=cv2.BORDER_DEFAULT) # cv2.imshow('img_sobel', img_sobel_x) # Apply optimal threshold by using Oslu algorithm retval, img_threshold = cv2.threshold(img_sobel_x, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY) # cv2.imshow('s', img_threshold) # cv2.waitKey(0) # TODO: Try to apply AdaptiveThresh # Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on. # gaus_threshold = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 115, 1) # cv2.imshow('or', img) # cv2.imshow('gaus', gaus_threshold) # cv2.waitKey(0) # Define a stuctural element as rectangular of size 17x3 (we'll use it during the morphological cleaning) element = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(17, 3)) # And use this structural element in a close morphological operation morph_img_threshold = deepcopy(img_threshold) cv2.morphologyEx(src=img_threshold, op=cv2.MORPH_CLOSE, kernel=element, dst=morph_img_threshold) # cv2.dilate(img_threshold, kernel=np.ones((1,1), np.uint8), dst=img_threshold, iterations=1) # cv2.imshow('Normal Threshold', img_threshold) # cv2.imshow('Morphological Threshold based on rect. mask', morph_img_threshold) # cv2.waitKey(0) # Find contours that contain possible plates (in hierarchical relationship) contours, hierarchy = cv2.findContours(morph_img_threshold, mode=cv2.RETR_EXTERNAL, # retrieve the external contours method=cv2.CHAIN_APPROX_NONE) # all pixels of each contour plot_intermediate_steps = False if plot_intermediate_steps: plot(plt, 321, img, "Original image") plot(plt, 322, img_blurred, "Blurred image") plot(plt, 323, img_gray, "Grayscale image", cmap='gray') plot(plt, 324, img_sobel_x, "Sobel") plot(plt, 325, img_threshold, "Threshold image") # plot(plt, 326, morph_img_threshold, "After Morphological filter") plt.tight_layout() plt.show() return contours