Skip to content

Instantly share code, notes, and snippets.

@rhzs
Created October 10, 2015 16:04
Show Gist options
  • Save rhzs/70b21bd2b0c266791773 to your computer and use it in GitHub Desktop.
Save rhzs/70b21bd2b0c266791773 to your computer and use it in GitHub Desktop.
Convert image in android to DCT image using OpenCV and JavaCV
// Author: Rheza Satria (c) 2015
// Please mention this gist if you inspired/used it!
public static Bitmap applyDCT(Bitmap image, int blockSize) {
int _w = image.getWidth();
int _h = image.getHeight();
// Preload the opencv_objdetect module to work around a known bug.
Loader.load(opencv_objdetect.class);
AndroidFrameConverter afc = new AndroidFrameConverter();
Frame frame = afc.convert(image);
OpenCVFrameConverter.ToMat converterToMat = new OpenCVFrameConverter.ToMat();
Mat img = converterToMat.convert(frame);
Mat img2 = new Mat(_h, _w, CV_32F);
cvtColor(img, img2, CV_RGB2GRAY);
Mat img3 = new Mat(_h, _w, CV_32F);
img2.convertTo(img3, CV_32F);
Mat freq = new Mat(_h, _w, CV_32F);
dct(img3, freq);
Mat img4 = new Mat(_h, _w, img.type());
freq.convertTo(img4, img.type());
Frame freqFrame = converterToMat.convert(img4);
return afc.convert(freqFrame);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment