-1

i need to crop the below image by detecting co-ordinates of the image using opencv. i tried below code but it's not working as expected.

import cv2

#reading image
image = cv2.imread("input.PNG")

#converting to gray scale
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

#applying canny edge detection
edged = cv2.Canny(image, 10, 250)

#finding contours
(_, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
idx = 0
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    if w>50 and h>50:
        idx+=1
        new_img=image[y:y+h,x:x+w]
        #cropping images
        cv2.imwrite("cropped/"+str(idx) + '.png', new_img)
#cv2.imshow("Original Image",image)
#cv2.imshow("Canny Edge",edged)
#cv2.waitKey(0)

Input Image:

enter image description here

Output image:

enter image description here

6
  • What you are looking for is cv2.minAreaRect()
    – Jeru Luke
    Jun 3, 2022 at 7:28
  • uploaded input and expected output image. Please check
    – kp987987
    Jun 3, 2022 at 7:29
  • 1
    Watching the input and output images you would not only to crop the image but to rotate them, isn't it? Jun 3, 2022 at 7:32
  • No,its already orientation is done.need to crop only
    – kp987987
    Jun 3, 2022 at 7:38
  • @DavidSerrano ,sorry yes you are right. after crop again rotate is needed.
    – kp987987
    Jun 3, 2022 at 9:38

1 Answer 1

1

Cropping the image based on coordinates returned from cv2.boundingRect() will not give you the desired result. You need to obtain the 4 corners of the image and orient it accordingly. This can be done using the perspective transformation matrix

Also, detecting edges and later finding contours is not the right way to go. You need to find a contour large enough to enclose the entire page. To do so, binarize the image and find the largest external contour.

Code:

# read image and binarize
img = cv2.imread(r'C:\Users\524316\Desktop\Stack\dc.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

# find the largest contour
contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
c = max(contours, key = cv2.contourArea)

# To find the 4 corners of the contour
rect = cv2.minAreaRect(c)
input_corners = cv2.boxPoints(rect)
input_corners = np.int0(input_corners)

# We need 4 new points onto which the 4 input points need to be warped
# which will be done on an image with the same size as the input image
ht, wd = img.shape[:2]
output_corners = [[0,0], [wd,0], [wd,ht], [0,ht]]

# converting to float data type
input_corners = np.float32(input_corners)
output_corners = np.float32(output_corners)

# get the transformation matrix
M = cv2.getPerspectiveTransform(input_corners, output_corners)
# perform warping 
warped = cv2.warpPerspective(img, M, (wd, ht))

cv2.imshow('Warped output', warped)

Result:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.