# Difference in same operations between Python and C++ OpenCV Code

I've been working on BRISQUE IQA for Python and C++ for a while now. There is a set of code in the source code for C++ : Note: In the given code, orig_bw is the input image I've read using imread function (in grayscale).

```
int scalenum = 2;
for (int itr_scale = 1; itr_scale<=scalenum; itr_scale++)
{
Size dst_size(orig_bw.cols/cv::pow((double)2, itr_scale-1), orig_bw.rows/pow((double)2, itr_scale-1));
Mat imdist_scaled;
resize(orig_bw, imdist_scaled, dst_size, 0, 0, INTER_CUBIC); // INTER_CUBIC
imdist_scaled.convertTo(imdist_scaled, CV_64FC1, 1.0/255.0);
Mat mu(imdist_scaled.size(), CV_64FC1, 1);
GaussianBlur(imdist_scaled, mu, Size(7, 7), 1.166);
Mat mu_sq(imdist_scaled.size(), CV_64FC1, 1);
mu_sq = mu.mul(mu);
//compute sigma
Mat sigma(imdist_scaled.size(), CV_64FC1, 1);
sigma = imdist_scaled.mul(imdist_scaled);
GaussianBlur(sigma, sigma, Size(7, 7), 1.166);
subtract(sigma, mu_sq, sigma);
cv::pow(sigma, double(0.5), sigma);
//compute structdis = (x-mu)/sigma
add(sigma, Scalar(1.0/255), sigma);
//cvAddS(sigma, cvScalar(1.0/255), sigma);
Mat structdis(imdist_scaled.size(), CV_64FC1, 1);
subtract(imdist_scaled, mu, structdis);
divide(structdis, sigma, structdis);
//cvDiv(structdis, sigma, structdis);
//Compute AGGD fit
double lsigma_best, rsigma_best, gamma_best;
structdis = AGGDfit(structdis, lsigma_best, rsigma_best, gamma_best);
```

So this is nothing major what's happening above, just gaussian blur, addition, division and multiplication operations. I tried to convert the above set to python as follows:

Note: In the given code, im_ is the input image I'm taking using imread function. I'm just testing for one iteration, and there is some change in the amount of negative pixels + positive pixels in structdis. >

```
scalenum = 2
feat = []
im_original = im_.copy()
for itr_scale in range(scalenum):
im = im_original.copy()
im = im / 255.0
mu = np.zeros((im.shape[0], im.shape[1]), dtype="float64")
mu += 255.0
mu_ = cv2.GaussianBlur(im, (7, 7), 1.166)
mu = mu_.copy()
mu_sq = mu * mu
sigma = im * im
sigma = cv2.GaussianBlur(sigma, (7, 7), 1.166)
sigma = mu_sq - sigma
sigma = abs(sigma) ** 0.5
sigma = sigma + 1.0/255
structdis = mu - im
structdis /= sigma
'''
sigma = np.sqrt(abs(cv2.GaussianBlur(im*im, (7, 7), 1.166) - mu_sq))
structdis = (mu-im)/(sigma+(1.0/255))
'''
structdis = AGGDfit(structdis)
```

Now, the AGGDfit function has some operations where it finds the number of positive pixel points and the negative pixel points, so for both the total number of negative and positive pixel points differ by a small amount (around 300-400) [here positive means > 0 and negative means < 0, and NOT >= or <=] . Why would that be? Is there any difference possible in the outputs of GaussianBlur of C++ and Python APIs?

`sigma = abs(sigma) ** 0.5`

<-- the abs() is probably to avoid NaN's, but your c++ code does not have it (it should, no ?).then, the final lines:

vs.

Thanks @berak, well, I was unsure about the subtraction order, so I included abs for that. If I remove that, it also works fine.

I did structdis = im - mu, and I did get close to the points, but there is still some difference.

Has it something to do with the way Mat objects are first initialized as blank

whiteimages in C++ and not in Python?your (c++) initialization has no effect, since it is overwritten in all cases above anyway. maybe better don't do any initialization at all (else you confuse yourself)

similar problem in the py code here:

Ohkay, is there any possibility of a slight difference in Gaussian Blur of C++ and Python?

another reversed line:

vs.

unlikely