如何量化cartToPolar输出以估计连续帧Python OpenCV之间的流量? - python

如何量化cartToPolar的输出以估计连续帧之间的流量?

为了简单起见,这里输出两帧

import cv2

img_1 = cv2.imread('0.png')
img_2 = cv2.imread('1.png')

frame_1 = cv2.cvtColor(img_1, cv2.COLOR_BGR2GRAY)
frame_2 = cv2.cvtColor(img_2, cv2.COLOR_BGR2GRAY)

flow = cv2.calcOpticalFlowFarneback(frame_1, frame_2, None, 0.5, 3, 21, 3, 7, 1.2, 0)

magnitude, angle = cv2.cartToPolar(flow[..., 0], flow[..., 1])

print(magnitude)

[[0.0001812  0.00021884 0.00027348 ... 0.00620058 0.0056933  0.00466164]
 [0.00022236 0.00027613 0.00034918 ... 0.00769999 0.0070625  0.00579038]
 [0.00027409 0.00034347 0.00043545 ... 0.00825442 0.00754972 0.00617364]
 ...
 [0.02791223 0.03603577 0.04387245 ... 0.00049675 0.00026466 0.00018964]
 [0.01995636 0.02411344 0.0282621  ... 0.00040552 0.00023594 0.00017011]
 [0.01731196 0.02069538 0.0240274  ... 0.00033382 0.00022241 0.00016816]]

根据文件

幅度–与x大小和类型相同的幅度输出数组。

如何量化此输出,以便可以估计连续帧之间的流量?

参考方案

我有一个示例,可从.bsq帧获取像素幅度转换。您可以修改代码以输入视频文件。您可能对get_translation()函数最感兴趣。例:

如何量化cartToPolar输出以估计连续帧Python OpenCV之间的流量? - python

显示帧间像素转换的图形

如何量化cartToPolar输出以估计连续帧Python OpenCV之间的流量? - python

import numpy as np
import argparse
import os
import cv2
from matplotlib import pyplot as plt
from matplotlib import cm
import time
import random

# Usage: python translate_analyzer.py -p <filename.bsq>

# Automatic brightness and contrast optimization with optional histogram clipping
def automatic_brightness_and_contrast(image, clip_hist_percent=25):
    if len(image.shape) == 3:
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    else:
        gray = image

    # Calculate grayscale histogram
    hist = cv2.calcHist([gray],[0],None,[256],[0,256])
    hist_size = len(hist)

    # Calculate cumulative distribution from the histogram
    accumulator = []
    accumulator.append(float(hist[0]))
    for index in range(1, hist_size):
        accumulator.append(accumulator[index -1] + float(hist[index]))

    # Locate points to clip
    maximum = accumulator[-1]
    clip_hist_percent *= (maximum/100.0)
    clip_hist_percent /= 2.0

    # Locate left cut
    minimum_gray = 0
    while accumulator[minimum_gray] < clip_hist_percent:
        minimum_gray += 1

    # Locate right cut
    maximum_gray = hist_size -1
    while accumulator[maximum_gray] >= (maximum - clip_hist_percent):
        maximum_gray -= 1

    # Calculate alpha and beta values
    alpha = 255 / (maximum_gray - minimum_gray)
    beta = -minimum_gray * alpha

    auto_result = cv2.convertScaleAbs(image, alpha=alpha, beta=beta)
    return (auto_result, alpha, beta)

# Draw flow
def draw_flow(img, flow, step=30):
    h, w = img.shape[:2]
    y, x = np.mgrid[step/2:h:step, step/2:w:step].reshape(2,-1).astype(int)
    fx, fy = flow[y,x].T
    lines = np.vstack([x, y, x+fx, y+fy]).T.reshape(-1, 2, 2)
    lines = np.int32(lines + 0.5)
    vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    cv2.polylines(vis, lines, 1, (36, 255, 12))
    for (x1, y1), (_x2, _y2) in lines:
        cv2.circle(vis, (x1, y1), 2, (36, 255, 12), -1)
    return vis

# Return translation value
def get_translation(img, flow, step=30):
    return (np.median(flow[:,:,0].T), flow[:, :, 0].T)

# Get file path
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--path", help="Path to the directory")
args = vars(ap.parse_args())

if not args['path']:
    print('Usage: python translate_analyzer.py -p <directory>')
    exit(1)

# Extract file name
bsq_fname = os.path.split(args['path'])[-1]

if '.bsq' not in bsq_fname:
    print('ERROR: Invalid bsq file. Select correct file.')
    exit(1)

width = 640
height = 512
frame_count = int(os.path.getsize(bsq_fname)/(2*height*width))
x,y,w,h = 0,0,100,512

# Simulates calibrated frames to display on video frame
data_file = np.fromfile(bsq_fname, dtype=np.uint16, count=-1)
data_file = data_file.reshape((width, height, frame_count), order='F')
data_file = np.rot90(data_file)

print(bsq_fname)
fname = bsq_fname.split()[0]
prev = data_file[:,:,0].copy()
prev //= 64
prev = automatic_brightness_and_contrast(prev)[0]
prev = prev[y:y+h, x:x+w]

translation_data = []
frame_direction = []
start = time.time()
for index in range(1, frame_count):
    data = data_file[:,:,index].copy()
    data //= 64
    data = automatic_brightness_and_contrast(data)[0]
    data = data[y:y+h, x:x+w]

    flow = cv2.calcOpticalFlowFarneback(prev=prev, next=data, flow=None, pyr_scale=0.5, levels=2, winsize=80, iterations=2, poly_n=7, poly_sigma=4.5, flags=0)
    translation, pixel_direction = get_translation(data, flow)
    prev = data

    cv2.imshow('flow', draw_flow(data, flow))
    cv2.waitKey(1)

    translation_data.append(translation)
    frame_direction = pixel_direction

    index = (index+1) % frame_count

end = time.time()
print('Time:', end - start)

plt.figure()
plt.title(bsq_fname)
plt.xlabel("Frames")
plt.ylabel("Magnitude")
plt.plot(translation_data)

plt.figure()
plt.title("Pixel Direction")
plt.xlabel("Width")
plt.ylabel("Height")
plt.imshow(frame_direction.T)
plt.colorbar(orientation='vertical')
plt.show()

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…

将字符串分配给numpy.zeros数组[重复] - python

This question already has answers here: Weird behaviour initializing a numpy array of string data                                                                    (4个答案)         …

Python:传递记录器是个好主意吗? - python

我的Web服务器的API日志如下:started started succeeded failed 那是同时收到的两个请求。很难说哪一个成功或失败。为了彼此分离请求,我为每个请求创建了一个随机数,并将其用作记录器的名称logger = logging.getLogger(random_number) 日志变成[111] started [222] start…