Wiener Filter OpenCV (Java)
I am trying to design a Wiener Filter by using android studio,code is following
public void WienerFilter(View v){
/*
F0=fftshift(fft2(imt)); //make fft to to nosie image
LA = fftshift(fft2(ima)); //make fft to original image
K=0.1; // add constant to degradation function
for u=1:m
for v=1:n
H(u,v) = F0(u,v)/LA(u,v); //get degradation funtion
// Compute wiener filter function
H0(u,v)=(abs(H(u,v)))^2;
H1(u,v)=H0(u,v)/(H(u,v)*(H0(u,v)+K));
F2(u,v)=H1(u,v)*F0(u,v);
*/
Mat GrayImg = new Mat();
Utils.bitmapToMat(grayBitmap, GrayImg);
Mat noiseImg = new Mat();
Utils.bitmapToMat(noiseBitmap, noiseImg);
Core.dft(GrayImg,GrayImg);
Core.dft(noiseImg,noiseImg);
Mat dst = new Mat();
Core.divide(GrayImg,noiseImg,dst);
Mat dst1 = new Mat();
Core.convertScaleAbs(dst,dst1);
Core.multiply(dst1,dst1,dst1);
Mat dst2 =new Mat();
Core.addWeighted(dst1,1,null,0,0.5,dst2);
Core.multiply(dst,dst2,dst2);
Core.divide(dst1,dst2,dst2);
Core.multiply(dst2,GrayImg,dst2);
Core.idft(dst2,dst2);
Core.convertScaleAbs(dst2,dst2);
Core.normalize(dst2,dst2,0,255,Core.NORM_MINMAX);
dst2.convertTo(dst2,CvType.CV_8UC1);
Utils.matToBitmap(dst2,WienerBitmap);
ivImage.setImageBitmap(WienerBitmap);
}
But it doesn't work anyway, I can fix the problem shows below: 2019-01-30 21:42:13.046 25200-25200/com.addict.androidcamera E/cv::error(): OpenCV(3.4.1) Error: Assertion failed (type == (((5) & ((1 << 3) - 1)) + (((1)-1) << 3)) || type == (((5) & ((1 << 3) - 1)) + (((2)-1) << 3)) || type == (((6) & ((1 << 3) - 1)) + (((1)-1) << 3)) || type == (((6) & ((1 << 3) - 1)) + (((2)-1) << 3))) in void cv::dft(cv::InputArray, cv::OutputArray, int, int), file /build/master_pack-android/opencv/modules/core/src/dxt.cpp, line 3335 2019-01-30 21:42:13.047 25200-25200/com.addict.androidcamera E/org.opencv.core: core::dft_11() caught cv::Exception: OpenCV(3.4.1) /build/master_pack-android/opencv/modules/core/src/dxt.cpp:3335: error: (-215) type == (((5) & ((1 << 3) - 1)) + (((1)-1) << 3)) || type == (((5) & ((1 << 3) - 1)) + (((2)-1) << 3)) || type == (((6) & ((1 << 3) - 1)) + (((1)-1) << 3)) || type == (((6) & ((1 << 3) - 1)) + (((2)-1) << 3)) in function void cv::dft(cv::InputArray, cv::OutputArray, int, int)
confusing error msg, -- but you need
float
input fordct()
, like:also, reconsider the usage of convertScaleAbs(), as it will convert anything to CV_8U, which might mean loss of accuracy in some places