python - column_mult = np.multiply(column_sums, column_numbers) ValueError: 操作数不能与形状一起广播 (1,1920) (1,640)
问题描述
我正在尝试编写一个程序来告诉我在相机视图中对象的确切坐标。
我正在关注我在 YouTube 视频上看到的代码,因为我是 Python 新手,在这部分代码中,我编写了视频中显示的所有内容,但它无法正常工作。它不能与形状 (1,1920) (1,640) 一起广播。
代码:
import urllib.request
import cv2
import numpy as np
import math
URL = "http://123434543543253"
cm_to_pixel = 11.3 / 640.0
while (1):
img_arr = np.array(bytearray(urllib.request.urlopen(URL).read()), dtype=np.uint8)
img = cv2.imdecode(img_arr, -(1))
gray_image1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("background", gray_image1)
k = cv2.waitKey(5)
if k == 27:
break
while (1):
img_arr = np.array(bytearray(urllib.request.urlopen(URL).read()), dtype=np.uint8)
img = cv2.imdecode(img_arr, -(1))
gray_image2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("foreground", gray_image2)
Difference = np.absolute(np.matrix(np.int16(gray_image1)) - np.matrix(np.int16(gray_image2)))
Difference[Difference > 255] = 255
Difference = np.uint8(Difference)
cv2.imshow("difference", Difference)
BW = Difference
BW[BW <= 100] = 0
BW[BW > 100] = 1
column_sums = np.matrix(np.sum(BW, 0))
column_numbers = np.matrix(np.arange(640))
column_mult = np.multiply(column_sums, column_numbers)
total = np.sum(column_mult)
total_total = np.sum(np.sum(BW))
column_location = total / total_total
X_Location = column_location * cm_to_pixel
print(X_Location)
k = cv2.waitKey(5)
if k == 27:
break
解决方案
推荐阅读
- javascript - Javascript Footer Callback Sum Values in one column for each unique combine value in a 2 other columns
- angular - 以编程方式检查是否提供了 Angular ng-content 插槽
- vue.js - Vue for循环多行输出
- python - 如何在播放器之间进行循环功能切换?
- visual-studio-code - 是否可以推荐本地 VSIX 文件扩展名?
- mysql - 如何从行中选择最大值并加入另一个表
- python - 如何将特定模块的 `add_module_names` 更改为 `False`?
- c++ - 应该如何在类中使用常量变量编写 setter?
- rule-engine - 使用 List 属性评估对象
- r - 使用 dplyr::mutate 计算“平均销售份额”