Repository: opencv
Issue: Discrepancy between gaussian blur implemented with pytorch conv and OpenCV
Labels: category: imgproc,needs investigation,confirmed,Hackathon

##### System information (version)
<!-- Example
- OpenCV => 4.3.0.36
- Operating System / Platform => Ubuntu 18.04
- Pytorch => 1.5.1
-->

- OpenCV => 4.3.0.36
- Operating System / Platform => Ubuntu 18.04
- Pytorch => 1.5.1

##### Detailed description

As far as I know, OpenCV’s GaussianBlur is wrapping sepFilter2D, which is exactly same with the below implementation in pytorch.

However, these two implementation yield ~4% difference in below (image, kernel_size, sigma).
The difference becomes larger worse when the rounding (`img = img.round()`) is removed (reaches ~50%)

##### Steps to reproduce

```.py
from PIL import Image
from torchvision import transforms

import cv2
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import requests
from io import BytesIO

class GaussianBlur(object):
    def __call__(self, img):
        sigma = 2
        after = cv2.GaussianBlur(np.asarray(img), (23, 23), sigma)
        return after

class GaussianBlur2(object):
    """blur a single image on CPU"""
    def __init__(self, kernel_size=23):
        radias = kernel_size // 2
        kernel_size = radias * 2 + 1
        self.pad = nn.ReflectionPad2d(radias)
        self.blur_h = nn.Conv2d(3, 3, kernel_size=(kernel_size, 1),
                                stride=1, padding=0, bias=False, groups=3)
        self.blur_v = nn.Conv2d(3, 3, kernel_size=(1, kernel_size),
                                stride=1, padding=0, bias=False, groups=3)
        self.k = kernel_size
        self.r = radias

        self.tensor_to_pil = transforms.ToPILImage()

    def __call__(self, img):
        img = torch.tensor(np.asarray(img)).unsqueeze(0).float()
        img = img.permute(0, 3, 1, 2)

        sigma = 2
        x = np.arange(-self.r, self.r + 1)
        x = np.exp(-np.power(x, 2) / (2 * sigma * sigma))
        x = x / x.sum()
        x = torch.from_numpy(x).view(1, -1).repeat(3, 1)

        self.blur_h.weight.data.copy_(x.view(3, 1, self.k, 1))
        self.blur_v.weight.data.copy_(x.view(3, 1, 1, self.k))

        with torch.no_grad():
            img = self.pad(img)
            img = self.blur_h(img)
            img = self.blur_v(img)
            img = img.round()
            img = img.squeeze()


        img = self.tensor_to_pil((img / 255).float().cpu())

        return img

response = requests.get('https://img.sunset02.com/sites/default/files/styles/marquee_large_2x/public/image/2017/04/main/el-capitan-getty-0517.jpg?itok=vuJS4hXh')
img = Image.open(BytesIO(response.content)).convert('RGB')
cv2img = Image.fromarray(GaussianBlur()(img))
cv2img_tensor = transforms.ToTensor()(cv2img)
torchimg = GaussianBlur2()(img)
torchimg_tensor = transforms.ToTensor()(torchimg)

match = (cv2img_tensor == torchimg_tensor).float()
print(match.sum() / (match.shape[0] * match.shape[1] * match.shape[2]))
print(np.asarray(cv2img)[np.asarray(cv2img) != np.asarray(torchimg)])
print(np.asarray(torchimg)[np.asarray(cv2img) != np.asarray(torchimg)])

>>> tensor(0.9617)
>>> [226 161 173 ...  22  18   3]
>>> [227 162 172 ...  23  19   2]
```

##### Issue submission checklist

 - [x] I report the issue, it's not a question
   <!--
   OpenCV team works with answers.opencv.org, Stack Overflow and other communities
   to discuss problems. Tickets with question without real issue statement will be
   closed.
   -->
 - [x] I checked the problem with documentation, FAQ, open issues,
       answers.opencv.org, Stack Overflow, etc and have not found solution
   <!--
   Places to check:
   * OpenCV documentation: https://docs.opencv.org
   * FAQ page: https://github.com/opencv/opencv/wiki/FAQ
   * OpenCV forum: https://answers.opencv.org
   * OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
   * Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
   -->
 - [x] I updated to latest OpenCV version and the issue is still there
   <!--
   master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
   OpenCV team supports only latest release for each branch.
   The ticket is closed, if the problem is not reproduced with modern version.
   -->
 - [x] There is reproducer code and related data files: videos, images, onnx, etc
   <!--
   The best reproducer -- test case for OpenCV that we can add to the library.
   Recommendations for media files and binary files:
   * Try to reproduce the issue with images and videos in opencv_extra repository
     to reduce attachment size
   * Use PNG for images, if you report some CV related bug, but not image reader
     issue
   * Attach the image as archite to the ticket, if you report some reader issue.
     Image hosting services compress images and it breaks the repro code.
   * Provide ONNX file for some public model or ONNX file with with random weights,
     if you report ONNX parsing or handling issue. Architecture details diagram
     from netron tool can be very useful too. See https://lutzroeder.github.io/netron/
   -->