slee2540
slee2540

Reputation: 3

How to transform image opencv in android

I am only a android beginner for Opencv. here is the simple code to tranform image. but I have tried to do a image tansformation for few days. but I cannot figure out what is problem. I think that the part blow comment (src,dst) is wrong asertation. I need you guys help right now. (something like Android OpenCV getPerspectiveTransform and warpPerspective).

here is my logcat.

E/AndroidRuntime(9880): CvException [org.opencv.core.CvException: /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)

public class MainActivity extends Activity implements OnClickListener{

private Button btnProc; 
 private ImageView imageView; 
 private Bitmap bmp,bmp1; 

 private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { 
     @Override 
     public void onManagerConnected(int status) { 
         switch (status) { 
            case LoaderCallbackInterface.SUCCESS:{ 
            } break; 
            default:{ 
             super.onManagerConnected(status); 
             } break; 
         } 
     } 
 }; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.activity_main);

    btnProc = (Button) findViewById(R.id.btn_gray_process); 
    imageView = (ImageView) findViewById(R.id.image_view);
    btnProc.setOnClickListener(this);
}

public void onClick(View v) { 

    bmp = BitmapFactory.decodeResource(getResources(),R.drawable.a2);
    Mat original_image = new Mat();     
    Utils.bitmapToMat(bmp, original_image);     

    Mat dst = new Mat(4,1,CvType.CV_32FC2);
    Mat src = new Mat(4,1,CvType.CV_32FC2);
    src.put(0,0,407.0,74.0,1606.0,74.0,420.0,2589.0,1698.0,2589.0);
        dst.put(0,0,0.0,0.0,1600.0,0.0, 0.0,2500.0,1600.0,2500.0);      
    Mat perspectiveTransform = Imgproc.getPerspectiveTransform(src, dst);
    Mat cropped_image = original_image.clone();
    Imgproc.warpPerspective(src, cropped_image, perspectiveTransform, new Size(512,512));    
    bmp1 = Bitmap.createBitmap(400, 400, Config.RGB_565);
    Utils.matToBitmap(dst, bmp1);

    imageView.setImageBitmap(bmp1);
}

public void onResume(){ 
     super.onResume();       
     OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this,mLoaderCallback); 
} 

}

Upvotes: 0

Views: 516

Answers (1)

timegalore
timegalore

Reputation: 731

You need to be careful that the parameters to the various functions are of the right type and dimensions. For example, this call here:

   Imgproc.warpPerspective(src, cropped_image, perspectiveTransform, new Size(512,512));    

I suspect you don't really want src here as it's a 4x1 CV_32FC2. This should probably be original_image.

Also, here:

  Utils.matToBitmap(dst, bmp1);

again, dst is a 4x1 CV_32FC2. I suspect you should try cropped_image instead.

Upvotes: 0

Related Questions