本文介绍了将OpenCv DCT转换为Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android中实现DCT代码。我正在测试使用代码,但只是改为DCT而不是DFT:。由于timegalore,代码已经发生了变化。现在我在将图像转换回BGR时遇到了问题。

I'm trying to implement a DCT code in Android. I'm testing out using the code but just changing to DCT instead of DFT : Convert OpenCv DFT example from C++ to Android. There have been changes to the code, thanks to timegalore. Now I'm having problems converting the image back to BGR.

public void transformImage(){
image = Highgui.imread(imageName, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
try {
    secondImage = new Mat(image.rows(), image.cols(), CvType.CV_64FC1);
    image.convertTo(secondImage, CvType.CV_64FC1);

    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_64FC1); // expand input image to optimal size

    Imgproc.copyMakeBorder(secondImage, padded, 0, m - secondImage.rows(), 0, n - secondImage.cols(), Imgproc.BORDER_CONSTANT);

    Mat result = new Mat(padded.size(), padded.type());
    Core.dct(padded, result);

    Mat transformedImage = new Mat(padded.size(), padded.type());
    Core.idct(result, watermarkedImage);

    completedImage = new Mat(image.rows(), image.cols(), CvType.CV_64FC1);
    Imgproc.cvtColor(transformedImage, completedImage, Imgproc.COLOR_GRAY2BGR);

} catch (Exception e) {
    Log.e("Blargh", e.toString());
}

}

现在,我已经获得了这个错误

Now, I have obtained this error

我不确定我该怎么办,请指教。非常感谢你的帮助!

I am not sure what I should do, please advise. Your help is very much appreciated!

推荐答案

你有这一行:

image.convertTo(secondImage, CvType.CV_64FC1);

但是你不再使用secondImage,只是图像。尝试:

but then you don't use secondImage again, just image. Try:

Imgproc.copyMakeBorder(secondImage, padded, 0, m - secondImage.rows(), 0, n - secondImage.cols(), Imgproc.BORDER_CONSTANT);

看看你是怎么上的。

DCT看起来它只适用于实数而不是像DFT这样复杂的数字,因此您不需要添加第二个通道来将虚部归零。你可以直接使用padded变量,所以:

Also DCT looks like it only works on reals not complex numbers like DFT and so you don't need to add a second channel to zero the imaginary part. You can work directly with the padded variable, so:

Mat result = new Mat(padded.size(), padded.type());

然后

Core.dct(padded, result);

此外,原始图像需要是单通道 - 所以是灰度。当您调用Highgui.imread时,将加载的图像是多声道 - 在我的设备上,它是BGR格式的3声道。您可以使用Imgproc.cvtColor将其转换为灰度,但首先将其加载为灰度级会更简单:

also, the original image needs to be single channel - so a greyscale. When you call Highgui.imread the image that will be loaded is multichannel - on my device it is 3 channel in BGR format. You can convert it to greyscale using Imgproc.cvtColor but it would be simpler just to load it as grey scale in the first place:

image = Highgui.imread(imageName, Highgui.CV_LOAD_IMAGE_GRAYSCALE);

这篇关于将OpenCv DCT转换为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 04:05