local standard deviation
hi, here i have tried to get the local standard deviation of image using 5x5 block. but i dont know where my code is crashing. according to me there is some dimension error of loop. can any one tell me what exactly wrong i am doing here.
below is my code :-
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
String photoPath = "/storage/emulated/0/Download/std.bmp";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
width = bitmap.getWidth();
height = bitmap.getHeight();
//Bitmap tmp = null;
processing(bitmap);
setContentView(R.layout.activity_main);
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
public String processing(Bitmap t1) {
Mat rgb = new Mat (height, width, CvType.CV_8U, new Scalar(4));
Mat gry = new Mat (height, width, CvType.CV_8U, new Scalar(4));
Mat imfilter = new Mat (height, width, CvType.CV_8U, new Scalar(4));
Mat res = new Mat (height, width, CvType.CV_8U, new Scalar(4));
Mat block = new Mat (5, 5, CvType.CV_8U);
Bitmap bmp = t1.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bmp, rgb);
Imgproc.cvtColor(rgb, gry, Imgproc.COLOR_RGB2GRAY);
int rows = gry.rows(); int cols = gry.cols();
Mat tmp = new Mat(rows, cols, gry.type());
Imgproc.copyMakeBorder(tmp, tmp, 1, 1, 1, 1, 1);
rows = tmp.rows();cols=tmp.cols();
rows = rows-1;cols = cols-1;
for(int i=2;i<rows-2;i++)
{
for(int j=2;j<cols-2;j++)
{
int sum1=0;
int sum2=0;
block = tmp.rowRange(i-2,i+3).colRange(j-2,j+3);
sum1 = (int) Core.sumElems(block).val[0];
sum2 = (int) Core.sumElems(block.mul(block)).val[0];
int mean = sum1/25;
int variance = -(mean*mean -(sum2/25));
int deviation = (int) Math.sqrt(variance);
tmp.put(i-2, j-2, deviation);
}
}
Bitmap scale = Bitmap.createBitmap(gry.cols(), gry.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(tmp, scale);
Bitmap std = Bitmap.createBitmap(tmp.cols(), tmp.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(imfilter, std);
String filename1 = "/storage/emulated/0/Download/stdfilt.jpg";
boolean b1 = Highgui.imwrite(filename1, tmp);
if (b1==true)
Toast.makeText(getApplicationContext(),"Image Saved",Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(),"Not Saved",Toast.LENGTH_LONG).show();
return null;
}
Here is its logcat :-
10-29 23:11:06.346: E/cv::error()(31569): OpenCV Error: Assertion failed (src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv, jclass, jlong, jobject, jboolean), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp, line 97 10-29 23:11:06.346: E/org.opencv.android.Utils(31569): nMatToBitmap catched cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv, jclass, jlong, jobject, jboolean) 10-29 23:11:06.346: W/dalvikvm(31569): threadid=1: thread exiting with uncaught exception (group=0x41bb3798) 10-29 23:11:06.356: E/AndroidRuntime(31569): FATAL EXCEPTION: main 10-29 23:11 ...