操作数无法以形状 (256) (257) 广播在一起

0 投票
1 回答
2573 浏览
提问于 2025-04-18 00:16

“操作数无法一起广播,形状分别是 (256) 和 (257)” - 这是我在运行程序时遇到的错误。有人能帮我解决这个问题吗?提前谢谢大家 :) 我的图片大小是 181x256。我正在尝试计算两张图片的 Bhattacharya 系数。

from numpy import *
from PIL import Image
from scipy import misc
from scipy import stats
import sys
import math
import os
import itertools
import numpy as np
import scipy.signal
import cv2

histgray1 = np.zeros(255)
histgray2 = np.zeros(255)

def histo(path1,path2) :
    img1 = cv2.imread(path1)
    img2 = cv2.imread(path2)

    red1 = img1[:,:,2]
    green1= img1[:,:,1]
    blue1 = img1[:,:,0]
    histgray1 = 0.299 * red1 + 0.587 * green1 + 0.114 * blue1
    x=np.histogram(img1,bins=256)
    #print x
    histgray1 =x

    red2 = img2[:,:,2]
    green2 = img2[:,:,1]
    blue2 = img2[:,:,0]
    histgray2 = 0.299 * red2 + 0.587 * green2 + 0.114 * blue2
    y=np.histogram(img2,bins=256)
    histgray2 =y
    sumvalue=0.0
    for i in range(0,255):
        sumvalue=sumvalue + (histgray1[i]*histgray2[i])
    BC=math.sqrt(sumvalue)
    return BC

path1=''
path2=''   
BC = histo(path1,path2)
print BC

1 个回答

2

我不太确定你想计算什么,但这几行代码

x=np.histogram(img1,bins=256)
histgray1 =x

y=np.histogram(img2,bins=256)
histgray2 =y

看起来有点问题,因为你在对每个 i 进行 histgray1[i]*histgray2[i] 的乘法运算。

一个 numpy 的直方图 返回的是 hist, bin_edges,所以我猜你可能想把这些代码改成

histgray1 = np.histogram(img1,bins=256)[0]

histgray2 = np.histogram(img2,bins=256)[0]

撰写回答