Ask Your Question
-1

How to Convert Mat to ArrayList<Point> Java

asked 2020-04-29 06:08:36 -0600

Nagarjuna gravatar image

updated 2020-04-29 10:48:37 -0600

Hi, I could not able to convert Mat to List<ponit> and getting pixels.Could you please help me on this.

My source code looks like:

     // we first blur the gray scale image 
        Mat blurred = new Mat(gray.size(),gray.type()); 
        Mat binary4c = new Mat(gray.size(),gray.type()); 
        Imgproc.GaussianBlur(gray,blurred,new Size(0,0),1,1); 
        // next we threshold the blurred image 
        Imgproc.threshold(blurred,binary4c,128,255,Imgproc.THRESH_BINARY);

        Mat mHierarchy = new Mat(); 
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 
        Imgproc.findContours(binary4c, contours, mHierarchy, Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
       int dpi=300;    // select dpi, 300 is optimal for OCR
    List<Point> corners=getOuterQuad(contours); // find corners from contour
    /*List<Point> corners= new ArrayList<>();
    for(int i=0;i<=contours.size();i++){
        Converters.Mat_to_vector_Point(contours.get(i), corners);
    }*/

    List<Point> corners=getOuterQuad(contours); //// find corners from contour
    Point inchesDim=????????????????????????;  // select business card dimensions and compute pixels
    float inchesWide=inchesDim.x;
    float inchesHigh=inchesDim.y;
    int pixelsWide = (int)(inchesWide*dpi); // width and height of business card at given dpi
    int pixelsHigh = (int)(inchesHigh*dpi);
    // now establish from and to parameters for warpPerspective
    Point[] fromPts = {corners.get(0),corners.get(1),corners.get(2),corners.get(3)};
    Point[] toPts = {new Point(0,0), new Point(0,pixelsHigh), new Point(pixelsWide,pixelsHigh), new 
        Point(pixelsWide,0)};
    MatOfPoint2f srcPts = new MatOfPoint2f(); srcPts.fromArray(fromPts);
    MatOfPoint2f dstPts = new MatOfPoint2f(); dstPts.fromArray(toPts);
    Mat hh=Calib3d.findHomography(srcPts,dstPts);
    Mat rectified=Mat.zeros(pixelsHigh,pixelsWide,gray.type());
    Imgproc.warpPerspective(gray,rectified, hh,rectified.size());
    // condition the output image a little
    Core.normalize(rectified,rectified,0,255,Core.NORM_MINMAX,CvType.CV_8UC1);
    float meanA=(float) Core.mean(rectified).val[0];
    if(meanA>128) {
        Core.bitwise_not(rectified,rectified);
    }

Could anyone please help on this.

edit retag flag offensive close merge delete

Comments

my downvote. read docs before asking anywhere, please

berak gravatar imageberak ( 2020-04-29 13:09:01 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-04-29 06:52:57 -0600

berak gravatar image

please take a look at the docs

contours is a List<MatOfPoint> and MatOfPoint has a toList() function

edit flag offensive delete link more

Comments

Thanks berak.I have updated code as List<point> points = new ArrayList<point>(); for (MatOfPoint point : contours) { points.addAll(point.toList()); } How can I get pixels

Nagarjuna gravatar imageNagarjuna ( 2020-04-29 10:34:00 -0600 )edit

I need the same code exactly same as https://www.linkedin.com/pulse/using-...

Nagarjuna gravatar imageNagarjuna ( 2020-04-29 10:46:02 -0600 )edit

hey, you probably do not want to add all points from all contours to the same list

berak gravatar imageberak ( 2020-04-29 13:02:31 -0600 )edit

Thanks for your suggestion,but i'm new to opencv.I could not able to.I searched many articles but it didnt helpful.I need to do PAN card text reading,but its getting nothing.Please save my day berak.

Nagarjuna gravatar imageNagarjuna ( 2020-04-29 23:37:16 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-04-29 06:08:36 -0600

Seen: 1,748 times

Last updated: Apr 29 '20