简体   繁体   中英

How to do PCA Image Compression in JavaCV / OpenCV

I am tyring to do image compression in javacv /opencv.

Firts the code:

CanvasFrame canvasOrig = new CanvasFrame("Original");
canvasOrig.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

CanvasFrame canvasOrig = new CanvasFrame("Original");
canvasOrig.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

CanvasFrame canvasGray = new CanvasFrame("Gray");
canvasGray.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

CanvasFrame canvasPCA = new CanvasFrame("PCA");
canvasPCA.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

OpenCVFrameConverter.ToIplImage t = new OpenCVFrameConverter.ToIplImage();
IplImage image = cvLoadImage("640x480.jpg");

Mat imageOriginal = t.convertToMat(t.convert(image));   
Mat imageGray = new Mat();
cvtColor(imageOriginal, imageGray, CV_RGB2GRAY);
System.out.println("ArraySize-Original-Mat: "+imageGray.arraySize());

PCA pca = new PCA(imageGray,new Mat(), CV_PCA_DATA_AS_ROW, 48);
Mat eigValues = pca.eigenvalues().clone();
Mat eigVectors = pca.eigenvectors().clone();

Mat projectedMat = new Mat();
pca.project(imageGray, projectedMat);

During PCA calculating the process and the methods are bahaving like I would expect.

My Question is:

How could I compress the original Image from the results of the PCA?

After the projection I get a Mat (projectedMat) with the number of given components and the height of the image. After backProjection the ArraySize is much higher than the original Image. That is not what I did expect.

Thanks

I found the answer for myself.

The problem or in this context the solutions was to convert the image to type 32F oder 64F for floating point operations.

After calculationg pca the image or even matrix has to be converted back to type 8 bit numbers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM