本文整理汇总了Python中cv2.GaussianBlur方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.GaussianBlur方法的具体用法?Python cv2.GaussianBlur怎么用?Python cv2.GaussianBlur使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv2
的用法示例。
在下文中一共展示了cv2.GaussianBlur方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: prediction
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GaussianBlur [as 别名]
def prediction(self, image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.GaussianBlur(image, (21, 21), 0)
if self.avg is None:
self.avg = image.copy().astype(float)
cv2.accumulateWeighted(image, self.avg, 0.5)
frameDelta = cv2.absdiff(image, cv2.convertScaleAbs(self.avg))
thresh = cv2.threshold(
frameDelta, DELTA_THRESH, 255,
cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
cnts = cv2.findContours(
thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
self.avg = image.copy().astype(float)
return cnts
示例2: _elastic
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GaussianBlur [as 别名]
def _elastic(image, p, alpha=None, sigma=None, random_state=None):
"""Elastic deformation of images as described in [Simard2003]_ (with modifications).
.. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
Convolutional Neural Networks applied to Visual Document Analysis", in
Proc. of the International Conference on Document Analysis and
Recognition, 2003.
Based on https://gist.github.com/erniejunior/601cdf56d2b424757de5
From:
https://www.kaggle.com/bguberfain/elastic-transform-for-data-augmentation
"""
if random.random() > p:
return image
if alpha == None:
alpha = image.shape[0] * random.uniform(0.5,2)
if sigma == None:
sigma = int(image.shape[0] * random.uniform(0.5,1))
if random_state is None:
random_state = np.random.RandomState(None)
shape = image.shape[:2]
dx, dy = [cv2.GaussianBlur((random_state.rand(*shape) * 2 - 1) * alpha, (sigma|1, sigma|1), 0) for _ in range(2)]
x, y = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]))
x, y = np.clip(x+dx, 0, shape[1]-1).astype(np.float32), np.clip(y+dy, 0, shape[0]-1).astype(np.float32)
return cv2.remap(image, x, y, interpolation=cv2.INTER_LINEAR, borderValue= 0, borderMode=cv2.BORDER_REFLECT)
示例3: find_squares
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GaussianBlur [as 别名]
def find_squares(img):
img = cv2.GaussianBlur(img, (5, 5), 0)
squares = []
for gray in cv2.split(img):
for thrs in xrange(0, 255, 26):
if thrs == 0:
bin = cv2.Canny(gray, 0, 50, apertureSize=5)
bin = cv2.dilate(bin, None)
else:
retval, bin = cv2.threshold(gray, thrs, 255, cv2.THRESH_BINARY)
bin, contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
cnt_len = cv2.arcLength(cnt, True)
cnt = cv2.approxPolyDP(cnt, 0.02*cnt_len, True)
if len(cnt) == 4 and cv2.contourArea(cnt) > 1000 and cv2.isContourConvex(cnt):
cnt = cnt.reshape(-1, 2)
max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in xrange(4)])
if max_cos < 0.1:
squares.append(cnt)
return squares
示例4: __init__
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GaussianBlur [as 别名]
def __init__(self, size_range=(0, 3), sigma_range=(0, 0), symmetric=True, max_size=None):
"""
Args:
size_range (tuple[int]): Gaussian window size would be 2 * size +
1, where size is randomly sampled from this [low, high) range.
sigma_range (tuple[float]): min,max of the sigma value. 0 means
opencv's default.
symmetric (bool): whether to use the same size & sigma for x and y.
max_size (int): deprecated
"""
super(GaussianBlur, self).__init__()
if not isinstance(size_range, (list, tuple)):
size_range = (0, size_range)
assert isinstance(sigma_range, (list, tuple)), sigma_range
if max_size is not None:
log_deprecated("GaussianBlur(max_size=)", "Use size_range= instead!", "2020-09-01")
size_range = (0, max_size)
self._init(locals())
示例5: augment
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GaussianBlur [as 别名]
def augment(self, img, strength):
s = self.output_size[0]
start_x = random.randrange(0, img.shape[0] - s + 1)
start_y = random.randrange(0, img.shape[1] - s + 1)
img = img[start_x:start_x + s, start_y:start_y + s]
### No resizing and rotating....
# img = rotate_and_crop(img, (random.random() - 0.5) * strength * 300)
# img = cv2.resize(img, self.output_size)
if random.random() < 0.5:
# left-right flip
img = img[:, ::-1]
if len(img.shape) < 3:
img = img[:, :, None]
if self.blur:
angle = random.uniform(-1, 1) * 10
# img = cv2.GaussianBlur(img, (3, 3), 0)
img = rotate_and_crop(img, angle)
img = rotate_and_crop(img, -angle)
img = cv2.resize(img, dsize=self.output_size)
return img
示例6: test_motion
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GaussianBlur [as 别名]
def test_motion():
image = cv2.imread("./imgs/image.jpeg")
print(image.shape)
detector = Detector_Motion()
image2 = cv2.imread("./imgs/image_box.jpg")
print(image2.shape)
assert image.shape == image2.shape
image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
image2 = cv2.GaussianBlur(image2, (21, 21), 0)
detector.avg = image2.astype(float)
output = detector.prediction(image)
df = detector.filter_prediction(output, image)
image = detector.draw_boxes(image, df)
print(df)
assert df.shape[0] == 1
cv2.imwrite("./imgs/outputcv.jpg", image)
示例7: correct_color
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GaussianBlur [as 别名]
def correct_color(img1, img2, landmark):
blur_amount = 0.4 * np.linalg.norm(
np.mean(landmark[core.LEFT_EYE_POINTS], axis=0)
- np.mean(landmark[core.RIGHT_EYE_POINTS], axis=0)
)
blur_amount = int(blur_amount)
if blur_amount % 2 == 0:
blur_amount += 1
img1_blur = cv2.GaussianBlur(img1, (blur_amount, blur_amount), 0)
img2_blur = cv2.GaussianBlur(img2, (blur_amount, blur_amount), 0)
img2_blur += (128 * (img2_blur <= 1.0)).astype(img2_blur.dtype)
return img2.astype(np.float64) * img1_blur.astype(np.float64) / img2_blur.astype(np.float64)
示例8: disk
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GaussianBlur [as 别名]
def disk(radius, alias_blur=0.1, dtype=np.float32):
if radius <= 8:
L = np.arange(-8, 8 + 1)
ksize = (3, 3)
else:
L = np.arange(-radius, radius + 1)
ksize = (5, 5)
X, Y = np.meshgrid(L, L)
aliased_disk = np.array((X ** 2 + Y ** 2) <= radius ** 2, dtype=dtype)
aliased_disk /= np.sum(aliased_disk)
# supersample disk to antialias
return cv2.GaussianBlur(aliased_disk, ksize=ksize, sigmaX=alias_blur)
# Tell Python about the C method
示例9: censor
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GaussianBlur [as 别名]
def censor(self, img_path, out_path=None, visualize=True, parts_to_blur=['BELLY', 'BUTTOCKS', 'F_BREAST', 'F_GENITALIA', 'M_GENETALIA', 'M_BREAST']):
if not out_path and not visualize:
print('No out_path passed and visualize is set to false. There is no point in running this function then.')
image = cv2.imread(img_path)
boxes = Detector.detect(self, img_path)
boxes = [i['box'] for i in boxes if i['label'] in parts_to_blur]
for box in boxes:
part = image[box[1]:box[3], box[0]:box[2]]
image = cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (0, 0, 0), cv2.FILLED)
# image = cv2.GaussianBlur(part,(23, 23), 30)
# image[box[1]:box[3], box[0]:box[2]] = part
if visualize:
cv2.imshow("Blurred image", image)
cv2.waitKey(0)
if out_path:
cv2.imwrite(out_path, image)
示例10: __init__
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GaussianBlur [as 别名]
def __init__(self, frame, rect):
x1, y1, x2, y2 = rect
w, h = map(cv2.getOptimalDFTSize, [x2-x1, y2-y1])
x1, y1 = (x1+x2-w)//2, (y1+y2-h)//2
self.pos = x, y = x1+0.5*(w-1), y1+0.5*(h-1)
self.size = w, h
img = cv2.getRectSubPix(frame, (w, h), (x, y))
self.win = cv2.createHanningWindow((w, h), cv2.CV_32F)
g = np.zeros((h, w), np.float32)
g[h//2, w//2] = 1
g = cv2.GaussianBlur(g, (-1, -1), 2.0)
g /= g.max()
self.G = cv2.dft(g, flags=cv2.DFT_COMPLEX_OUTPUT)
self.H1 = np.zeros_like(self.G)
self.H2 = np.zeros_like(self.G)
for i in xrange(128):
a = self.preprocess(rnd_warp(img))
A = cv2.dft(a, flags=cv2.DFT_COMPLEX_OUTPUT)
self.H1 += cv2.mulSpectrums(self.G, A, 0, conjB=True)
self.H2 += cv2.mulSpectrums( A, A, 0, conjB=True)
self.update_kernel()
self.update(frame)
示例11: pre_process_image
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GaussianBlur [as 别名]
def pre_process_image(img, skip_dilate=False):
"""Uses a blurring function, adaptive thresholding and dilation to expose the main features of an image."""
# Gaussian blur with a kernal size (height, width) of 9.
# Note that kernal sizes must be positive and odd and the kernel must be square.
proc = cv2.GaussianBlur(img.copy(), (9, 9), 0)
# Adaptive threshold using 11 nearest neighbour pixels
proc = cv2.adaptiveThreshold(proc, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
# Invert colours, so gridlines have non-zero pixel values.
# Necessary to dilate the image, otherwise will look like erosion instead.
proc = cv2.bitwise_not(proc, proc)
if not skip_dilate:
# Dilate the image to increase the size of the grid lines.
kernel = np.array([[0., 1., 0.], [1., 1., 1.], [0., 1., 0.]],np.uint8)
proc = cv2.dilate(proc, kernel)
return proc
示例12: sobelOperT
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GaussianBlur [as 别名]
def sobelOperT(self, img, blursize, morphW, morphH):
'''
No different with sobelOper ?
'''
blur = cv2.GaussianBlur(img, (blursize, blursize), 0, 0, cv2.BORDER_DEFAULT)
if len(blur.shape) == 3:
gray = cv2.cvtColor(blur, cv2.COLOR_RGB2GRAY)
else:
gray = blur
x = cv2.Sobel(gray, cv2.CV_16S, 1, 0, 3)
absX = cv2.convertScaleAbs(x)
grad = cv2.addWeighted(absX, 1, 0, 0, 0)
_, threshold = cv2.threshold(grad, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)
element = cv2.getStructuringElement(cv2.MORPH_RECT, (morphW, morphH))
threshold = cv2.morphologyEx(threshold, cv2.MORPH_CLOSE, element)
return threshold
示例13: __apply_canny
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GaussianBlur [as 别名]
def __apply_canny(self, src, ksize=7, sigma=1.2, low_th=10, high_th=70):
"""Apply canny edge detection.
Args:
src (int): Input image BGR.
numpy.ndarray, (720, 1280, 3), 0~255
Returns:
dst (int): Output image.
numpy.ndarray, (720, 1280), 0~1
"""
gray = cv2.cvtColor(src, cv2.COLOR_RGB2GRAY)
blur_gray = cv2.GaussianBlur(gray,(ksize, ksize), sigma)
dst = cv2.Canny(blur_gray, low_th, high_th) // 255
return dst
示例14: produce_heatmaps_with_bbox
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GaussianBlur [as 别名]
def produce_heatmaps_with_bbox(image,label,h_out,w_out,num_klass,ksize=9,sigma=0):
heatmap=np.zeros(shape=[h_out,w_out,num_klass])
h,w,_=image.shape
for single_box in label:
if single_box[4]>=0:
####box center (x,y)
center=[(single_box[0]+single_box[2])/2/w,(single_box[1]+single_box[3])/2/h] ###0-1
heatmap[round(center[1]*h_out),round(center[0]*w_out),int(single_box[4]) ]=1.
heatmap = cv2.GaussianBlur(heatmap, (ksize,ksize), sigma)
am = np.amax(heatmap)
if am>0:
heatmap /= am / 255.
heatmap=np.expand_dims(heatmap,-1)
return heatmap
示例15: produce_heatmaps_with_keypoint
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import GaussianBlur [as 别名]
def produce_heatmaps_with_keypoint(image,label,h_out,w_out,num_klass,ksize=7,sigma=0):
heatmap=np.zeros(shape=[h_out,w_out,num_klass])
h,w,_=image.shape
for i in range(label.shape[0]):
single_point=label[i]
if single_point[0]>0 and single_point[1]>0:
heatmap[int(single_point[1]*(h_out-1)),int(single_point[0]*(w_out-1)),i ]=1.
heatmap = cv2.GaussianBlur(heatmap, (ksize,ksize), sigma)
am = np.amax(heatmap)
if am>0:
heatmap /= am / 255.
return heatmap