Question

"operands could not be broadcast together with shapes (256) (257)" - This is the error i get when i try to run my program. Can anyone help me on this . Advance Thanks:) My image is of size 181x256 . I am trying to find Bhattacharya coefficient of two images

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
Was it helpful?

Solution

I'm not exactly sure of what you want to compute, but the lines

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

and

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

seem a bit off given that you are multiplying histgray1[i]*histgray2[i] for each i.

The return value of a numpy histogram is hist, bin_edges so my guess is that you want to change those lines to

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

and

histgray2 = np.histogram(img2,bins=256)[0]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top