“操作数无法与形状一起广播(256)(257)” - 这是我尝试运行程序时遇到的错误。谁可以帮我这个事 。 Advance Thanks :)我的图片大小为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
答案 0 :(得分:2)
我不确定你想要计算什么,但行
x=np.histogram(img1,bins=256)
histgray1 =x
和
y=np.histogram(img2,bins=256)
histgray2 =y
如果您为每个histgray1[i]*histgray2[i]
乘以i
,似乎有些偏差。
numpy histogram的返回值为hist, bin_edges
,所以我的猜测是你要将这些行改为
histgray1 = np.histogram(img1,bins=256)[0]
和
histgray2 = np.histogram(img2,bins=256)[0]