numpy 检测视频代码python上的特定坐标

iqjalb3h  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(135)

所以我是这个编码领域的初学者,我写了一些代码,使用openCV来检测我的泡沫的边缘。
Video of the bubble
最终结果输出了许多x和y坐标,但它们有点奇怪,很难处理,总体上看起来它与它们应该是的坐标不匹配,也许我错了。
我想做的是得到3分:
1.得到y在每个时刻的最大值的坐标,所以在1s y是n,在1.1s y是n1,等等。
1.对于x的两个坐标,也就是x1和x2(气泡边缘的最左边和最右边的坐标),得到相同的结果。

import cv2
import numpy as np
import pandas as pd

cap = cv2.VideoCapture('bubble_cut_1mov.mp4')

listx = []
listy = []

ret, frame = cap.read()
height, width, _ = frame.shape
x_center, y_center = width // 2, height // 2
x_min, x_max = x_center - 50, x_center + 50
y_min, y_max = y_center - 50, y_center + 50

while ret:
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, 50, 150)

    # Find contours
    contours, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for contour in contours:
        for point in contour:
            x, y = point.ravel()
            if x < x_min or x > x_max or y < y_min or y > y_max:
                listx.append(x)
                listy.append(y)
                cv2.drawContours(frame, [contour], -1, (0, 255, 0), 1)

    cv2.imshow('Contours from video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    ret, frame = cap.read()

cap.release()

cv2.destroyAllWindows()

min_x = min(listx)
max_x = max(listx)
min_y = min(listy)
max_y = max(listy)

print("Minimum x:", min_x)
print("Maximum x:", max_x)
print("Minimum y:", min_y)
print("Maximum y:", max_y)

data = {'x': listx, 'y': listy}
df = pd.DataFrame(data)

df.to_excel('contours_data.xlsx', index=False)

字符串
我试着用pandas来获取时间戳,但没有用,现在代码将所有x和y值输出到excel中。请帮助,我似乎无法完成。

2j4z5cfb

2j4z5cfb1#

您可以使用VideoCapture.get检索有关帧编号(cv2.CAP_PROP_POS_FRAMES)或时间(cv2.CAP_PROP_POS_MSEC)的一些信息。您可以检查VideoCaptureProperties的其他属性。
完整代码:

import cv2
import numpy as np
import pandas as pd

cap = cv2.VideoCapture('bubble_cut_1mov.mov')

listx = []
listy = []
listf = []  # HERE, frame number
listt = []  # HERE, timestamp

ret, frame = cap.read()
height, width, _ = frame.shape
x_center, y_center = width // 2, height // 2
x_min, x_max = x_center - 50, x_center + 50
y_min, y_max = y_center - 50, y_center + 50

while ret:
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, 50, 150)

    # Find contours
    contours, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for contour in contours:
        for point in contour:
            x, y = point.ravel()
            f = cap.get(cv2.CAP_PROP_POS_FRAMES)  # HERE
            t = cap.get(cv2.CAP_PROP_POS_MSEC)  # HERE
            if x < x_min or x > x_max or y < y_min or y > y_max:
                listx.append(x)
                listy.append(y)
                listf.append(f)  # HERE
                listt.append(t)  # HERE
                cv2.drawContours(frame, [contour], -1, (0, 255, 0), 1)

    cv2.imshow('Contours from video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    ret, frame = cap.read()

cap.release()

cv2.destroyAllWindows()

min_x = min(listx)
max_x = max(listx)
min_y = min(listy)
max_y = max(listy)

print("Minimum x:", min_x)
print("Maximum x:", max_x)
print("Minimum y:", min_y)
print("Maximum y:", max_y)

data = {'f': listf, 't': listt, 'x': listx, 'y': listy}  # HERE
df = pd.DataFrame(data)

df.to_excel('contours_data.xlsx', index=False)

字符串
输出量:

>>> df
             f             t    x    y
0          1.0      0.000000  478  610
1          1.0      0.000000  477  611
2          1.0      0.000000  473  611
3          1.0      0.000000  472  612
4          1.0      0.000000  469  612
...        ...           ...  ...  ...
250139  1449.0  24133.333333  513  623
250140  1449.0  24133.333333  509  623
250141  1449.0  24133.333333  508  622
250142  1449.0  24133.333333  501  622
250143  1449.0  24133.333333  500  621

相关问题