使用sobel的奇怪方向图。怎么了?

时间:2014-04-09 05:36:34

标签: c++ image opencv orientation fingerprint

我尝试按照这种方法绘制方向图http://answers.opencv.org/question/9493/fingerprint-orientation-map-through-gradient/

我在480x320图像上使用了5x5的块大小。我得到的梯度是0-270度。并且有一些常数值不断重复,如44.7623和224.762。我想知道我的渐变是不是错了。

之后,我添加5x5块中的所有渐变并将它们除以25(平均值),就像链接所说的那样。我将度数划分为8个45度的区间并绘制出来。但它看起来不像我的原始图像。谁能告诉我什么是错的?我只想检测图像的核心(圆形)特征。

我原来的形象是_________________________________________________但是我的方向图是这样的:

这就是我正在做的事情

怎么了? =(

我从这个方法得到了渐变:

/// Gradient X
 cv::Sobel(original_Mat, grad_x, CV_32FC1, 1, 0, 3);

 /// Gradient Y
 cv::Sobel(original_Mat, grad_y, CV_32FC1, 0, 1, 3);

  Mat orientation = Mat(grad_x.rows, grad_y.cols, CV_32F);

 for(int i = 0; i < grad_x.rows; i++){
    for(int j = 0; j < grad_x.cols; j++){
        // Retrieve a single value
        float valueX = grad_x.at<float>(i,j);
        float valueY = grad_x.at<float>(i,j);
        // Calculate the corresponding single direction, done by applying the arctangens function
        float result = fastAtan2(valueX,valueY);
        // Store in orientation matrix element
        orientation.at<float>(i,j) = result;

    }
    }

这是完整的代码。

 int main()
 {

cv::Mat original_Mat=cv::imread("Source.bmp", 1);

cv::Mat grad = cv::Mat::zeros(original_Mat.size(),CV_64F);

 /// Generate grad_x and grad_y
 cv::Mat grad_x = cv::Mat::zeros(original_Mat.size(), CV_64F); 
 cv::Mat grad_y = cv::Mat::zeros(original_Mat.size(), CV_64F);
 cv::Mat grad_angle = cv::Mat::zeros(original_Mat.size(), CV_64F);

 /// Gradient X
 cv::Sobel(original_Mat, grad_x, CV_32FC1, 1, 0, 3);

 /// Gradient Y
 cv::Sobel(original_Mat, grad_y, CV_32FC1, 0, 1, 3);

  Mat orientation = Mat(grad_x.rows, grad_y.cols, CV_32F); //to store the gradients
  Mat img=Mat(grad_x.rows, grad_y.cols, CV_32F);//to draw out the map
  img = cv::Scalar(255,255,255);//all white

// Calculate orientations of gradients --> in degrees
// Loop over all matrix values and calculate the accompanied orientation

 for(int i = 0; i < grad_x.rows; i++){
    for(int j = 0; j < grad_x.cols; j++){
        // Retrieve a single value
        float valueX = grad_x.at<float>(i,j);
        float valueY = grad_x.at<float>(i,j);
        // Calculate the corresponding single direction, done by applying the arctangens function
        float result = fastAtan2(valueX,valueY);
        // Store in orientation matrix element
        orientation.at<float>(i,j) = result;

    }
    }


int i=0,j=0;
int x1=0,x2=0;
float results;

for(int l=0;l<96;l++) //to loop all the rows
{
    int x1=(5+(l*5)); // to get 5x5 block sizes

for(int k=0;k<64;k++)//to loop all the columns
{

    int x2=(5+(k*5)); // to get 5x5 block sizes
     results=0;


           //to get the total of 5x5 gradient values
    for(i=(x1-5); i < x1; i++){
    for(j=(x2-5); j < x2; j++){

        results=results+orientation.at<float>(i,j);
        orientation.at<float>(i,j)=0;


    }
    }

    results=results/25; //averaging the 5x5 block gradients

    orientation.at<float>((x1-3),(x2-3))=results; //to store the results in the center of the 5x5 block

}
}
results=0;


//this loop is to draw out the orientation map
for(int i=0;i<480;i++)
{
    for(int j=0;j<320;j++)
    {
        results=orientation.at<float>(i,j);
    if ((results<=22.5)&&(results>0)){
        results=0;
        img.at<int>(i,j)=255;
        img.at<int>(i,j+1)=255;
        img.at<int>(i,j+2)=255;

    }
    else if((results>22.5)&&(results<=67.5)){
        results=45;
        img.at<int>(i,j)=255;
        img.at<int>(i-1,j+1)=255;
        img.at<int>(i-2,j+2)=255;


    }
    else if((results>67.5)&&(results<=112.5)){
        results=90;
        img.at<int>(i,j)=255;
        img.at<int>(i-1,j)=255;
        img.at<int>(i-2,j)=255;

    }
    else if((results>112.5)&&(results<=157.5)){
        results=135;
        img.at<int>(i,j)=255;
        img.at<int>(i-1,j-1)=255;
        img.at<int>(i-2,j-2)=255;

    }
    else if((results>157.5)&&(results<=202.5)){
        results=180;
        img.at<int>(i,j)=255;
        img.at<int>(i,j-1)=255;
        img.at<int>(i,j-2)=255;

    }
    else if((results>202.5)&&(results<=247.5)){
        results=225;
        img.at<int>(i,j)=255;
        img.at<int>(i+1,j-1)=255;
        img.at<int>(i+2,j-2)=255;
        endx=x2-5;
        endy=x1-1;
    }
    else if((results>247.5)&&(results<=292.5)){
        results=270;
        img.at<int>(i,j)=255;
        img.at<int>(i+1,j)=255;
        img.at<int>(i+2,j)=255;

    }
    else if((results>292.5)&&(results<=337.5)){
        results=315;
        img.at<int>(i,j)=255;
        img.at<int>(i+1,j+1)=255;
        img.at<int>(i+2,j+2)=255;

    }
    else
    {
        results=0;

    }
    orientation.at<float>(i,j)=results;


    }
}

1 个答案:

答案 0 :(得分:3)

这是我的结果: 对于图像:

enter image description here

我有结果:

enter image description here

代码:

#include <stdio.h>
#include <stdarg.h>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main(int argc, char* argv[])
{
    namedWindow("source");
    namedWindow("result");

    namedWindow("ang");

    Mat img=imread("D:\\ImagesForTest\\binarized_image.png",0);
    cv::threshold(img,img,128,255,cv::THRESH_BINARY);
    Mat thinned;

    thinned=img.clone(); // Just clone the input
    //Thinning(img,thinned); // Not actually needed

    cv::GaussianBlur(thinned,thinned,Size(3,3),1.0);
    Mat gx,gy,ang,mag;
    cv::Sobel(thinned,gx,CV_32FC1,1,0);
    cv::Sobel(thinned,gy,CV_32FC1,0,1);
    cv::phase(gx,gy,ang,false);
    cv::magnitude(gx,gy,mag);

    cv::normalize(mag,mag,0,1,cv::NORM_MINMAX);


    Mat angRes=Mat::zeros(img.rows*3,img.cols*3,CV_8UC1);

    for (int i=0;i< img.rows;i+=2)
    {
        for (int j=0;j< img.cols;j+=2)
        {
            int x=j*3;
            int y=i*3;

            float r=5;
            float m=r*(mag.at<float>(i,j));
            float dx=m*r*cos(ang.at<float>(i,j));
            float dy=m*r*sin(ang.at<float>(i,j));

            cv::line(angRes,cv::Point(x,y),cv::Point(x+dx,y+dy),Scalar::all(255),1,CV_AA);
        }
    }
    imshow("ang",angRes);
    imshow("source",img);
    imshow("result",thinned);
    cv::waitKey(0);
}

另一种变体(加权块平均值):

#include <stdio.h>
#include <stdarg.h>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
float GetWeightedAngle(Mat& mag,Mat& ang)
{
    float res=0;
    float n=0;
    for (int i=0;i< mag.rows;++i)
    {
        for (int j=0;j< mag.cols;++j)
        {
            res+=ang.at<float>(i,j)*mag.at<float>(i,j);
            n+=mag.at<float>(i,j);
        }
    }
    res/=n;
    return res;
}


int main(int argc, char* argv[])
{
    namedWindow("source");
    namedWindow("ang");

    Mat img=imread("D:\\ImagesForTest\\binarized_image.png",0);
    cv::threshold(img,img,128,255,cv::THRESH_BINARY);
    Mat thinned;

    thinned=img.clone();
    //Thinning(img,thinned);

    //cv::GaussianBlur(thinned,thinned,Size(3,3),1.0);
    Mat gx,gy,ang,mag;
    cv::Sobel(thinned,gx,CV_32FC1,1,0,7);
    cv::Sobel(thinned,gy,CV_32FC1,0,1,7);
    cv::phase(gx,gy,ang,false);
    cv::magnitude(gx,gy,mag);

    cv::normalize(mag,mag,0,1,cv::NORM_MINMAX);


    Mat angRes=Mat::zeros(img.rows,img.cols,CV_8UC1);

    int blockSize=img.cols/15-1;
    float r=blockSize;

    for (int i=0;i< img.rows-blockSize;i+= blockSize)
    {
        for (int j=0;j< img.cols-blockSize;j+= blockSize)
        {

            float a=GetWeightedAngle(mag(Rect(j,i,blockSize,blockSize)),ang(Rect(j,i,blockSize,blockSize)));

            float dx=r*cos(a);
            float dy=r*sin(a);
            int x=j;
            int y=i;            

            cv::line(angRes,cv::Point(x,y),cv::Point(x+dx,y+dy),Scalar::all(255),1,CV_AA);
        }
    }
    imshow("ang",angRes);
    imshow("source",img);
    cv::waitKey(0);
}

它给出了结果图像:

enter image description here