Crop black edges with OpenCV

后端 未结 7 1515
遥遥无期
遥遥无期 2020-12-01 01:08

I think it should be a very simple problem, but I cannot find a solution or an effective keyword for search.

I just have this image.

7条回答
  •  佛祖请我去吃肉
    2020-12-01 01:30

    Python Version 3.6


    Crop images and insert into a 'CropedImages' folder

    import cv2
    import os
    
    arr = os.listdir('./OriginalImages')
    
    for itr in arr:
        img = cv2.imread(itr)
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        _,thresh = cv2.threshold(gray, 120, 255, cv2.THRESH_BINARY)
        _, contours, _ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
        cnt = contours[0]
        x,y,w,h = cv2.boundingRect(cnt)
        crop = img[y:y+h,x:x+w]
        cv2.imwrite('CropedImages/'+itr,crop)
    

    Change the number 120 to other in 9th line and try for your images, It will work

提交回复
热议问题