Quantcast
Channel: OpenCV Q&A Forum - Latest question feed
Viewing all articles
Browse latest Browse all 600

I need help for image classification

$
0
0
I have been using Discrete Fourier Transform for preprocessing method. Now for classification, I need to take Discrete Fourier Transform,assign label to DFT and then train KNN for the training data.Then,for predicting data,i need to find k-nearest neighbours of test data and finally perform matching(euclidean).How to proceed with it using KNN now? Below is my c++ code for DFT using namespace std; using namespace cv; void takeDFT(Mat&source, Mat &destination) { Mat dftReady; Mat originalComplex[2] = { source,Mat::zeros(source.size(),CV_32F) }; merge(originalComplex, 2, dftReady); Mat dftOriginal; dft(dftReady, dftOriginal, DFT_COMPLEX_OUTPUT); destination = dftOriginal; } void recenterDFT(Mat &source) { // rearrange the quadrants of Fourier image int centerX = source.cols / 2; int centerY = source.rows / 2; Mat q1(source, Rect(0, 0, centerX, centerY)); Mat q2(source, Rect(centerX, 0, centerX, centerY)); Mat q3(source, Rect(0, centerY, centerX, centerY)); Mat q4(source, Rect(centerX, centerY, centerX, centerY)); Mat swapMap; q1.copyTo(swapMap); q4.copyTo(q1); swapMap.copyTo(q4); q2.copyTo(swapMap); q3.copyTo(q2); swapMap.copyTo(q3); } void showDFT(Mat& source) { Mat splitArray[2] = { Mat::zeros(source.size(),CV_32F),Mat::zeros(source.size(),CV_32F)}; split(source, splitArray); Mat dftMagnitude; magnitude(splitArray[0],splitArray[1],dftMagnitude); dftMagnitude += Scalar::all(1); // switch to logarithmic scale log(dftMagnitude,dftMagnitude); normalize(dftMagnitude,dftMagnitude,0,1,CV_MINMAX); recenterDFT(dftMagnitude); imshow("DFT", dftMagnitude); waitKey(); } void invertDFT(Mat& source,Mat&destination) { Mat inverse; dft(source,inverse,DFT_INVERSE | DFT_REAL_OUTPUT | DFT_SCALE); destination = inverse; } int main() { Mat img = imread("canny.jpg", 0); Mat dftInput1; img.convertTo(dftInput1, CV_32FC1,1.0/255.0); Mat dftOriginal; takeDFT(dftInput1, dftOriginal); showDFT(dftOriginal); Mat invertedDFT; invertDFT(dftOriginal, invertedDFT); imshow("invertDFT Result", invertedDFT); imwrite("DFTimg.jpg",invertedDFT); waitKey(); } My DFT code is working but not being able to proceed using KNN.

Viewing all articles
Browse latest Browse all 600

Latest Images

Trending Articles



Latest Images