Opencv Python Crop Image Using Numpy Array

前端 未结 1 2040
自闭症患者
自闭症患者 2021-01-12 16:26

I am using OpenCV 3.1.0-dev and python 2.7.

I am trying to crop out the black exterior of an image I have stitched. The struggle is that there are other pixels in t

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-12 17:05

    Once you have the contours, you can do a list of the x and the y of them and then find the maximum and minimum:

    x, y = [], []
    
    for contour_line in contours:
        for contour in contour_line:
            x.append(contour[0][0])
            y.append(contour[0][1])
    
    x1, x2, y1, y2 = min(x), max(x), min(y), max(y)
    
    cropped = img[y1:y2, x1:x2]
    

    The (x1, y1) would be the top left corner and the (x2, y2) the bottom right.

    Hope this helped!

    0 讨论(0)
提交回复
热议问题