我需要从sim返回一个数字以显示差异。我之前在python中编写过代码并成功执行。
(score, diff) = compare_ssim(img1, img2, full=True, multichannel=True)
diff = (diff * 255).astype("uint8")
“ diff”可以用plt.show()
我编写了自己的ssim方法,并尝试返回位图,但未成功。
private Bitmap SSIM_map(Mat img1_temp , Mat img2_temp ){
double C1 = 6.5025, C2 = 58.5225;
Size size = new Size(img1_temp.width(), img1_temp.height());
int d = CV_32FC4;
Mat img1 = new Mat(size, d);
Mat img2 = new Mat(size, d);
img1_temp.convertTo(img1, d);
img2_temp.convertTo(img2, d);
Mat img1_sq = new Mat(size, d);
Mat img2_sq = new Mat(size, d);
Mat img1_img2 = new Mat(size, d);
Mat mu1 = new Mat(size, d);
Mat mu2 = new Mat(size, d);
Mat mu1_sq = new Mat(size, d);
Mat mu2_sq = new Mat(size, d);
Mat mu1_mu2 = new Mat(size, d);
Mat sigma1_sq = new Mat(size, d);
Mat sigma2_sq = new Mat(size, d);
Mat sigma12 = new Mat(size, d);
Mat temp1 = new Mat(size, d);
Mat temp2 = new Mat(size, d);
Mat temp3 = new Mat(size, d);
Mat ssim_map = new Mat(size, d);
//*************************** END INITS **********************************//
//////////////////////////////////////////////////////////////////////////
// PRELIMINARY COMPUTING
GaussianBlur(img1, mu1, new Size(11, 11), 1.5);
GaussianBlur(img2, mu2, new Size(11, 11), 1.5);
Core.pow(mu1, 2, mu1_sq);
Core.pow(mu2, 2, mu2_sq);
mu1_mu2 = mu1.mul(mu2);
GaussianBlur(img1_sq, sigma1_sq, new Size(11, 11), 1.5 );
Core.addWeighted(sigma1_sq, 1, mu1_sq, -1, 0, sigma1_sq);
GaussianBlur(img2_sq, sigma2_sq, new Size(11, 11), 1.5 );
Core.addWeighted(sigma2_sq, 1, mu2_sq, -1, 0, sigma2_sq );
GaussianBlur(img1_img2, sigma12, new Size(11, 11), 1.5 );
Core.addWeighted(sigma12, 1, mu1_mu2, -1, 0, sigma12 );
///////////////////////////////// FORMULA ////////////////////////////////
Scalar scalar = new Scalar(C1);
Core.multiply(mu1_mu2, new Scalar(2,2,2,2), temp1);
Core.add(temp1, scalar, temp1);
//t2 = 2 * sigma12 + C2;
Core.multiply(sigma12, new Scalar(2,2,2,2), temp2);
Core.add(temp2, new Scalar(C2), temp2);
temp3 = temp1.mul(temp2); // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
//t1 = mu1_2 + mu2_2 + C1;
Core.add(mu1_sq, mu2_sq, temp1);
Core.add(temp1, new Scalar(C1), temp1);
//t2 = sigma1_2 + sigma2_2 + C2;
Core.add(sigma1_sq, sigma2_sq, temp2);
Core.add(temp2, new Scalar(C2),temp2);
temp1 = temp1.mul(temp2); // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))
Core.divide(temp3, temp1, ssim_map); // ssim_map = t3./t1;
Bitmap bitmap = Bitmap.createBitmap(ssim_map.width(), ssim_map.height(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(ssim_map, bitmap);
return bitmap;
}
下面显示错误消息
E/cv::error(): OpenCV(3.4.3) Error: Assertion failed (src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean), file /build/3_4_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp, line 102
E/org.opencv.android.Utils: nMatToBitmap caught cv::Exception: OpenCV(3.4.3) /build/3_4_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp:102: error: (-215:Assertion failed) src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4 in function 'void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)'
有人可以帮助我解决问题吗?