首页 > 解决方案 > 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

标签: pythonmatrix-multiplication

解决方案


推荐阅读