drawContours around detected document using opencv

2020-06-29 02:20发布

问题:

I am new to OpenCv4Android. I am trying to auto detect document using OpenCv4Android sdk. Initially i have gone through issue of landscape opencv camera . Somehow i managed to change the orientation of opencv JavaCameraview to portrait. I made following changes in default classes of opencv sdk to orient opencv camera in portrait :

1) In CameraBridgeViewBase class

 Matrix matrix = new Matrix();
 matrix.setRotate(90f);
 Bitmap bitmap = Bitmap.createBitmap(mCacheBitmap, 0, 0, mCacheBitmap.getWidth(), mCacheBitmap.getHeight(), matrix, true);

2) now in drawbitmap method replace above bitmap with mCacheBitmap

3) now , In JavaCameraView class

if ((getLayoutParams().width == ActionBar.LayoutParams.MATCH_PARENT) && (getLayoutParams().height == ActionBar.LayoutParams.MATCH_PARENT))
       mScale = Math.min(((float)height)/mFrameWidth, ((float)width)/mFrameHeight);
else
       mScale = 0;

After above changes , i am able to orient camera in portrait mode . But , having strange issue in detecting document . See below images

As you can see in first image , before customization i can detect paper in full green color . And in 2nd image you can see the bug of drawing line (drawContours) around paper

what i follow to detect paper is : GaussianBlur -> Canny edge detection -> findContours -> drawContour

In OnCameraFrame method :

     mRgba = inputFrame.rgba();
   Mat mGray = new Mat();
   Mat edged = new Mat();       
  Imgproc.cvtColor(mRgba,mGray,Imgproc.COLOR_BGR2GRAY);
  Imgproc.GaussianBlur(mRgba,mGray,new Size(5,5),0);           

-> and then finding contours

Imgproc.findContours(edged, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

-> Then i found the largest contour and finally the drawContours on that

Imgproc.drawContours(mRgba, contours, maxI, new Scalar(0, 255, 0), 5);

I don't know where i m wrong..! Please help me to solve this strange issue.

@Rick M. following is the original image :

@ilke444 , following is the image after edge detection which is perfectly what i want :