c++ - 使用索贝尔的奇怪方向图。怎么了?

标签 c++ image opencv orientation fingerprint

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

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

之后,我将 5x5 block 中的所有渐变相加,并将它们除以 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;


    }
}

最佳答案

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

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);
}

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

#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

关于c++ - 使用索贝尔的奇怪方向图。怎么了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22953666/

相关文章:

c++ - 使用并行构建时 PRE_TARGETDEPS 失败

php - 如何在 Laravel Blade 模板中显示上传的图像?

python - 解释 cv2.imencode 结果

c++ - 我可以使用覆盖非虚拟方法的方法吗?

C++使用多个定界符分解字符串

c++ - 为什么会有函数隐式转换为 boolean 值?

c# - 从 PersistentDataPath 加载图像

wordpress - Img 扩展到父 Div 之外

opencv - haarCascades可用于获取对象大小吗?

opencv - 由于 FFmpeg,如何解决 Ubuntu 11.10 上的 OpenCV 2.3.1 编译错误