gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:44:51 24 4
gpt4 key购买 nike

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

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

之后,我将所有渐变添加到 5x5 block 中,并将它们除以 25(平均),就像链接所说的那样。我将度数分成 45 度间隔的 8 个部分并绘制出来。但它看起来与我的原始图像完全不同。谁能告诉我怎么了?我只想检测图像的核心(圆形)特征。

我的原始图像是 _____________________________________________________________ 但我的方向图是这样的:

这就是我正在做的

怎么了? =(

我从这个方法得到了梯度:

/// 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++ - 使用 sobel 的奇怪方向图。怎么了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22953666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com