我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用cv2.CV_16S。
def img_sobel_binary(im, blur_sz): # ?????????????? img_blur = cv2.GaussianBlur(im,blur_sz,0) if len(img_blur.shape) == 3: blur_gray = cv2.cvtColor(img_blur,cv2.COLOR_BGR2GRAY) else: blur_gray = img_blur # ??Sobel???? sobelx = cv2.Sobel(blur_gray,cv2.CV_16S,1,0,ksize=3) abs_sobelx = np.absolute(sobelx) sobel_8u = np.uint8(abs_sobelx) img_show_hook("Sobel??", sobel_8u) # OTSU?????? ret, thd = cv2.threshold(sobel_8u, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU) thd_abs = cv2.convertScaleAbs(thd) bgimg = cv2.addWeighted(thd_abs, 1, 0, 0, 0) img_show_hook("OTSU????", bgimg) return bgimg
def gradient_img(colorsrc): ''' http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/sobel_derivatives/sobel_derivatives.html ''' SCALE = 1 DELTA = 0 DDEPTH = cv2.CV_16S ## to avoid overflow graysrc = cv2.cvtColor(colorsrc, cv2.cv.CV_BGR2GRAY) graysrc = cv2.GaussianBlur(graysrc, (3, 3), 0) ## gradient X ## gradx = cv2.Sobel(graysrc, DDEPTH, 1, 0, ksize=3, scale=SCALE, delta=DELTA) gradx = cv2.convertScaleAbs(gradx) ## gradient Y ## grady = cv2.Sobel(graysrc, DDEPTH, 0, 1, ksize=3, scale=SCALE, delta=DELTA) grady = cv2.convertScaleAbs(grady) grad = cv2.addWeighted(gradx, 0.5, grady, 0.5, 0) return grad
def edgedetect(channel): sobelx = cv2.Sobel(channel, cv2.CV_16S, 1, 0, ksize=3) sobely = cv2.Sobel(channel, cv2.CV_16S, 0, 1, ksize=3) sobel = np.hypot(sobelx, sobely) sobel[sobel > 255] = 255 return sobel
def getRGBS(img, PLOT = False): image = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) # grab the image channels, initialize the tuple of colors, # the figure and the flattened feature vector features = [] featuresSobel = [] Grayscale = cv2.cvtColor(img, cv2.cv.CV_BGR2GRAY) histG = cv2.calcHist([Grayscale], [0], None, [16], [0, 256]) histG = histG / histG.sum() features.extend(histG[:,0].tolist()) grad_x = np.abs(cv2.Sobel(Grayscale, cv2.CV_16S, 1, 0, ksize = 3, scale = 1, delta = 0, borderType = cv2.BORDER_DEFAULT)) grad_y = np.abs(cv2.Sobel(Grayscale, cv2.CV_16S, 0, 1, ksize = 3, scale = 1, delta = 0, borderType = cv2.BORDER_DEFAULT)) abs_grad_x = cv2.convertScaleAbs(grad_x) abs_grad_y = cv2.convertScaleAbs(grad_y) dst = cv2.addWeighted(abs_grad_x,0.5,abs_grad_y,0.5,0) histSobel = cv2.calcHist([dst], [0], None, [16], [0, 256]) histSobel = histSobel / histSobel.sum() features.extend(histSobel[:,0].tolist()) Fnames = [] Fnames.extend(["Color-Gray"+str(i) for i in range(8)]) Fnames.extend(["Color-GraySobel"+str(i) for i in range(8)]) return features, Fnames
def gradient_img(colorsrc): ''' http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/sobel_derivatives/sobel_derivatives.html ''' SCALE = 1 DELTA = 0 DDEPTH = cv2.CV_16S ## to avoid overflow # grayscale image if len(colorsrc.shape)==2: graysrc = cv2.GaussianBlur(colorsrc, (3, 3), 0) ## gradient X ## gradx = cv2.Sobel(graysrc, DDEPTH, 1, 0, ksize=3, scale=SCALE, delta=DELTA) gradx = cv2.convertScaleAbs(gradx) ## gradient Y ## grady = cv2.Sobel(graysrc, DDEPTH, 0, 1, ksize=3, scale=SCALE, delta=DELTA) grady = cv2.convertScaleAbs(grady) grad = cv2.addWeighted(gradx, 0.5, grady, 0.5, 0) return grad # multi-channel image else: gradx_total = np.zeros((colorsrc.shape[0], colorsrc.shape[1])) grady_total = np.zeros((colorsrc.shape[0], colorsrc.shape[1])) for index in range(colorsrc.shape[2]): graysrc=colorsrc[:,:,index] graysrc = cv2.GaussianBlur(graysrc, (3, 3), 0) ## gradient X ## gradx = cv2.Sobel(graysrc, DDEPTH, 1, 0, ksize=3, scale=SCALE, delta=DELTA) gradx = cv2.convertScaleAbs(gradx) gradx_total=gradx_total+gradx ## gradient Y ## grady = cv2.Sobel(graysrc, DDEPTH, 0, 1, ksize=3, scale=SCALE, delta=DELTA) grady = cv2.convertScaleAbs(grady) grady_total = grady_total + grady grad = cv2.addWeighted(gradx_total, 0.5, grady_total, 0.5, 0) return grad