Ask Your Question

khrizt's profile - activity

2017-06-19 07:18:24 -0500 received badge  Self-Learner (source)
2017-06-19 07:18:24 -0500 received badge  Necromancer (source)
2017-06-19 06:46:00 -0500 asked a question Convert background to white

Hi,

I have already finished my document scanner but I'm trying to improve the results. I correctly detect edges, crop and fix perspective of the document but I wanted to improve the readability of the final image. Here I have a cropped document:

image description

And I wanted to clear a liitle bit the background of the image and remove the dog-eared cases. I've read about the grab-cut algorithm but I'm not really sure if this is what I need. In this case the document does not have white background but it usually will. Any idea if the grab-cut is the correct one and how to implement it?

Thanks

2017-06-19 06:33:48 -0500 answered a question Problem when transporting points to a bigger image

For those who find themselves in the same position, the problem was the division between integers results in an integer value instead of a double value. So this:

double scaleWidth = imgMat.width() / previewSizeWidth;
double scaleHeight = imgMat.height() / previewSizeHeight;

should be:

double scaleWidth = ((double) imgMat.width()) / ((double) previewSizeWidth);
double scaleHeight = ((double) imgMat.height()) / ((double) previewSizeHeight);

besides this the transportation works flawlessly.

2017-03-24 03:04:11 -0500 received badge  Enthusiast
2017-03-23 05:41:48 -0500 received badge  Student (source)
2017-03-23 05:11:16 -0500 commented question Problem when transporting points to a bigger image

Both images line up perfectly, and even the warp perspective on the preview gives a perfectly cropped and square image. I guess it can be something about the scale but I really don't know what. Thanks

2017-03-22 10:01:56 -0500 commented question Problem when transporting points to a bigger image

Sorry Steven, I've added some code to see if someone has some insight. Thanks!

2017-03-22 03:16:10 -0500 received badge  Editor (source)
2017-03-22 03:15:40 -0500 commented question Problem when transporting points to a bigger image

The test with a chessboard gives rectangular shapes instead of square shapes and same margin problem. I've updated the question with those images to see if anyone can help me out. Thanks!

2017-03-22 03:02:56 -0500 commented question Problem when transporting points to a bigger image

Thanks, will try that.

2017-03-21 15:03:38 -0500 asked a question Problem when transporting points to a bigger image

Hi,

I'm developing a document scanner library from Android and I have almost everthing sorted but there's one thing that I can't get an appropiate solution. The library detects, crops and fixes the perspective for the document it finds, on the preview the contour is found perfectly adjusted to the document so it stores this contour and takes a full resolution picture, then multiplies (Core.multiply) the found set of points to crop the full-sized image and in that image I don't get the same adjustment that in the preview.

Here is an example of the preview (the document is lightly highlighted with a blue rectangle):

image description

and the full-sized image (that has some annoying margin):

image description

Has someone any idea what can be happening?

Thanks a lot.

EDIT:

The code I use to find the contour:

Imgproc.GaussianBlur(mat, mat, new Size(5.d, 5.d), 0);
Imgproc.Canny(mat, mat, 40, 180);
Imgproc.findContours(imageMat, points, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

for (int i = 0; i < points.size(); ++i) {
    MatOfInt convexedPoints = new MatOfInt();
    Imgproc.convexHull(points.get(i), convexedPoints);
    points.set(i, convertIndexesToPoints(points.get(i), convexedPoints));
}

// sort points by size

MatOfPoint approxPoint = null;
for (MatOfPoint p : points) {
    MatOfPoint2f c = new MatOfPoint2f(p.toArray());
    double peri = Imgproc.arcLength(c, true);
    MatOfPoint2f approx = new MatOfPoint2f();
    Imgproc.approxPolyDP(c, approx, 0.1 * peri, true);

    if (approx.toArray().length == 4 && Imgproc.contourArea(p) >= (imageArea * 0.25)) {
        // this is the contour
    }
}

And the code to convert the detected contour to the full-sized image:

    double scaleWidth = imgMat.width() / previewSizeWidth;
double scaleHeight = imgMat.height() / previewSizeHeight;

MatOfPoint scaled = new MatOfPoint();
Core.multiply(detectedPoint, new Scalar(scaleWidth, scaleHeight), scaled);

// from scaled, sort points to get topLeft, topRight, ...

// calculate new width and height
double widthA = Math.sqrt(
        Math.pow(bottomRight.x - bottomLeft.x, 2) +
                Math.pow(bottomRight.y - bottomLeft.y, 2)
);
double widthB = Math.sqrt(
        Math.pow(topRight.x - topLeft.x, 2) +
                Math.pow(topRight.y - topLeft.y, 2)
);
double width = Math.max(widthA, widthB);

double heightA = Math.sqrt(
        Math.pow(topRight.x - bottomRight.x, 2) +
                Math.pow(topRight.y - bottomRight.y, 2)
);
double heightB = Math.sqrt(
        Math.pow(topLeft.x - bottomLeft.x, 2) +
                Math.pow(topLeft.y - bottomLeft.y, 2)
);
double height = Math.max(heightA, heightB);

// prepare mats for perspective and transform
MatOfPoint2f src = new MatOfPoint2f(
        topLeft,
        topRight,
        bottomRight,
        bottomLeft
);
MatOfPoint2f dest = new MatOfPoint2f(
        new Point(0, 0),
        new Point(width - 1, 0),
        new Point(width - 1, height - 1),
        new Point(0, height - 1)
);

// prepare mats for perspective and transform
MatOfPoint2f src = new MatOfPoint2f(
        topLeft,
        topRight,
        bottomRight,
        bottomLeft
);
MatOfPoint2f dest = new MatOfPoint2f(
        new Point(0, 0),
        new Point(width - 1, 0),
        new Point(width - 1, height - 1),
        new Point(0, height - 1)
);

// apply perspective and transform
Mat perspective = Imgproc.getPerspectiveTransform(src, dest);
Mat finalImage = new Mat((int) height, (int) width, CvType.CV_8UC1);
Imgproc.warpPerspective(image, finalImage, perspective, new Size(width, height));

Sorry for not attaching code earlier.

2017-03-20 03:06:44 -0500 received badge  Supporter (source)
2017-03-20 03:06:42 -0500 received badge  Scholar (source)
2017-03-16 12:09:47 -0500 commented question MatOfPoint image transportation

Wow, I didn't know this method. Yep, that works, thanks a lot!!

2017-03-16 04:28:46 -0500 asked a question MatOfPoint image transportation

Hi,

I'm developing a document scanner using OpenCV and I succesfully detected and drawed the contour on the preview of the JavaCameraView, now I want to take a picture and use the detected points to extract that part of the image from the newly taken image. That image taken by the camera is bigger than the preview, is there a way to use the same MatOfPoint in the new image "transporting" (I don't know if it's the right word) the points?

Thanks