[JAVA] Desktop: OpenCV Dft

Goal
Test OpenCV Dft.

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

Result
![lena.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/697b070e-ff11-4ec5-490a-b30913b26a69.jpeg)

lena-dft.jpg

Recommended Posts

Desktop: OpenCV Dft
Desktop: OpenCV Threshold
Desktop: OpenCV BilateralFilterBlur
Desktop: OpenCV Dilate
Desktop: OpenCV Expand
Desktop: OpenCV Affine
Desktop: OpenCV Emboss
Desktop: OpenCV Ellipse2Poly
Desktop: OpenCV HDR
Desktop: OpenCV Polylines
Desktop: OpenCV Sharpness
Desktop: OpenCV Concat
Desktop: OpenCV OpenCV_SalonUseBlurAddWeighted
Desktop: OpenCV Mosaic
Desktop: OpenCV Erode
Desktop: OpenCV Denoise
Desktop: OpenCV Rectangle
Desktop: OpenCV Watershed
Desktop: OpenCV Text
Desktop: OpenCV Inpaint
Desktop: OpenCV NormalizeBlur
Desktop: OpenCV Canny
Desktop: OpenCV Denoise3
Desktop: OpenCV Histogram
Desktop: OpenCV Decolor
Desktop: OpenCV FaceDetector
Desktop: OpenCV Denoise2
Desktop: OpenCV StereoBM
Desktop: OpenCV Kirsch Filter
Desktop: OpenCV Laplacian Filter 2
Desktop: OpenCV Illumination Change
Desktop: OpenCV Add WaterMark
Desktop: OpenCV Fill ConvexPoly
Desktop: OpenCV Grab Cut
Desktop: OpenCV Sharpness Gui
Desktop: OpenCV Color Change
Desktop: OpenCV Freichennel Filter
Desktop: OpenCV Adaptive Threshold
Desktop: OpenCV Fill Poly
Desktop: OpenCV Mean Filter
Desktop: OpenCV Java Repository
Desktop: OpenCV Sobel Filter2
Desktop: OpenCV pyrMeanShift Filter
Desktop: OpenCV OpticalFlow PyrLK
Desktop: OpenCV Virtual Piano
Desktop: OpenCV merge Picture
Desktop: OpenCV Scharr Filter
Desktop: Opencv webcam preview
Desktop: OpenCV Laplacian Filter
Desktop: OpenCV Median Blur
Desktop: OpenCV Add Broad
Desktop: OpenCV record video
Desktop: OpenCV Flood Fill
Desktop: OpenCV color space
Desktop: OpenCV SqrBox Filter
Desktop: OpenCV resize image
Desktop: OpenCV Pencil Sketch
Desktop: OpenCV Color Map
Desktop: OpenCV Seamless Clone
Desktop: OpenCV Draw Point
Desktop: OpenCV Sobel Filter