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

iqjalb3h  于 2024-01-08  发布在  Python
关注(0)|答案(1)|浏览(158)

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

  1. import cv2
  2. import numpy as np
  3. import pandas as pd
  4. cap = cv2.VideoCapture('bubble_cut_1mov.mp4')
  5. listx = []
  6. listy = []
  7. ret, frame = cap.read()
  8. height, width, _ = frame.shape
  9. x_center, y_center = width // 2, height // 2
  10. x_min, x_max = x_center - 50, x_center + 50
  11. y_min, y_max = y_center - 50, y_center + 50
  12. while ret:
  13. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  14. edges = cv2.Canny(gray, 50, 150)
  15. # Find contours
  16. contours, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  17. for contour in contours:
  18. for point in contour:
  19. x, y = point.ravel()
  20. if x < x_min or x > x_max or y < y_min or y > y_max:
  21. listx.append(x)
  22. listy.append(y)
  23. cv2.drawContours(frame, [contour], -1, (0, 255, 0), 1)
  24. cv2.imshow('Contours from video', frame)
  25. if cv2.waitKey(1) & 0xFF == ord('q'):
  26. break
  27. ret, frame = cap.read()
  28. cap.release()
  29. cv2.destroyAllWindows()
  30. min_x = min(listx)
  31. max_x = max(listx)
  32. min_y = min(listy)
  33. max_y = max(listy)
  34. print("Minimum x:", min_x)
  35. print("Maximum x:", max_x)
  36. print("Minimum y:", min_y)
  37. print("Maximum y:", max_y)
  38. data = {'x': listx, 'y': listy}
  39. df = pd.DataFrame(data)
  40. 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的其他属性。
完整代码:

  1. import cv2
  2. import numpy as np
  3. import pandas as pd
  4. cap = cv2.VideoCapture('bubble_cut_1mov.mov')
  5. listx = []
  6. listy = []
  7. listf = [] # HERE, frame number
  8. listt = [] # HERE, timestamp
  9. ret, frame = cap.read()
  10. height, width, _ = frame.shape
  11. x_center, y_center = width // 2, height // 2
  12. x_min, x_max = x_center - 50, x_center + 50
  13. y_min, y_max = y_center - 50, y_center + 50
  14. while ret:
  15. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  16. edges = cv2.Canny(gray, 50, 150)
  17. # Find contours
  18. contours, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  19. for contour in contours:
  20. for point in contour:
  21. x, y = point.ravel()
  22. f = cap.get(cv2.CAP_PROP_POS_FRAMES) # HERE
  23. t = cap.get(cv2.CAP_PROP_POS_MSEC) # HERE
  24. if x < x_min or x > x_max or y < y_min or y > y_max:
  25. listx.append(x)
  26. listy.append(y)
  27. listf.append(f) # HERE
  28. listt.append(t) # HERE
  29. cv2.drawContours(frame, [contour], -1, (0, 255, 0), 1)
  30. cv2.imshow('Contours from video', frame)
  31. if cv2.waitKey(1) & 0xFF == ord('q'):
  32. break
  33. ret, frame = cap.read()
  34. cap.release()
  35. cv2.destroyAllWindows()
  36. min_x = min(listx)
  37. max_x = max(listx)
  38. min_y = min(listy)
  39. max_y = max(listy)
  40. print("Minimum x:", min_x)
  41. print("Maximum x:", max_x)
  42. print("Minimum y:", min_y)
  43. print("Maximum y:", max_y)
  44. data = {'f': listf, 't': listt, 'x': listx, 'y': listy} # HERE
  45. df = pd.DataFrame(data)
  46. df.to_excel('contours_data.xlsx', index=False)

字符串
输出量:

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

展开查看全部

相关问题