Android mask and apply bitwise around contour

asked 2020-05-12 23:32:23 -0500

Managed to do this is Python but conversion to Android doesn't work. Can someone point me to the error? Objective: Crop images to show the screen only. Then once cropped, everything outside the screen is set to black.

Python code:

print('Please input file name with extension:')
image = cv2.imread(input(), cv2.IMREAD_COLOR) 

image2 = image.copy() #make copy for 2nd contour plot

scale_factor = 800 #input how big you want the image to be scaled down to here

original_image = scale_image(image.copy(), scale_factor)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 
gray = cv2.bilateralFilter(gray, 11, 17, 17) 
v = np.median(gray)
sigma = 0.33

lower = int(max(0, (1.0 - sigma) * v))
upper = int(min(255, (1.0 + sigma) * v))
edged = cv2.Canny(gray, lower, upper)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(9,9))
dilated = cv2.dilate(edged, kernel)
contours, hierarchy = cv2.findContours(dilated.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

print("Number of Contours found = " + str(len(contours)))

cnts = sorted(contours, key=cv2.contourArea,  reverse = True)[:10] 
screen_cnt = None

    for c in cnts:

        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.015 * peri, True)
        area = cv2.contourArea(cnts[1])

    if len(approx) == 4:
        screen_cnt = approx
        break

rect = cv2.boundingRect(screen_cnt)
x,y,w,h = rect
croped = image[y:y+h, x:x+w].copy()

screen_cnt = screen_cnt - screen_cnt.min(axis=0)

mask = np.zeros(croped.shape[:2], np.uint8)
cv2.drawContours(mask, [screen_cnt], -1, (255, 255, 255), -1, cv2.LINE_AA)

dst = cv2.bitwise_and(croped, croped, mask=mask)

cv2.drawContours(image2, [cnts[1]], -1, (0, 255, 0), 3) 
contour_scaled3 = scale_image(image2, scale_factor) 
cv2.imshow('Contours_of_screen', contour_scaled3)

Android code:

static void sortContoursByArea(List<MatOfPoint> contours) {

    for(int contourIdx=0; contourIdx < contours.size(); contourIdx++) {
        double areaC = Imgproc.contourArea(contours.get(contourIdx));
        if (areaC < 100000){
            contours.remove(contourIdx);
        }
    }

    Collections.sort(contours, (a, b) -> {

        double areaA = Imgproc.contourArea(a);
        double areaB = Imgproc.contourArea(b);

        // Change sign depending on whether your want sorted small to big
        // or big to small


        if (areaA < areaB) {
            return 1;
        } else if (areaA > areaB) {
            return -1;
        }
        return 0;
    });
}

public void edgeDetection(View v)
{
    Mat srcMat = new Mat (imageBitmap.getHeight(), imageBitmap.getWidth(), CvType.CV_8UC3);

    Bitmap myBitmap32 = imageBitmap.copy(Bitmap.Config.ARGB_8888, true);

    Utils.bitmapToMat(myBitmap32, srcMat);

    Mat gray = new Mat(srcMat.size(), CvType.CV_8UC1);
    Imgproc.cvtColor(srcMat, gray, Imgproc.COLOR_RGB2GRAY);
    Mat edge = new Mat();
    Mat edge2 = new Mat();
    Mat edge3 = new Mat();
    List<MatOfPoint> contours = new ArrayList<>();

    Mat hierarchy = new Mat();
    Mat edge4 = new Mat();

    Imgproc.bilateralFilter(gray, edge, 11, 17, 17);
    Imgproc.Canny(edge, edge2, 80, 90);
    Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5,5));
    Imgproc.dilate(edge2, edge3, element);
    Imgproc.findContours(edge3, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);

    sortContoursByArea(contours);

    for(int c = 0; c < contours.size(); c++) {
        MatOfPoint2f approxCurve = new MatOfPoint2f();
        MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(c).toArray());
        double approxDistance = Imgproc.arcLength(contour2f, true) * 0.015;
        Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

        if (approxCurve.toArray().length == 4) {
            MatOfPoint points = new MatOfPoint(approxCurve.toArray());
            // Get bounding rect of contour
            Rect rect = Imgproc.boundingRect(points);

            //Imgproc ...
(more)
edit retag flag offensive close merge delete