Ask Your Question

Hardik Patel's profile - activity

2020-05-18 18:31:46 -0600 received badge  Famous Question (source)
2020-04-28 02:41:09 -0600 received badge  Famous Question (source)
2018-04-20 01:28:08 -0600 received badge  Notable Question (source)
2017-12-31 18:42:46 -0600 received badge  Notable Question (source)
2017-10-03 07:57:55 -0600 received badge  Popular Question (source)
2017-10-02 18:41:56 -0600 received badge  Notable Question (source)
2017-08-10 22:33:55 -0600 received badge  Popular Question (source)
2017-05-31 04:34:01 -0600 received badge  Popular Question (source)
2016-07-12 02:14:57 -0600 answered a question Crop rectangle and apply transformation from image?

Yes @Missing i already use approxPolyDP() in my code

  Imgproc.findContours(gray, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
        int i=0;
        for (MatOfPoint contour : contours) {
            MatOfPoint2f contourFloat = GeomUtils.toMatOfPointFloat(contour);
            double arcLen = Imgproc.arcLength(contourFloat, true) * 0.02;

            // Approximate polygonal curves.
            MatOfPoint2f approx = new MatOfPoint2f();
            Imgproc.approxPolyDP(contourFloat, approx, arcLen, true);

            if (isRectangle(approx, srcArea)) {
                Imgproc.drawContours(src, contours, i, new Scalar(255, 0, 0), 3);
            }
        }

  private boolean isRectangle(MatOfPoint2f polygon, int srcArea) {
    MatOfPoint polygonInt = GeomUtils.toMatOfPointInt(polygon);

    if (polygon.rows() != 4) {
        return false;
    }

    double area = Math.abs(Imgproc.contourArea(polygon));
    if (area < srcArea * areaLowerThresholdRatio || area > srcArea * areaUpperThresholdRatio) {
        return false;
    }

    if (!Imgproc.isContourConvex(polygonInt)) {
        return false;
    }

    // Check if the all angles are more than 72.54 degrees (cos 0.3).
    double maxCosine = 0;
    Point[] approxPoints = polygon.toArray();

    for (int i = 2; i < 5; i++) {
        double cosine = Math.abs(GeomUtils.angle(approxPoints[i % 4], approxPoints[i - 2], approxPoints[i - 1]));
        maxCosine = Math.max(cosine, maxCosine);
    }

    if (maxCosine >= 0.3) {
        return false;
    }
    return true;
}

Do you have any other working code then please tell me

Thanks

2016-07-12 00:15:28 -0600 commented answer Crop rectangle and apply transformation from image?

Thanks @Missing for help. but i need code in java.and just tell me one thing that how to check biggest contour is rectangle? as you know in my code,largest contour is like Sample image so if i able to check this contour is rectangle or not then i filter the result.. thanks

2016-07-11 05:48:29 -0600 commented question Crop rectangle and apply transformation from image?

so just tell me that how to check which contour contain sheet or rectangle?

2016-07-11 05:31:28 -0600 commented question Crop rectangle and apply transformation from image?

i can't get gray mat because there is loop for Find squares in every color plane of the image and inner loop for different threshold values. its find contours from 15 different mat with different color and threshold value

2016-07-11 02:58:41 -0600 commented question Crop rectangle and apply transformation from image?

Means original image? this is original image http://i.stack.imgur.com/ojhGb.jpg. and my code is mentioned in my question

2016-07-11 00:46:19 -0600 commented question Crop rectangle and apply transformation from image?

Hello @Missing.please help me

2016-07-08 07:23:30 -0600 commented question Crop rectangle and apply transformation from image?

when i draw largest contour its return this :- http://i.stack.imgur.com/BhNhI.jpg

2016-07-08 07:18:37 -0600 commented question Crop rectangle and apply transformation from image?

Hello @Missing,i have tried with following code:

     Imgproc.findContours(gray, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
            int i=0;
            MatOfPoint largestContour=contours.get(0);
            double largestares=Imgproc.contourArea(largestContour);
            for (MatOfPoint contour : contours)            {
              MatOfPoint temp_contour = contours.get(i);
                double temparea = Imgproc.contourArea(temp_contour);
              if (temparea > largestares) {
                        largestares=temparea;
                        largestContour=temp_contour;
                        largestRectNo=i;
                        Log.e("Contour Size",largestares+"");}}
2016-07-07 09:03:27 -0600 commented question Crop rectangle and apply transformation from image?

Yes @Missing i calculated contours but i want find largest contour and crop into new mat.please help me.thanks

2016-07-07 02:42:25 -0600 asked a question Crop rectangle and apply transformation from image?

Hello all,

i want to detect paper sheet from image.i applied medianBlur, Canny ,dilate,threshold,Etc. algorithms to find.i am able to find sheet but dont know how to crop rectangle and apply transformation

image description

this is my code:-

    Mat blurred = new Mat();
    Imgproc.medianBlur(src, blurred, 9);

    // Set up images to use.
    Mat gray0 = new Mat(blurred.size(), CvType.CV_8U);
    Mat gray = new Mat();

    // For Core.mixChannels.
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    List<MatOfPoint2f> rectangles = new ArrayList<MatOfPoint2f>();

    List<Mat> sources = new ArrayList<Mat>();
    sources.add(blurred);
    List<Mat> destinations = new ArrayList<Mat>();
    destinations.add(gray0);

    // To filter rectangles by their areas.
    int srcArea = src.rows() * src.cols();

    // Find squares in every color plane of the image.
    for (int c = 0; c < 3; c++) {
        int[] ch = {c, 0};
        MatOfInt fromTo = new MatOfInt(ch);

        Core.mixChannels(sources, destinations, fromTo);

        // Try several threshold levels.
        for (int l = 0; l < N; l++) {
            if (l == 0) {
                // HACK: Use Canny instead of zero threshold level.
                // Canny helps to catch squares with gradient shading.
                // NOTE: No kernel size parameters on Java API.
                Imgproc.Canny(gray0, gray, 0, CANNY_THRESHOLD);

                // Dilate Canny output to remove potential holes between edge segments.
                Imgproc.dilate(gray, gray, Mat.ones(new Size(3, 3), 0));
            } else {
                int threshold = (l + 1) * 255 / N;
                Imgproc.threshold(gray0, gray, threshold, 255, Imgproc.THRESH_BINARY);
            }

            // Find contours and store them all as a list.
            Imgproc.findContours(gray, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
            int i=0;
            for (MatOfPoint contour : contours) {
                MatOfPoint2f contourFloat = GeomUtils.toMatOfPointFloat(contour);
                double arcLen = Imgproc.arcLength(contourFloat, true) * 0.02;

                // Approximate polygonal curves.
                MatOfPoint2f approx = new MatOfPoint2f();
                Imgproc.approxPolyDP(contourFloat, approx, arcLen, true);

                if (isRectangle(approx, srcArea)) {
                    Imgproc.drawContours(src, contours, i, new Scalar(255, 0, 0), 3);
                    //rectangles.add(approx);
                    /*Rect rect = Imgproc.boundingRect(contour);
                    Log.e("Rectangle Finder:-" + i, "Height:-" + rect.height + ", Width:-" + rect.width + " and Area:-" + rect.area() + "\nX:-" + rect.x + ",Y:-" + rect.y);*/
                }
                i++;
            }
        }

i want to select only white papersheet.please help me

Thanks in advance

2016-07-01 08:42:48 -0600 commented answer Find rectangle from image in android?

hello @lama123, i am very thankfull if you help me on this topic http://answers.opencv.org/question/96...

2016-07-01 08:39:58 -0600 asked a question How to apply PerspectiveTransformation on image in android?

Hello all, i want to detect sheet from image. i am able to detect sheet and apply warpPerspective if background is black but if background is light then its not detecting so.

my code:-

     public Mat transform(Mat src, MatOfPoint2f corners) {

    MatOfPoint2f sortedCorners = sortCorners(corners);
    Size size = getRectangleSize(sortedCorners);

    Log.e(DEBUG_TAG, String.format("Transforming to: %f %f", size.width, size.height));

    Mat result = Mat.zeros(size, src.type());
    MatOfPoint2f imageOutline = getOutline(result);

    Mat transformation = Imgproc.getPerspectiveTransform(sortedCorners, imageOutline);
    Imgproc.warpPerspective(src, result, transformation, size);

    return result;
}

private Size getRectangleSize(MatOfPoint2f rectangle) {
    Point[] corners = rectangle.toArray();

    double top = getDistance(corners[0], corners[1]);
    double right = getDistance(corners[1], corners[2]);
    double bottom = getDistance(corners[2], corners[3]);
    double left = getDistance(corners[3], corners[0]);

    double averageWidth = (top + bottom) / 2f;
    double averageHeight = (right + left) / 2f;

    return new Size(new Point(averageWidth, averageHeight));
}

private double getDistance(Point p1, Point p2) {
    double dx = p2.x - p1.x;
    double dy = p2.y - p1.y;
    return Math.sqrt(dx * dx + dy * dy);
}

private MatOfPoint2f getOutline(Mat image) {
    Point topLeft = new Point(0, 0);
    Point topRight = new Point(image.cols(), 0);
    Point bottomRight = new Point(image.cols(), image.rows());
    Point bottomLeft = new Point(0, image.rows());
    Point[] points = {topLeft, topRight, bottomRight, bottomLeft};

    MatOfPoint2f result = new MatOfPoint2f();
    result.fromArray(points);

    return result;
}

private MatOfPoint2f sortCorners(MatOfPoint2f corners) {
    Point center = getMassCenter(corners);
    List<Point> points = corners.toList();
    List<Point> topPoints = new ArrayList<Point>();
    List<Point> bottomPoints = new ArrayList<Point>();

    for (Point point : points) {
        if (point.y < center.y) {
            topPoints.add(point);
        } else {
            bottomPoints.add(point);
        }
    }

    Point topLeft = topPoints.get(0).x > topPoints.get(1).x ? topPoints.get(1) : topPoints.get(0);
    Point topRight = topPoints.get(0).x > topPoints.get(1).x ? topPoints.get(0) : topPoints.get(1);
    Point bottomLeft = bottomPoints.get(0).x > bottomPoints.get(1).x ? bottomPoints.get(1) : bottomPoints.get(0);
    Point bottomRight = bottomPoints.get(0).x > bottomPoints.get(1).x ? bottomPoints.get(0) : bottomPoints.get(1);

    MatOfPoint2f result = new MatOfPoint2f();
    Point[] sortedPoints = {topLeft, topRight, bottomRight, bottomLeft};
    result.fromArray(sortedPoints);

    return result;
}

private Point getMassCenter(MatOfPoint2f points) {
    double xSum = 0;
    double ySum = 0;
    List<Point> pointList = points.toList();
    int len = pointList.size();
    for (Point point : pointList) {
        xSum += point.x;
        ySum += point.y;
    }
    return new Point(xSum / len, ySum / len);
}
2016-06-20 02:22:45 -0600 commented answer Sorting contours from left to right and top to bottom

hello berak,Can you please explain this code in java?

2016-06-19 23:56:26 -0600 commented answer Find rectangle from image in android?

yes lama123. specially all corner where rectangle placed

2016-06-17 05:59:15 -0600 commented answer Find rectangle from image in android?

hello lama123. please just tell me how to check intensity of image?

2016-06-17 05:46:43 -0600 marked best answer Find rectangle from image in android?

hello,

i want to find four black cornered rectangle from below image

how to find those four border rectangle from this image?

i have tried with this code

 Mat rgbMat=ImageUtils.bitmapToMat(resultBitmap);

        Mat grayMat = new Mat(resultBitmap.getHeight(), resultBitmap.getWidth(),CvType.CV_8U, new Scalar(1));
        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY, 2);
        Imgproc.threshold(grayMat, grayMat, 100, 255, Imgproc.THRESH_BINARY);
        Core.bitwise_not(grayMat, grayMat);

        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Mat hierarchy = new Mat();

        Imgproc.findContours(grayMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
        List<Point> pointList=new ArrayList<Point>();
        for(int i=0; i<contours.size(); i++)
        {
            Rect rect = Imgproc.boundingRect(contours.get(i));
            double k = (rect.height+0.0)/rect.width;
            if (0.9<k && k<1.1 && rect.area() > 100)
            {
                Imgproc.drawContours(rgbMat, contours, i, new Scalar(255, 0, 0), 3);
            }
        }

        resultBitmap = ImageUtils.matToBitmap(rgbMat);

Please help me ..

Thanks in advance

2016-06-17 05:45:39 -0600 commented answer Find rectangle from image in android?

Thankyou very very much lama123.you are great and very helpfull

2016-06-16 08:40:33 -0600 commented answer Find rectangle from image in android?

Thank you lama123 for your help. but i can't convert this code to java

int left = stats.at<int>(i, CC_STAT_LEFT);
int top = stats.at<int>(i, CC_STAT_TOP);
double width = stats.at<int>(i, CC_STAT_WIDTH);
double height = stats.at<int>(i, CC_STAT_HEIGHT);
int area = stats.at<int>(i, CC_STAT_AREA);

please help anybody for convert this code to java