Reputation: 51
I am developing an application in whitch i have to take a picture from camera and after that Detect the Edge Square(Like Document page.) in the picture...After a long searching i found OpenCV library to achieve this, i have succesfully imported the java library for android, But problem is that when i call the method of opencv to detect Square(The method is
Imgproc.findContours(converted, contours,hierarchy,Imgproc.CHAIN_APPROX_SIMPLE,Imgproc.RETR_LIST))..it give me the
exception...OpenCV Error: Unsupported format or combination of formats (FindContours support only 8uC1 and 32sC1 images) in _CvContourScanner* cvStartFindContours(void*, CvMemStorage*, int, int, int, CvPoint), file /home/reports/ci/slave/50-SDK/opencv/modules/imgproc/src/contours.cpp, line 196
I am sending u some piece of code-----------------------------------------------------
public void convertImage() {
Mat ori = new Mat();
Mat converted = new Mat(200, 200, CvType.CV_8UC1, new Scalar(0));
try {
ori = Utils.loadResource(MainActivity.this, R.drawable.ic_launcher, Highgui.CV_LOAD_IMAGE_COLOR);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Imgproc.cvtColor(ori, converted, Imgproc.COLOR_RGB2GRAY, 4); // convert Image to grayscale
Imgproc.threshold(ori, converted, 50, 250, Imgproc.ADAPTIVE_THRESH_MEAN_C); // threshold the image
List<MatOfPoint> contours = new ArrayList<MatOfPoint>(10);
Mat hierarchy = new Mat(200, 200, CvType.CV_32FC1, new Scalar(0));
Imgproc.findContours(converted, contours, hierarchy,Imgproc.CHAIN_APPROX_SIMPLE,Imgproc.RETR_LIST);
ImageView frame = (ImageView) findViewById(R.id.imageView1);
Imgproc.cvtColor(converted, converted, Imgproc.COLOR_GRAY2RGBA, 4); // convert Image back to RGB
Bitmap bmp = Bitmap.createBitmap(converted.cols(), converted.rows(), Bitmap.Config.ARGB_8888);
frame.setImageBitmap(bmp);
}
Any Help will be apreciated------------------ Thanks in advance
Upvotes: 0
Views: 15290
Reputation: 41
In 'cvFindCounters' The first argument is the input image; this image should be an 8-bit single-channel image and will be interpreted as binary. so Instead of passing a 4 channel image , you should be passing a single channel image.
this should work for you.
Imgproc.cvtColor(ori, converted, Imgproc.COLOR_RGB2GRAY, 1);
Upvotes: 4
Reputation: 141
First load the bitmap as -
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_launcher);
Then convert the bitmap to Mat -
Mat m = new Mat();
Utils.bitmapToMat(icon, m);
Imgproc.cvtColor(m, m, Imgproc.COLOR_RGB2GRAY, 1);
Perform your threshold and findcontours as above..basic idea is to convert Bitmap to Mat which is single channel..
Upvotes: 0
Reputation: 1055
Try to use
Imgproc.cvtColor(ori, converted, Imgproc.COLOR_RGB2GRAY, 1);
Imgproc.cvtColor(converted, converted, Imgproc.COLOR_GRAY2RGBA, 1);
instead of
Imgproc.cvtColor(ori, converted, Imgproc.COLOR_RGB2GRAY, 4);
Imgproc.cvtColor(converted, converted, Imgproc.COLOR_GRAY2RGBA, 4);
Because you need to use 1 channel.
Upvotes: 0