winadiw
winadiw

Reputation: 55

Crop phone screen using contour

I want to crop phone screen given on the image. I tried this code but the result is not what i wanted. Phone Screen

import cv2
import numpy as np

img = cv2.imread('phone_test.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_,thresh = cv2.threshold(gray,1,255,cv2.THRESH_BINARY)

_, contours, _= cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
x,y,w,h = cv2.boundingRect(contours[0])

crop = img[y:y+h,x:x+w]
cv2.imwrite('phone_test_crop.jpg',crop)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image',img) #show the image
cv2.waitKey(0)

The result would be just this Crop Result

any solutions?

Upvotes: 0

Views: 276

Answers (2)

winadiw
winadiw

Reputation: 55

This is my solution to the problem.

import cv2
import numpy as np 

# read and scale down image
img = cv2.pyrDown(cv2.imread('sample_images/phone_test.jpg', 
cv2.IMREAD_UNCHANGED))

# threshold image
ret, threshed_img = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
            150, 255, cv2.THRESH_BINARY)
# find contours and get the external one
image, contours, hier = cv2.findContours(threshed_img, 
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for c in contours:
    # get the bounding rect
    x, y, w, h = cv2.boundingRect(c)
    crop = img[y:y+h+22,x:x+w+5] #give a bit of space, because the boundingRect is not perfect

cv2.imwrite('sample_images/phone_test_crop.jpg', crop)

cv2.destroyAllWindows()

Before: Before crop

After: After crop

Upvotes: 1

lucians
lucians

Reputation: 2269

Ok, here is the code which I made and worked for me.

import cv2
import numpy as np

img = cv2.imread('phone.jpg')
# cv2.imshow('image', img)
# cv2.waitKey(0)

# Convert BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# define range of blue color in HSV
lower = np.array([20, 20, 20])
upper = np.array([220, 220, 220])

# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower, upper)
# cv2.imshow('mask', mask)
# cv2.waitKey(0)

kernel = np.ones((1, 10), np.uint8)
img_dilation = cv2.dilate(mask, kernel, iterations=1)
# cv2.imshow('dilated', img_dilation)
# cv2.waitKey(0)

# find contours
im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])

for i, ctr in enumerate(sorted_ctrs):
    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)

    # Getting ROI
    roi = img[y:y + h, x:x + w]

    # show ROI
    # cv2.imshow('segment no:'+str(i), roi)
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 1)  # put (0,255,0) to see green rectangles
    # cv2.waitKey(0)

    if h > 100 and 350 < w < 450:
        # cv2.imshow('marked areas', img)
        # cv2.waitKey(0)

        # cv2.imshow('roi', roi)
        # cv2.waitKey(0)

        cv2.imwrite('extracted_screen.png', roi)

Is up to you tweak the code to fit specific needs (ex: more or less precision on screen extraction).

Result

extracted_screen res2

Upvotes: 1

Related Questions