Cropping Images in Python/OpenCV so Image Doesn’t Include Dead/White/Black Space (Different Size Height/Width)

I have been working to stitch together multiple images in a grid (2 by 2 in this case) using SIFT and ANN. When all of the images are the same orientation, translation scale factor, my images are coming out perfectly.

Input Images:
4 Input Images

Stitched Image (Grayscale):

Stitched Image

The issue arises when I translate the images (this algorithm needs to run on actual datasets where every image is not perfect). When I translate the input images even 1 pixel from their original orientation, I begin seeing black lines appear all over the images.

Top-Left Image (Translated 20 pixels in the x and y directions):

Image with Thick Horizontal Black Line

(The line here is 20 pixels thick).

Top-Left and Top-Right Images (Translated 5 pixels in the x and y directions):

Image with Horizontal and Vertical Black Line.

(The vertical line here is 5 pixels thick and the horizontal line here is 1 pixel thick).

I did some investigating and the black lines show up during the stitching process, when parts of the image are lost, due to the translation. The cropping algorithm includes these missing pieces in the final image (showing up as black).

My overall stitching algorithm involves stitching all the horizontal images together first and then loops vertically to stitch the images vertically. This means that if there is any blank space from one of the previous stitched images, it carries forward and ends up appearing somewhere along the final image:

See this image for the first iteration of the algorithm, keeping the black space at the bottom: Top Image

Here’s the algorithm I incorporated to remove the seams and extra black space:

# CLEAN UP IMAGE: The following code will clean up the merged image, removing any seams that may arise, ensuring the image stays grayscale, and cropping the image to scale. 
def CleanImage(result):
    # Remove Seams, Ensure Grayscale, Crop Image to Scale
    
    # grayscale images
    finalG = result
    thresh = cv.threshold(finalG, 0, 255, cv.THRESH_BINARY|cv.THRESH_OTSU)[1]
    #OTSU's binarization determines the best value to separate the peaks as certain pixels/colors appear

    cnts = cv.findContours(thresh.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    print(cnts)
    cnts = imutils.grab_contours(cnts)
    print("Contours", len(cnts))

    # find maximum contour area
    c = max(cnts, key=cv.contourArea)
    (x, y, w, h) = cv.boundingRect(c)

    # crop image to smallest box coordinates
    finalG = finalG[y:y + h, x:x + w]
    
    cnts = cv.findContours(thresh.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)

    #output final result
    printF(finalG)
    
    return finalG

The issue is that this code crops to the maximum x and y values, meaning that if parts of one side of the image are lost but the other side is not (see this image above), then the crop still keeps the dead space.

My question here is: How do I crop these images so that the dead space is removed (e.g. how do I crop these images using a coordinate corresponding to my desired edge)?

Thank you!

New contributor

Hana Karim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật