本文主要使用DFT相关函数实现对水平文本和旋转文本的DFT变换,在幅度谱中识别文本的变换,从而为图像旋转的检测和校正做准备。
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include using namespace cv;
using namespace std;void help(char* progName)
{cout <}int main(int argc, char ** argv)
{help(argv[0]);const char* filename &#61; argc >&#61;2 ? argv[1] : "lena.jpg";/* Mat I &#61; imread(filename, CV_LOAD_IMAGE_GRAYSCALE);*/Mat I &#61; imread(filename, 0);if( I.empty()){cout<<"Can&#39;t load image!"<(padded), Mat::zeros(padded.size(), CV_32F)};Mat complexI;merge(planes, 2, complexI);//把0值添加到另一个扩充的平面//这样处理的结果可以适合原来的矩阵dft(complexI, complexI);//计算这个幅度并转换到log领域//log(1 &#43; sqrt(Re(DFT(I))^2 &#43; Im(DFT(I))^2))//planes[0] &#61; Re(DFT(I)&#xff09;实部, planes[1] &#61; Im(DFT(I))虚部split(complexI, planes); //planes[0] &#61; magnitude幅度值magnitude(planes[0], planes[1], planes[0]);Mat magI &#61; planes[0];//转换到log运算magI &#43;&#61; Scalar::all(1); log(magI, magI);//如果它由奇数个行或奇数个列&#xff0c;截取频谱magI &#61; magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));//重新分配傅里叶变换后图像的象限从而让图像原始(0&#xff0c;0)位置在图像中心int cx &#61; magI.cols/2;int cy &#61; magI.rows/2;Mat q0(magI, Rect(0, 0, cx, cy)); // Top-Left -每个象限创建一个感兴趣区域Mat q1(magI, Rect(cx, 0, cx, cy)); // Top-RightMat q2(magI, Rect(0, cy, cx, cy)); // Bottom-LeftMat q3(magI, Rect(cx, cy, cx, cy)); // Bottom-Right//交换Ⅱ和Ⅳ象限位置(Top-Left with Bottom-Right)Mat tmp; q0.copyTo(tmp);q3.copyTo(q0);tmp.copyTo(q3);//交换Ⅰ和Ⅲ象限位置(Top-Right with Bottom-Left)q1.copyTo(tmp); q2.copyTo(q1);tmp.copyTo(q2);// Transform the matrix with float values into a// viewable image form (float between values 0 and 1).normalize(magI, magI, 0, 1, CV_MINMAX); imshow("Input Image" , I ); imshow("spectrum magnitude", magI);waitKey();return 0;
}