本文整理匯總了Python中cv2.CV_8U屬性的典型用法代碼示例。如果您正苦於以下問題:Python cv2.CV_8U屬性的具體用法?Python cv2.CV_8U怎麽用?Python cv2.CV_8U使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類cv2
的用法示例。
在下文中一共展示了cv2.CV_8U屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: sharpen
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CV_8U [as 別名]
def sharpen(my_image):
my_image = cv2.cvtColor(my_image, cv2.CV_8U)
height, width, n_channels = my_image.shape
result = np.zeros(my_image.shape, my_image.dtype)
## [basic_method_loop]
for j in range (1, height-1):
for i in range (1, width-1):
for k in range (0, n_channels):
sum = 5 * my_image[j, i, k] - my_image[j + 1, i, k] - my_image[j - 1, i, k]\
- my_image[j, i + 1, k] - my_image[j, i - 1, k];
if sum > 255:
sum = 255
if sum < 0:
sum = 0
result[j, i, k] = sum
## [basic_method_loop]
return result
## [basic_method]
示例2: verticalEdgeDetection
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CV_8U [as 別名]
def verticalEdgeDetection(image):
image_sobel = cv2.Sobel(image.copy(),cv2.CV_8U,1,0)
# image = auto_canny(image_sobel)
# img_sobel, CV_8U, 1, 0, 3, 1, 0, BORDER_DEFAULT
# canny_image = auto_canny(image)
flag,thres = cv2.threshold(image_sobel,0,255,cv2.THRESH_OTSU|cv2.THRESH_BINARY)
print(flag)
flag,thres = cv2.threshold(image_sobel,int(flag*0.7),255,cv2.THRESH_BINARY)
# thres = simpleThres(image_sobel)
kernal = np.ones(shape=(3,15))
thres = cv2.morphologyEx(thres,cv2.MORPH_CLOSE,kernal)
return thres
#確定粗略的左右邊界
示例3: get_blur_im
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CV_8U [as 別名]
def get_blur_im(self):
"""downscale and blur the image"""
# preprocess image
dwnscl_factor = 4; # Hydra images' shape is divisible by 4
blr_sigma = 17; # blur the image a bit, seems to work better
new_shape = (self.img.shape[1]//dwnscl_factor, # as x,y, not row,columns
self.img.shape[0]//dwnscl_factor)
try:
dwn_gray_im = cv2.resize(self.img, new_shape)
except:
pdb.set_trace()
# apply blurring
blur_im = cv2.GaussianBlur(dwn_gray_im, (blr_sigma,blr_sigma),0)
# normalise between 0 and 255
blur_im = cv2.normalize(blur_im, None, alpha=0, beta=255,
norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
return blur_im
示例4: classify_frame
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CV_8U [as 別名]
def classify_frame(net, inputQueue, outputQueue):
# keep looping
while True:
# check to see if there is a frame in our input queue
if not inputQueue.empty():
# grab the frame from the input queue, resize it, and
# construct a blob from it
frame = inputQueue.get()
frame = cv2.resize(frame, (300, 300))
blob = cv2.dnn.blobFromImage(frame, size=(300, 300), ddepth=cv2.CV_8U)
net.setInput(blob)
out = net.forward()
# write the detections to the output queue
outputQueue.put(out)
# initialize the input queue (frames), output queue (out),
# and the list of actual detections returned by the child process
示例5: sketch_image
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CV_8U [as 別名]
def sketch_image(img):
"""Sketches the image applying a laplacian operator to detect the edges"""
# Convert to gray scale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply median filter
img_gray = cv2.medianBlur(img_gray, 5)
# Detect edges using cv2.Laplacian()
edges = cv2.Laplacian(img_gray, cv2.CV_8U, ksize=5)
# Threshold the edges image:
ret, thresholded = cv2.threshold(edges, 70, 255, cv2.THRESH_BINARY_INV)
return thresholded
示例6: worker
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CV_8U [as 別名]
def worker(path, select_folder, waste_img_folder, crop_sz, stride, thres_sz, cont_var_thresh, freq_var_thresh):
img_name = os.path.basename(path)
img = cv2.imread(path, cv2.IMREAD_UNCHANGED)
h, w, c = img.shape
h_space = np.arange(0, h - crop_sz + 1, stride)
if h - (h_space[-1] + crop_sz) > thres_sz:
h_space = np.append(h_space, h - crop_sz)
w_space = np.arange(0, w - crop_sz + 1, stride)
if w - (w_space[-1] + crop_sz) > thres_sz:
w_space = np.append(w_space, w - crop_sz)
index = 0
for x in h_space:
for y in w_space:
index += 1
patch_name = img_name.replace('.png', '_s{:05d}.png'.format(index))
patch = img[x:x + crop_sz, y:y + crop_sz, :]
im_gray = patch[:, :, 1]
[mean, var] = cv2.meanStdDev(im_gray)
freq_var = cv2.Laplacian(im_gray, cv2.CV_8U).var()
if var > cont_var_thresh and freq_var>freq_var_thresh:
cv2.imwrite(os.path.join(select_folder, patch_name), patch)
else:
cv2.imwrite(os.path.join(waste_img_folder, patch_name), patch)
return 'Processing {:s} ...'.format(img_name)
示例7: LaplacianOfGaussian
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CV_8U [as 別名]
def LaplacianOfGaussian(image):
LoG_image = cv2.GaussianBlur(image, (3,3), 0) # paramter
gray = cv2.cvtColor( LoG_image, cv2.COLOR_BGR2GRAY)
LoG_image = cv2.Laplacian( gray, cv2.CV_8U,3,3,2) # parameter
LoG_image = cv2.convertScaleAbs(LoG_image)
return LoG_image
示例8: cartoonize_image
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CV_8U [as 別名]
def cartoonize_image(img, ksize=5, sketch_mode=False):
num_repetitions, sigma_color, sigma_space, ds_factor = 10, 5, 7, 4
# Convert image to grayscale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply median filter to the grayscale image
img_gray = cv2.medianBlur(img_gray, 7)
# Detect edges in the image and threshold it
edges = cv2.Laplacian(img_gray, cv2.CV_8U, ksize=ksize)
ret, mask = cv2.threshold(edges, 100, 255, cv2.THRESH_BINARY_INV)
# 'mask' is the sketch of the image
if sketch_mode:
return cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
# Resize the image to a smaller size for faster computation
img_small = cv2.resize(img, None, fx=1.0/ds_factor, fy=1.0/ds_factor, interpolation=cv2.INTER_AREA)
# Apply bilateral filter the image multiple times
for i in range(num_repetitions):
img_small = cv2.bilateralFilter(img_small, ksize, sigma_color, sigma_space)
img_output = cv2.resize(img_small, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_LINEAR)
dst = np.zeros(img_gray.shape)
# Add the thick boundary lines to the image using 'AND' operator
dst = cv2.bitwise_and(img_output, img_output, mask=mask)
return dst
示例9: __init__
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CV_8U [as 別名]
def __init__(self,
width, height,
intrinsic_matrix,
undistort_rectify=False,
extrinsic_matrix=None,
distortion_coeffs=None,
rectification_matrix=None,
projection_matrix=None):
self.width = width
self.height = height
self.intrinsic_matrix = intrinsic_matrix
self.extrinsic_matrix = extrinsic_matrix
self.distortion_coeffs = distortion_coeffs
self.rectification_matrix = rectification_matrix
self.projection_matrix = projection_matrix
self.undistort_rectify = undistort_rectify
self.fx = intrinsic_matrix[0, 0]
self.fy = intrinsic_matrix[1, 1]
self.cx = intrinsic_matrix[0, 2]
self.cy = intrinsic_matrix[1, 2]
if undistort_rectify:
self.remap = cv2.initUndistortRectifyMap(
cameraMatrix=self.intrinsic_matrix,
distCoeffs=self.distortion_coeffs,
R=self.rectification_matrix,
newCameraMatrix=self.projection_matrix,
size=(width, height),
m1type=cv2.CV_8U)
else:
self.remap = None
示例10: preprocess
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CV_8U [as 別名]
def preprocess(self, input_img):
imgBlurred = cv2.GaussianBlur(input_img, (7, 7), 0) # old window was (5,5)
gray = cv2.cvtColor(imgBlurred, cv2.COLOR_BGR2GRAY) # convert to gray
sobelx = cv2.Sobel(gray, cv2.CV_8U, 1, 0, ksize=3) # sobelX to get the vertical edges
ret2, threshold_img = cv2.threshold(sobelx, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
element = self.element_structure
morph_img_threshold = threshold_img.copy()
cv2.morphologyEx(src=threshold_img, op=cv2.MORPH_CLOSE, kernel=element, dst=morph_img_threshold)
return morph_img_threshold
示例11: text_detect
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CV_8U [as 別名]
def text_detect(img,ele_size=(8,2)): #
if len(img.shape)==3:
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img_sobel = cv2.Sobel(img,cv2.CV_8U,1,0)#same as default,None,3,1,0,cv2.BORDER_DEFAULT)
img_threshold = cv2.threshold(img_sobel,0,255,cv2.THRESH_OTSU+cv2.THRESH_BINARY)
element = cv2.getStructuringElement(cv2.MORPH_RECT,ele_size)
img_threshold = cv2.morphologyEx(img_threshold[1],cv2.MORPH_CLOSE,element)
res = cv2.findContours(img_threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
if cv2.__version__.split(".")[0] == '3':
_, contours, hierarchy = res
else:
contours, hierarchy = res
Rect = [cv2.boundingRect(i) for i in contours if i.shape[0]>100]
RectP = [(int(i[0]-i[2]*0.08),int(i[1]-i[3]*0.08),int(i[0]+i[2]*1.1),int(i[1]+i[3]*1.1)) for i in Rect]
return RectP
示例12: cartoonize_image
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CV_8U [as 別名]
def cartoonize_image(img, ds_factor=4, sketch_mode=False):
#convert to gray scale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#apply median filter
img_gray = cv2.medianBlur(img_gray, 7)
#detect edges and threshold the imag
edges = cv2.Laplacian(img_gray, cv2.CV_8U, ksize=5)
ret, mask = cv2.threshold(edges, 100, 255, cv2.THRESH_BINARY_INV)
#mask is the sketch of the image
if sketch_mode:
return cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
img_small = cv2.resize(img, None, fx=1.0/ds_factor, fy=1.0/ds_factor, interpolation = cv2.INTER_AREA)
num_repetitions = 10
sigma_color = 5
sigma_space = 7
size = 5
#apply bilateral filter multiple times
for i in range(num_repetitions):
img_small = cv2.bilateralFilter(img_small, size, sigma_color, sigma_space)
img_output = cv2.resize(img_small, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_LINEAR)
dst = np.zeros(img_gray.shape)
dst = cv2.bitwise_and(img_output, img_output, mask=mask)
return dst
示例13: merge_channels_into_color_image
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CV_8U [as 別名]
def merge_channels_into_color_image(channels):
"""
Combine a full resolution grayscale reconstruction and four color channels at half resolution
into a color image at full resolution.
:param channels: dictionary containing the four color reconstructions (at quarter resolution),
and the full resolution grayscale reconstruction.
:return a color image at full resolution
"""
with Timer('Merge color channels'):
assert('R' in channels)
assert('G' in channels)
assert('W' in channels)
assert('B' in channels)
assert('grayscale' in channels)
# upsample each channel independently
for channel in ['R', 'G', 'W', 'B']:
channels[channel] = cv2.resize(channels[channel], dsize=None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
# Shift the channels so that they all have the same origin
channels['B'] = shift_image(channels['B'], dx=1, dy=1)
channels['G'] = shift_image(channels['G'], dx=1, dy=0)
channels['W'] = shift_image(channels['W'], dx=0, dy=1)
# reconstruct the color image at half the resolution using the reconstructed channels RGBW
reconstruction_bgr = np.dstack([channels['B'],
cv2.addWeighted(src1=channels['G'], alpha=0.5,
src2=channels['W'], beta=0.5,
gamma=0.0, dtype=cv2.CV_8U),
channels['R']])
reconstruction_grayscale = channels['grayscale']
# combine the full res grayscale resolution with the low res to get a full res color image
upsampled_img = upsample_color_image(reconstruction_grayscale, reconstruction_bgr)
return upsampled_img
return upsampled_img
示例14: detect_fn
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CV_8U [as 別名]
def detect_fn(img, img_name, img_save_path):
resize_img = cv2.resize(img, (int(2.0 * img.shape[1]), int(2.0 * img.shape[0])), interpolation=cv2.INTER_CUBIC)
img = preprocess_img(img, img_name)
# cv2.imwrite(img_save_path + img_name + '_processed.jpg', img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
sobel = cv2.Sobel(gray, cv2.CV_8U, 1, 0, ksize=3)
# 二值化
ret, binary = cv2.threshold(sobel, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)
element1 = cv2.getStructuringElement(cv2.MORPH_RECT, (16, 6))
element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (13, 4)) # 兩個參數可調
# 膨脹一次,讓輪廓突出
dilation = cv2.dilate(binary, element2, iterations=1)
# 腐蝕一次,去掉細節,如表格線等。注意這裏去掉的是豎直的線
erosion = cv2.erode(dilation, element1, iterations=1)
dilation2 = cv2.dilate(erosion, element2, iterations=2)
# cv2.imwrite(img_save_path + img_name + '_dilation.jpg', dilation2)
region = []
# 查找輪廓
contours, hierarchy = cv2.findContours(dilation2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 利用以上函數可以得到多個輪廓區域,存在一個列表中。
# 篩選那些麵積小的
for i in range(len(contours)):
cnt = contours[i]
area = cv2.contourArea(cnt)
if (area < 50):
continue
# 找到最小的矩形,該矩形可能有方向
rect = cv2.minAreaRect(cnt)
# box是四個點的坐標
box = cv2.boxPoints(rect)
box = np.int0(box)
# 計算高和寬
height = abs(box[0][1] - box[2][1])
width = abs(box[0][0] - box[2][0])
# 篩選那些太細的矩形,留下扁的
if 25 < height < 80 and width > 25 and height < width * 1.3:
region.append(box)
max_x = 0
for box in region: # 每個box是左下,左上,右上,右下坐標
for box_p in box:
if box_p[0] > max_x:
max_x = box_p[0]
h, w, c = resize_img.shape
return resize_img[0:h, 0:min(max_x + 50, w)]
開發者ID:Mingtzge,項目名稱:2019-CCF-BDCI-OCR-MCZJ-OCR-IdentificationIDElement,代碼行數:48,代碼來源:fix_img_address_unit.py
示例15: spatter
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CV_8U [as 別名]
def spatter(x, severity=1):
c = [(0.65, 0.3, 4, 0.69, 0.6, 0),
(0.65, 0.3, 3, 0.68, 0.6, 0),
(0.65, 0.3, 2, 0.68, 0.5, 0),
(0.65, 0.3, 1, 0.65, 1.5, 1),
(0.67, 0.4, 1, 0.65, 1.5, 1)][severity - 1]
x = np.array(x, dtype=np.float32) / 255.
liquid_layer = np.random.normal(size=x.shape[:2], loc=c[0], scale=c[1])
liquid_layer = gaussian(liquid_layer, sigma=c[2])
liquid_layer[liquid_layer < c[3]] = 0
if c[5] == 0:
liquid_layer = (liquid_layer * 255).astype(np.uint8)
dist = 255 - cv2.Canny(liquid_layer, 50, 150)
dist = cv2.distanceTransform(dist, cv2.DIST_L2, 5)
_, dist = cv2.threshold(dist, 20, 20, cv2.THRESH_TRUNC)
dist = cv2.blur(dist, (3, 3)).astype(np.uint8)
dist = cv2.equalizeHist(dist)
ker = np.array([[-2, -1, 0], [-1, 1, 1], [0, 1, 2]])
dist = cv2.filter2D(dist, cv2.CV_8U, ker)
dist = cv2.blur(dist, (3, 3)).astype(np.float32)
m = cv2.cvtColor(liquid_layer * dist, cv2.COLOR_GRAY2BGRA)
m /= np.max(m, axis=(0, 1))
m *= c[4]
# water is pale turqouise
color = np.concatenate((175 / 255. * np.ones_like(m[..., :1]),
238 / 255. * np.ones_like(m[..., :1]),
238 / 255. * np.ones_like(m[..., :1])), axis=2)
color = cv2.cvtColor(color, cv2.COLOR_BGR2BGRA)
x = cv2.cvtColor(x, cv2.COLOR_BGR2BGRA)
return cv2.cvtColor(np.clip(x + m * color, 0, 1), cv2.COLOR_BGRA2BGR) * 255
else:
m = np.where(liquid_layer > c[3], 1, 0)
m = gaussian(m.astype(np.float32), sigma=c[4])
m[m < 0.8] = 0
# mud brown
color = np.concatenate((63 / 255. * np.ones_like(x[..., :1]),
42 / 255. * np.ones_like(x[..., :1]),
20 / 255. * np.ones_like(x[..., :1])), axis=2)
color *= m[..., np.newaxis]
x *= (1 - m[..., np.newaxis])
return np.clip(x + color, 0, 1) * 255