我们希望纠正数码相机镜头引入的场曲率。我们计划使用数字不锐化掩模digital unsharp mask,而不是应用高斯模糊,我们想尝试径向模糊,这样锐化对图像边缘的影响更大。
使用OpenCV创建径向模糊的最简单方法是什么?
使用OpenCV创建径向模糊的最简单方法是什么?
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
}
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)
growMapx,growMapy = np.abs(growMapx),np.abs(growMapy)
并在两个cv2.remap调用上使用 borderMode=cv2.BORDER_REFLECT
- Oliver Zendel我对类似于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
}