我没有答案,但头发用完了 - 见评论。我一直在研究这个并且有一些不起作用的代码,但是比我更聪明但不想编写代码的人可能能够看到哪里出了问题,所以我想我会分享我所拥有的。我对任何观点都不感兴趣,所以欢迎任何人接受并调整它并展示一个有效的答案。如果我们找到解决方案,我很好。我是在 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