本文整理汇总了Python中cv2.cartToPolar方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.cartToPolar方法的具体用法?Python cv2.cartToPolar怎么用?Python cv2.cartToPolar使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv2
的用法示例。
在下文中一共展示了cv2.cartToPolar方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: preprocess_hog
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cartToPolar [as 别名]
def preprocess_hog(digits):
samples = []
for img in digits:
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bin_n = 16
bin = np.int32(bin_n*ang/(2*np.pi))
bin_cells = bin[:10,:10], bin[10:,:10], bin[:10,10:], bin[10:,10:]
mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists)
# transform to Hellinger kernel
eps = 1e-7
hist /= hist.sum() + eps
hist = np.sqrt(hist)
hist /= norm(hist) + eps
samples.append(hist)
return np.float32(samples)
#不能保证包括所有省份
示例2: preprocess_hog
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cartToPolar [as 别名]
def preprocess_hog(digits):
samples = []
for img in digits:
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bin_n = 16
bin = np.int32(bin_n*ang/(2*np.pi))
bin_cells = bin[:10,:10], bin[10:,:10], bin[:10,10:], bin[10:,10:]
mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists)
# transform to Hellinger kernel
eps = 1e-7
hist /= hist.sum() + eps
hist = np.sqrt(hist)
hist /= norm(hist) + eps
samples.append(hist)
return np.float32(samples)
示例3: proc_oflow
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cartToPolar [as 别名]
def proc_oflow(images):
h, w = images.shape[-3:-1]
processed_images = []
for image in images:
hsv = np.zeros((h, w, 3), dtype=np.uint8)
hsv[:, :, 0] = 255
hsv[:, :, 1] = 255
mag, ang = cv2.cartToPolar(image[..., 0], image[..., 1])
hsv[..., 0] = ang*180/np.pi/2
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
processed_image = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
processed_images.append(processed_image)
return np.stack(processed_images)
示例4: preprocess_hog
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cartToPolar [as 别名]
def preprocess_hog(digits):
samples = []
for img in digits:
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bin_n = 16
bin = np.int32(bin_n*ang/(2*np.pi))
bin_cells = bin[:10,:10], bin[10:,:10], bin[:10,10:], bin[10:,10:]
mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists)
# transform to Hellinger kernel
eps = 1e-7
hist /= hist.sum() + eps
hist = np.sqrt(hist)
hist /= norm(hist) + eps
samples.append(hist)
return np.float32(samples)
示例5: compute_dense_optical_flow
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cartToPolar [as 别名]
def compute_dense_optical_flow(prev_image, current_image):
old_shape = current_image.shape
prev_image_gray = cv2.cvtColor(prev_image, cv2.COLOR_BGR2GRAY)
current_image_gray = cv2.cvtColor(current_image, cv2.COLOR_BGR2GRAY)
assert current_image.shape == old_shape
hsv = np.zeros_like(prev_image)
hsv[..., 1] = 255
flow = None
flow = cv2.calcOpticalFlowFarneback(prev=prev_image_gray,
next=current_image_gray, flow=flow,
pyr_scale=0.8, levels=15, winsize=5,
iterations=10, poly_n=5, poly_sigma=0,
flags=10)
mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
hsv[..., 0] = ang * 180 / np.pi / 2
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
示例6: _make_block_hofm
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cartToPolar [as 别名]
def _make_block_hofm(flow, blocks, mag_buckets, ang_buckets, skutil):
mag, ang = cartToPolar(flow[..., 0], flow[..., 1], angleInDegrees=True)
mag_digit = digitize(mag, mag_buckets)
# mod so 360 falls into first bucket
ang_digit = digitize(ang % 360, ang_buckets)
mag_blocks = skutil.view_as_blocks(mag_digit, (blocks, blocks))
ang_blocks = skutil.view_as_blocks(ang_digit, (blocks, blocks))
histogram = zeros(
(blocks, blocks, len(mag_buckets), len(ang_buckets) - 1)
)
for i in range(blocks):
for j in range(blocks):
for mblock, ablock in zip(
mag_blocks[:, :, i, j].flatten(),
ang_blocks[:, :, i, j].flatten(),
):
histogram[i, j, mblock - 1, ablock - 1] += 1
# normalize by block size (h,w)
histogram[i, j, :, :] /= mag_blocks[:, :, i, j].size
return histogram
示例7: get_mag_ang
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cartToPolar [as 别名]
def get_mag_ang(img):
"""
Gets image gradient (magnitude) and orientation (angle)
Args:
img
Returns:
Gradient, orientation
"""
img = np.sqrt(img)
gx = cv2.Sobel(np.float32(img), cv2.CV_32F, 1, 0)
gy = cv2.Sobel(np.float32(img), cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
return mag, ang, gx, gy
示例8: flow2colorimage
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cartToPolar [as 别名]
def flow2colorimage(ar_f_Flow:np.array(float)) -> np.array(int):
""" translate 1 optical flow (with values from -1.0 to 1.0) to an colorful image
"""
h, w, c = ar_f_Flow.shape
if not isinstance(ar_f_Flow[0,0,0], np.float32):
warnings.warn("Need to convert flows to float32")
ar_f_Flow = ar_f_Flow.astype(np.float32)
ar_n_hsv = np.zeros((h, w, 3), dtype = np.uint8)
ar_n_hsv[...,1] = 255
# get colors
mag, ang = cv2.cartToPolar(ar_f_Flow[..., 0], ar_f_Flow[..., 1])
ar_n_hsv[...,0] = ang * 180 / np.pi / 2
ar_n_hsv[...,2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
ar_n_bgr = cv2.cvtColor(ar_n_hsv, cv2.COLOR_HSV2BGR)
return ar_n_bgr
示例9: flow_to_rgb
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cartToPolar [as 别名]
def flow_to_rgb(flow):
"""
Visualizes optical flow in hsv space and converts it to rgb space.
:param flow: (np.array (h, w, c)) optical flow
:return: (np.array (h, w, c)) rgb data
"""
im1 = flow[:, :, 0]
im2 = flow[:, :, 1]
h, w = flow.shape[:2]
# Use Hue, Saturation, Value colour model
hsv = np.zeros((h, w, 3), dtype=np.float32)
hsv[..., 1] = 1
mag, ang = cv2.cartToPolar(im1, im2)
hsv[..., 0] = ang * 180 / np.pi
hsv[..., 2] = cv2.normalize(mag, None, 0, 1, cv2.NORM_MINMAX)
return cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)
示例10: hog
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cartToPolar [as 别名]
def hog(img):
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bins = np.int32(bin_n*ang/(2*np.pi)) # quantizing binvalues in (0...16)
bin_cells = bins[:10,:10], bins[10:,:10], bins[:10,10:], bins[10:,10:]
mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists) # hist is a 64 bit vector
return hist
## [hog]
示例11: hog
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cartToPolar [as 别名]
def hog(img):
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bins = np.int32(bin_n * ang / (2 * np.pi)) # quantizing binvalues in (0...16)
bin_cells = bins[:10, :10], bins[10:, :10], bins[:10, 10:], bins[10:, 10:]
mag_cells = mag[:10, :10], mag[10:, :10], mag[:10, 10:], mag[10:, 10:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists) # hist is a 64 bit vector
return hist
# 最后 和前 一样 我们将大图分割成小图。使用每个数字的前 250 个作 为训练数据
# 后 250 个作为测试数据
示例12: flow2rgb
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cartToPolar [as 别名]
def flow2rgb(flow):
hsv = np.zeros(list(flow.shape[:-1]) + [3], dtype=np.uint8)
hsv[..., 1] = 255
mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
hsv[..., 0] = ang * 180 / np.pi / 2
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
return cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)
示例13: show_flow
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cartToPolar [as 别名]
def show_flow(flow):
# Use Hue, Saturation, Value colour model
hsv = np.zeros((flow.shape[0], flow.shape[1], 3), dtype=np.uint8)
hsv[..., 1] = 255
mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
hsv[..., 0] = ang * 180 / np.pi / 2
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
plt.imshow(bgr)
plt.show()
示例14: flow2im
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cartToPolar [as 别名]
def flow2im(flow):
flow_npy = np.array(flow.detach().cpu().numpy().transpose(1, 2, 0), np.float32)
shape = flow_npy.shape[:-1]
hsv = np.zeros(shape + (3,), dtype=np.uint8)
hsv[..., 2] = 255
mag, ang = cv2.cartToPolar(flow_npy[..., 0], flow_npy[..., 1])
hsv[..., 0] = ang * 180 / np.pi / 2
hsv[..., 1] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
return bgr
示例15: denseOpticalFlow
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cartToPolar [as 别名]
def denseOpticalFlow():
# use 0 for webcam capturing
# cap = cv2.VideoCapture(0)
cap = cv2.VideoCapture('test/Pedestrian overpass.mp4')
ret, frame1 = cap.read()
prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
hsv = np.zeros_like(frame1)
hsv[...,1] = 255
while(1):
ret, frame2 = cap.read()
next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)
flow = cv2.calcOpticalFlowFarneback(prvs,next, None, 0.5, 3, 15, 3, 5, 1.2, 0)
mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
hsv[...,0] = ang*180/np.pi/2
hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
# print(np.sum(mag[100:300, 100:300]))
if (np.sum(mag)> 100000):
print('motion detected')
bgr = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
cv2.imshow('frame2',bgr)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
elif k == ord('s'):
cv2.imwrite('opticalfb.png',frame2)
cv2.imwrite('opticalhsv.png',bgr)
prvs = next
cap.release()
cv2.destroyAllWindows()