问题描述
我想使用opencv在C ++中复制GIMP滤镜>边缘变形>高斯的差异.
I want to replicate the GIMP Filter > Edge Decect > Difference of Gaussians in C++ using opencv.
我发现了用于DOG实现的简单代码,但我希望通过两个参数Raidus1和Radius2获得相同的GIMP结果.
I found this simple code for DOG implementation bu I want the same result of GIMP with the two params Raidus1 and Radius2.
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个半径参数?
How can add the 2 radius params to the implementation?
样本输入图像
Params
输出
如果可以帮助您,请访问过滤器C实现的链接
If can help this is the link to the C implementation of the filter
https://gitlab .gnome.org/GNOME/gimp/blob/master/plug-ins/common/edge-dog.c
推荐答案
我没有答案,但是已经用光了-请参阅注释.我一直在从事此工作,有一些无法使用的代码,但是比我更聪明但又不喜欢编写代码的人也许能够看到问题所在,所以我想我会分享我所拥有的.我对任何观点都不感兴趣,因此欢迎任何人采纳和改编并给出有效的答案.如果我们找到解决办法,请问我很好.我是用Python编写的,但是我确信如果我们得到了有用的东西,我们可以轻松地将任何Python改编成C ++.
I don't have the answer, but have run out of hair - see comment. I have been working on this and have some code that doesn't work, but somebody cleverer than me who doesn't feel like writing code, may be able to see what is wrong, so I thought I would share what I have. I am not interested in any points, so anyone is welcome to take and adapt this and show a working answer. Fine by me if we find a solution. I did it in Python but I am sure we can easily adapt any Python to C++ if we get something that works.
#!/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.
I believe the 22,000 and 5,000 shown for your radii are just a result of your internationalisation and these correspond to 22.0 and 5.0 in US/UK format.
我也尝试在命令行上使用 ImageMagick ,尽管我不确定这能证明什么,但得到了类似的含糊之处.
I also had an attempt with ImageMagick on the command-line and got something vaguely similar though I am not sure what that proves:
magick image.jpg -morphology Convolve DoG:0,20,5 -evaluate multiply 6 result.jpg
这篇关于Opencv中高斯人的GIMP差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!