我想使用opencv在C ++中复制GIMP滤镜>边缘变形>高斯的差异。
我发现了用于DOG实现的简单代码,但我希望通过两个参数Raidus1和Radius2获得相同的GIMP结果。
Mat g1, g2, result;
Mat img = imread("test.png", CV_LOAD_IMAGE_COLOR);
GaussianBlur(img, g1, Size(1,1), 0);
GaussianBlur(img, g2, Size(3,3), 0);
result = g1 - g2;
如何在实现中添加2个半径参数?
如果可以帮助您,请访问过滤器C实现的链接
https://gitlab.gnome.org/GNOME/gimp/blob/master/plug-ins/common/edge-dog.c
答案 0 :(得分:1)
我没有答案,但是头发已经用完了-请参阅评论。我一直在努力,有一些无法使用的代码,但是比我更聪明但又不喜欢编写代码的人也许能够看到问题所在,所以我想我会分享我所拥有的。我对任何观点都不感兴趣,因此欢迎任何人采纳和改编并给出有效的答案。如果我们找到解决方案,请问我很好。我是用Python编写的,但是我确信如果我们得到了有用的东西,我们可以轻松地将任何Python适应C ++。
#!/usr/bin/env python3
import numpy as np
import math
import cv2
def radius2stdev(radius):
"""
Return std deviation corresponding to a given radius.
I got this from: https://gitlab.gnome.org/GNOME/gimp/blob/master/plug-ins/common/edge-dog.c
"""
stdev = math.sqrt (-(radius * radius) / (2 * math.log (1.0 / 255.0)));
return stdev
# Load image, make float and scale to range 0..1
im = cv2.imread("image.jpg",cv2.IMREAD_COLOR).astype(np.float)
im = im/255.0
stdev1 = radius2stdev(22.0)
stdev2 = radius2stdev(5.0)
print('Stdev1: {}'.format(stdev1))
print('Stdev2: {}'.format(stdev2))
# Generate the two Gaussians and their difference
# I believe OpenCV calculates the size of the kernel to match the std dev if you pass no kernel size
# See https://docs.opencv.org/3.4.1/d4/d86/group__imgproc__filter.html#gaabe8c836e97159a9193fb0b11ac52cf1
g1 = cv2.GaussianBlur(im,(0,0),stdev1,stdev1)
g2 = cv2.GaussianBlur(im,(0,0),stdev2,stdev2)
result = g1 -g2
# Multiply back up by 255 and save as PNG
result = (result * 255).astype(np.uint8)
cv2.imwrite("result.png", result)
# Normalize and save normalised too
resultn = cv2.normalize(result,None,alpha=0,beta=255,norm_type=cv2.NORM_MINMAX)
cv2.imwrite("result-n.png", resultn)
标准偏差打印如下:
Stdev1: 6.608505869104614
Stdev2: 1.5019331520692305
我相信显示的半径22,000和5,000只是您国际化的结果,它们对应于美国/英国格式的22.0和5.0。
我还尝试了在命令行上使用 ImageMagick ,尽管我不确定这能证明什么,但得到了类似的含糊之处。
magick image.jpg -morphology Convolve DoG:0,20,5 -evaluate multiply 6 result.jpg
答案 1 :(得分:0)
似乎缺少一行。 来自lines 729-730 of edge-dog.c:
radius = fabs (radius) + 1.0;
std_dev = sqrt (-(radius * radius) / (2 * log (1.0 / 255.0)));
Gimp然后将高斯计算为make_curve(std_dev,...)中的整数列表。注意