OpenCV_Dft.java
package Ch14;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import java.util.ArrayList;
import java.util.List;
public class OpenCV_Dft {
    static {System.loadLibrary(Core.NATIVE_LIBRARY_NAME);};
    public static void main( String[] args )
    {
        try{
            Mat source = Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\lena.jpg ",Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
            /////////////part1 optimizeImageDim
            // optimize the dimension of the loaded image
            // init
            Mat padded=new Mat();
            // get the optimal rows size for dft
            int addPixelRows = Core.getOptimalDFTSize(source.rows());
            // get the optimal cols size for dft
            int addPixelCols = Core.getOptimalDFTSize(source.cols());
            // apply the optimal cols and rows size to the image
            //The calculation speed is the best when the image-like scale is multiplied by 2, 3, and 5. Rapid calculation,
            //將 Imported image Kaseki exhibition arrival The best scale,0 for additive image element initialization
            Core.copyMakeBorder(source, padded, 0, addPixelRows - source.rows(), 0, addPixelCols - source.cols(),1, Scalar.all(0));
            // return padded;
            //end part1 optimizeImageDim
            //System.out.println(padded.dump());
            //main
            //Distribution Tachibana Convertible Minabe Wakabe Profitable Space 兩 Individual 圖 Statue 值.Fourier transform result, factor, frequency domain, float type, average number, total amount, outside passage, frequency domain, total number:
            padded.convertTo(padded, CvType.CV_32F);
            List<Mat> planes = new ArrayList<Mat>();
            Mat complexImage=new Mat();
            // prepare the image planes to obtain the complex image
            planes.add(padded);
            planes.add(Mat.zeros(padded.size(), CvType.CV_32F));
            // prepare a complex image for performing the dft
            ////Post-exhibition of the image of the sword
            Core.merge(planes, complexImage);
            // dft
            //Progressive discrete Fourier transform,Conversion result exists Primitive Mat Matrix
            Core.dft(complexImage, complexImage);
            //Mat magnitude = new Mat();
            // optimize the image resulting from the dft operation
            /// part2 createOptimizedMagnitude
            // init
            List<Mat> newPlanes = new ArrayList<Mat>();
            Mat mag = new Mat();
            // split the comples image in two planes
            //Radian for multiple conversion.
            Core.split(complexImage, newPlanes);
            // compute the magnitude
            Core.magnitude(newPlanes.get(0), newPlanes.get(1), mag);
            // move to a logarithmic scale
            //Logarithmic scale
            Core.add(mag, Scalar.all(1), mag);
            Core.log(mag, mag);
            // optionally reorder the 4 quadrants of the magnitude image
            ////////////////part3 shiftDFT
            //Cropping odd number line
            mag = mag.submat(new Rect(0, 0, mag.cols() & -2, mag.rows() & -2));
            //Heavy-tailed distribution Standing leaf width 4 quadrants
            int cx = mag.cols() / 2;
            int cy = mag.rows() / 2;
            //Upper left corner-ROI for building one quadrant
            Mat q0 = new Mat(mag, new Rect(0, 0, cx, cy));
            //Upper right corner
            Mat q1 = new Mat(mag, new Rect(cx, 0, cx, cy));
            //Lower left corner
            Mat q2 = new Mat(mag, new Rect(0, cy, cx, cy));
            //Lower right corner
            Mat q3 = new Mat(mag, new Rect(cx, cy, cx, cy));
            Mat tmp = new Mat();
            q0.copyTo(tmp);
            q3.copyTo(q0);
            //Exchange quadrant:Upper left 與 lower right
            tmp.copyTo(q3);
            q1.copyTo(tmp);
            q2.copyTo(q1);
            //Exchange quadrant:Upper right, lower left
            tmp.copyTo(q2);
            //end part3 shiftDFT
            // normalize the magnitude image for the visualization since both JavaFX
            // and OpenCV need images with value between 0 and 255
            //Normalization[0,255]range
            Core.normalize(mag, mag, 0,255, Core.NORM_MINMAX);
            //end part2 createOptimizedMagnitude
            //Imgproc.GaussianBlur(source1, processBlur,new Size(GaussianKernelSize,GaussianKernelSize),0,0);
            //Mat lastoutput = ConvertDFTToImage(Dft,gaussianFilter,output);
            //source.convertTo(destination, -1, alpha, beta);
            Imgcodecs.imwrite("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\lena-dft.jpg ", mag);
            //main end
        }catch (Exception e) {
            System.out.println("error: " + e.getMessage());
        }
    }
}

Recommended Posts