Commit cd6f7d12 authored by LaurentBerger's avatar LaurentBerger

Rewrite deriche filter- add test - add python wrapper

parent bd46b032
......@@ -51,25 +51,25 @@ namespace ximgproc {
*
* For more details about this implementation, please see http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.476.5736&rep=rep1&type=pdf
*
* @param _op Source 8-bit or 16bit image, 1-channel or 3-channel image.
* @param _dst result CV_32FC image with same number of channel than _op.
* @param alphaDerive double see paper
* @param alphaMean double see paper
* @param op Source 8-bit or 16bit image, 1-channel or 3-channel image.
* @param dst result CV_32FC image with same number of channel than _op.
* @param alpha double see paper
* @param omega double see paper
*
*/
CV_EXPORTS void GradientDericheY(InputArray _op, OutputArray _dst, double alphaDerive,double alphaMean);
CV_EXPORTS_W void GradientDericheY(InputArray op, OutputArray dst, double alpha,double omega);
/**
* @brief Applies X Deriche filter to an image.
*
* For more details about this implementation, please see http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.476.5736&rep=rep1&type=pdf
*
* @param _op Source 8-bit or 16bit image, 1-channel or 3-channel image.
* @param _dst result CV_32FC image with same number of channel than _op.
* @param alphaDerive double see paper
* @param alphaMean double see paper
* @param op Source 8-bit or 16bit image, 1-channel or 3-channel image.
* @param dst result CV_32FC image with same number of channel than _op.
* @param alpha double see paper
* @param omega double see paper
*
*/
CV_EXPORTS void GradientDericheX(InputArray _op, OutputArray _dst, double alphaDerive,double alphaMean);
CV_EXPORTS_W void GradientDericheX(InputArray op, OutputArray dst, double alpha,double omega);
}
}
......
import sys
import numpy as np
import cv2 as cv
def AddSlider(sliderName,windowName,minSlider,maxSlider,valDefault, update=[]):
if update is None:
cv.createTrackbar(sliderName, windowName, valDefault,maxSlider-minSlider+1)
else:
cv.createTrackbar(sliderName, windowName, valDefault,maxSlider-minSlider+1, update)
cv.setTrackbarMin(sliderName, windowName, minSlider)
cv.setTrackbarMax(sliderName, windowName, maxSlider)
cv.setTrackbarPos(sliderName, windowName, valDefault)
class Filtrage:
def __init__(self):
self.s =0
self.alpha = 100
self.omega = 100
self.updateFiltre=True
self.img=[]
self.dximg=[]
self.dyimg=[]
self.module=[]
def DericheFilter(self):
self.dximg = cv.ximgproc.GradientDericheX( self.img, self.alpha/100., self.omega/1000. )
self.dyimg = cv.ximgproc.GradientDericheY( self.img, self.alpha/100., self.omega/1000. )
dx2=self.dximg*self.dximg
dy2=self.dyimg*self.dyimg
self.module = np.sqrt(dx2+dy2)
cv.normalize(src=self.module,dst=self.module,norm_type=cv.NORM_MINMAX)
def SlideBarDeriche(self):
cv.destroyWindow(self.filename)
cv.namedWindow(self.filename)
AddSlider("alpha",self.filename,1,400,self.alpha,self.UpdateAlpha)
AddSlider("omega",self.filename,1,1000,self.omega,self.UpdateOmega)
def UpdateOmega(self,x ):
self.updateFiltre=True
self.omega=x
def UpdateAlpha(self,x ):
self.updateFiltre=True
self.alpha=x
def run(self,argv):
# Load the source image
self.filename = argv[0] if len(argv) > 0 else "../doc/pics/corridor_fld.jpg"
self.img=cv.imread(self.filename,cv.IMREAD_GRAYSCALE)
if self.img is None:
print ('cannot read file')
return
self.SlideBarDeriche()
while True:
cv.imshow(self.filename,self.img)
if self.updateFiltre:
self.DericheFilter()
cv.imshow("module",self.module)
self.updateFiltre =False
code = cv.waitKey(10)
if code==27:
break
if __name__ == '__main__':
Filtrage().run(sys.argv[1:])
This diff is collapsed.
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
namespace opencv_test { namespace {
TEST(ximgproc_DericheFilter, regression)
{
Mat img = Mat::zeros(64, 64, CV_8UC3);
Mat res = Mat::zeros(64, 64, CV_32FC3);
img.at<Vec3b>(31, 31) = Vec3b(1, 2, 4);
double a = 0.5;
double w = 0.0005;
Mat dst;
ximgproc::GradientDericheX(img, dst, a, w);
double c = pow(1 - exp(-a), 2.0) * exp(a);
double k = pow(a*(1 - exp(-a)), 2.0) / (1 + 2 * a*exp(-a) - exp(-2 * a));
for (int i = 0; i < img.rows; i++)
{
double n = -31 + i;
for (int j = 0; j < img.cols; j++)
{
double m = -31 + j;
double x = -c * exp(-a * fabs(m))*sin(w*m);
x = x * (k*(a*sin(w*fabs(n)) + w * cos(w*fabs(n)))*exp(-a * fabs(n))) / (a*a + w * w);
x = x / (w*w);
float xx=static_cast<float>(x);
res.at<Vec3f>(i, j) = Vec3f(xx, 2 * xx, 4 * xx);
}
}
EXPECT_LE(cv::norm(res, dst, NORM_INF), 1e-5);
Mat dst2;
ximgproc::GradientDericheY(img, dst2, a, w);
cv::transpose(dst2, dst2);
EXPECT_LE(cv::norm(dst2, dst, NORM_INF), 1e-5);
}
}
} // namespace
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment