I am trying to implement a DCT code in Android. I'm using the split and merge method to obtain the original colors of the image.
Here is the snippet of the code
image = Highgui.imread(imageName);
secondImage = new Mat(image.rows(), image.cols(), CvType.CV_32FC3);
image.convertTo(secondImage, CvType.CV_32FC3);
int m = Core.getOptimalDFTSize(image.rows());
int n = Core.getOptimalDFTSize(image.cols()); // on the border add zero values
Mat padded = new Mat(new Size(n, m), CvType.CV_32FC3); // expand input image to optimal size
Imgproc.copyMakeBorder(secondImage, padded, 0, m - secondImage.rows(), 0, n - secondImage.cols(), Imgproc.BORDER_CONSTANT);
List planes = new ArrayList();
Core.split(padded, planes);
List outplanes = new ArrayList(planes.size());
for (int k = 0; k < planes.size(); k++) {
outplanes.add(new Mat(padded.size(), CvType.CV_32FC1));
}
Mat trans = new Mat(padded.size(), padded.type());
for (int k = 0; k < planes.size(); k++) {
Core.dct(planes.get(k), outplanes.get(k));
}
List ioutplanes = new ArrayList(outplanes.size());
for (int k = 0; k < planes.size(); k++) {
ioutplanes.add(new Mat(padded.size(), CvType.CV_32FC1));
}
for (int k = 0; k < planes.size(); k++) {
Core.idct(outplanes.get(k), ioutplanes.get(k));
}
Core.merge(ioutplanes, trans);
This time got this exception :
The function/feature is not implemented (Odd-size DCT's are not implemented) in void cv::dct(cv::InputArray, cv::OutputArray, int), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/core/src/dxt.cpp, line 2330
Is this code correct? Your help is very much appreciated.
Updated: I noticed that the copyMakeBorder() is the one that caused my image size to be odd, that is why I am having this exception.
解决方案
Maybe you can change this code
int m = Core.getOptimalDFTSize(image.rows());
int n = Core.getOptimalDFTSize(image.cols());
to
int m =Core.getOptimalDFTSize((image.rows()+1)/2)*2;
int n = Core.getOptimalDFTSize((image.cols()+1)/2)*2;
in order to get the odd size for DCT, since this function is for DFT.
It is written in the function documentation from the website as this:
While the function cannot be used directly to estimate the optimal vector size for DCT transform (since the current DCT implementation supports only even-size vectors), it can be easily processed as getOptimalDFTSize((vecsize+1)/2)*2.