Skip to content

Instantly share code, notes, and snippets.

@ajonnet
Created August 30, 2018 12:49
Show Gist options
  • Save ajonnet/809309d65e430de55f509305cdf4f311 to your computer and use it in GitHub Desktop.
Save ajonnet/809309d65e430de55f509305cdf4f311 to your computer and use it in GitHub Desktop.
Function to demo Gabor Filter params
void gaborFilterDemo() {
string inpImgFPath = "testImg.png";
cv::Mat inpImg = imread(inpImgFPath);
cv::Mat grayInpImg;
cvtColor(inpImg,grayInpImg,CV_BGR2GRAY);
cv::Mat grayInpImg_F;
grayInpImg.convertTo(grayInpImg_F,CV_32F);
int kernel_size = 5;
double sig = 5, th = 0, lm = 8, gm = 0.02, ps = 0;
//Vector<tupe<parm,title,inc_val>
vector<tuple<double*,string,float>> parms = {make_tuple(&th,"Thetha",0.1),make_tuple(&lm,"lambda",1),make_tuple(&sig,"sigma",1),make_tuple(&gm,"Gamma",0.01),make_tuple(&ps,"Phase",1)};
int prmIdx=0;
while(true) {
Mat kernel = cv::getGaborKernel(cv::Size(kernel_size,kernel_size), sig, th, lm, gm, ps, CV_32F);
Mat dest;
filter2D(grayInpImg_F, dest, CV_32F, kernel);
cv::Mat normDest;
cv::normalize( dest, normDest,0,255,NORM_MINMAX);
vector<cv::Mat> imgs = {inpImg,grayInpImg,dest,normDest};
cv::Mat opImg = utils::renderImgsToHorizontalStack(imgs);
imshow("OP",opImg);
Mat bkernel = cv::getGaborKernel(cv::Size(101,101), sig, th, lm, gm, ps, CV_32F);
Mat normbKernel;
cv::normalize(bkernel,normbKernel,0,255,NORM_MINMAX);
cv::Mat kernalOpImg = utils::renderImgsToHorizontalStack({bkernel,normbKernel});
imshow("Kernal",bkernel);
printf("sig: %f, th: %f, lm:%f, gm:%f, ps:%f\n",sig,th,lm,gm,ps);
uchar key = waitKey(0);
if(key == 'q') {
break;
}else if(key == 'w') {
double *parmVar;
string title;
float incVal;
tie(parmVar,title,incVal) = parms[prmIdx];
*parmVar+=incVal;
}else if(key == 's') {
double *parmVar;
string title;
float incVal;
tie(parmVar,title,incVal) = parms[prmIdx];
*parmVar-=incVal;
}else if(key == 'a') {
prmIdx-=1;
if(prmIdx < 0) prmIdx= parms.size() -1;
string title = std::get<1>(parms[prmIdx]);
cout<<title<<" chosen"<<endl;
}else if(key == 'd') {
prmIdx+=1;
if(prmIdx >= parms.size()) prmIdx= 0;
string title = std::get<1>(parms[prmIdx]);
cout<<title<<" chosen"<<endl;
}else {
//cout<<key<<", "<<(int)key<<endl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment