# histogram opencv c++ vs python

I'm converting histogram function which is written in c++ into python.

    Mat  Detection:: histogram(Mat img,bool vis)
{
int bins = 59;             // number of bins 256 -1 to ignore the last patterns (background pattern)

Mat hist;       // histogram arrays
// Initalize histogram array
hist = Mat::zeros(bins, 1, CV_32FC1);
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
uchar val =img.at<uchar>(i,j);
hist.at<float>(lookup[val]) += 1;   #on this line I'm facing the problem(is it talking about the lookup table? )
}
}

return hist;
}


# python conversion

def historgram(image):
#cv2.historgram()
bins = 59
row,col,channels = image.shape

hist = np.zeros((bins, 1), dtype = 'float32')
i = 0
j =0
while i < row:
while j < col:
val = image[i,j]
""
""

j = j+1
i = 1+i
return hist


How should I do the conversion in python of this part [hist.at<float>(lookup[val]) += 1]?

Thankyou

edit retag close merge delete

how would we know, without any context ?

( 2019-01-01 03:22:31 -0500 )edit
1

I just updated the code Kindly look into it

( 2019-01-01 03:29:44 -0500 )edit

Sort by ยป oldest newest most voted

since this looks like the "uniform lbp" lookup, there should have been a table / array, like this:

lookup = [   # the well known original uniform2 pattern
0,1,2,3,4,58,5,6,7,58,58,58,8,58,9,10,11,58,58,58,58,58,58,58,12,58,58,58,13,58,
14,15,16,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,17,58,58,58,58,58,58,58,18,
58,58,58,19,58,20,21,22,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,
58,58,58,58,58,58,58,58,58,58,58,58,23,58,58,58,58,58,58,58,58,58,58,58,58,58,
58,58,24,58,58,58,58,58,58,58,25,58,58,58,26,58,27,28,29,30,58,31,58,58,58,32,58,
58,58,58,58,58,58,33,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,34,58,58,58,58,
58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,
58,35,36,37,58,38,58,58,58,39,58,58,58,58,58,58,58,40,58,58,58,58,58,58,58,58,58,
58,58,58,58,58,58,41,42,43,58,44,58,58,58,45,58,58,58,58,58,58,58,46,47,48,58,49,
58,58,58,50,51,52,58,53,54,55,56,57]


think of it as some kind of "compression", going from [0..255] values to [0..58].

then inside your loops, you can use it like:

val = image[i,j]
hist[lookup[val]] += 1

more

1

Thanks a lot :)

( 2019-01-01 04:58:16 -0500 )edit

( 2019-01-01 07:53:44 -0500 )edit

Official site

GitHub

Wiki

Documentation