使用OpenCV实现径向模糊

时间:2011-09-30 07:50:18

标签: image-processing opencv

我们想要校正数码相机中镜头引入的场曲。我们计划使用digital unsharp mask而不是应用高斯模糊,我们想尝试径向模糊,因此锐化会对图像边缘产生更大的影响。

使用OpenCV创建径向模糊的最简单方法是什么?

3 个答案:

答案 0 :(得分:6)

上面的答案很接近,但遗漏了一些关键因素,让我想一想。 我已经改变了地图,以便他们正确地计算缩放和缩小,并在每个位置从x和y添加/减去它们(否则你最终会将图像重新映射到一个小方块。我也改变了/ blur * *模糊,否则你的地图将包含非常大的数字,而不是正确的(每个位置的极大倍数)。

float center_x = width/2; //or whatever
float center_y = height/2;
float blur = 0.002; //blur radius per pixels from center. 2px blur at 1000px from center
int iterations = 5;

Mat growMapx, growMapy;
Mat shrinkMapx, shrinkMapy;
for(int x = 0; x < width; x++) {
  for(int y = 0; y < height; y++) {
    growMapx[x,y] = x+((x - center_x)*blur);
    growMapy[x,y] = y+((y - center_y)*blur);
    shrinkMapx[x,y] = x-((x - center_x)*blur);
    shrinkMapy[x,y] = y-((y - center_y)*blur);
  }
}

Mat tmp1, tmp2;
for(int i = 0; i < iterations; i++)  {
  remap(src, tmp1, growMapx, growMapy, CV_INTER_LINEAR); // enlarge
  remap(src, tmp2, shrinkMapx, shrinkMapy, CV_INTER_LINEAR); // shrink
  addWeighted(tmp1, 0.5, tmp2, 0.5, 0, src); // blend back to src
}

答案 1 :(得分:0)

我对类似于Photoshop径向运动模糊的东西很感兴趣。如果这也是您正在寻找的,我认为最好的解决方案可能是迭代resize和混合(addWeighted)。也可以使用remap完成。伪代码,或多或少:

float center_x = width/2; //or whatever
float center_y = height/2;
float blur = 0.02; //blur radius per pixels from center. 2px blur at 100px from center
int iterations = 5;

Mat mapx, mapy;
for(int x = 0; x < width; x++) {
   for(int y = 0; y < height; y++) {
       mapx[x,y] = (x - center_x)/blur;
       mapy[x,y] = (y - center_y)/blur;
   }
}

Mat tmp1, tmp2;
for(int i = 0; i < iterations; i++)  {
    remap(src, tmp1, mapx, mapy, CV_INTER_LINEAR); // enlarge
    remap(src, tmp2, -mapx, -mapy, CV_INTER_LINEAR); // shrink
    addWeighted(tmp1, 0.5, tmp2, 0.5, 0, src); // blend back to src
}

答案 2 :(得分:0)

Python代码:

w, h = img.shape[:2]

center_x = w / 2
center_y = h / 2
blur = 0.01
iterations = 5

growMapx = np.tile(np.arange(h) + ((np.arange(h) - center_x)*blur), (w, 1)).astype(np.float32)
shrinkMapx = np.tile(np.arange(h) - ((np.arange(h) - center_x)*blur), (w, 1)).astype(np.float32)
growMapy = np.tile(np.arange(w) + ((np.arange(w) - center_y)*blur), (h, 1)).transpose().astype(np.float32)
shrinkMapy = np.tile(np.arange(w) - ((np.arange(w) - center_y)*blur), (h, 1)).transpose().astype(np.float32)

for i in range(iterations):
    tmp1 = cv2.remap(img, growMapx, growMapy, cv2.INTER_LINEAR)
    tmp2 = cv2.remap(img, shrinkMapx, shrinkMapy, cv2.INTER_LINEAR)
    img = cv2.addWeighted(tmp1, 0.5, tmp2, 0.5, 0)