How to identify black border of photo and remove this border?

I need to detect black border in image. I tried to detect it using OpenCV, but the results were not good.
Code here:

import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage.filters import threshold_local

image = cv2.imread("image.jpg")

V = cv2.split(cv2.cvtColor(image, cv2.COLOR_BGR2HSV))[2]
T = threshold_local(V, 15, offset=5, method="gaussian")
thresh = (V > T).astype("uint8") * 255
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
plt.imshow(thresh, cmap='gray')

Other code:

def find_threshold_img(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # # apply thresholding to convert grayscale to binary image
    # ret,thresh = cv2.threshold(gray,100,200,0)
    thresh = cv2.threshold(gray, 0, 30, cv2.THRESH_BINARY|cv2.THRESH_OTSU)[1]
    return thresh
thresh = find_threshold_img(image)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)

plt.imshow(image, cmap='gray')

This is the image:

New contributor

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

1

The basic idea is to detect everything that is not black. You can HSV-threshold on black, apply some morphology to get a nice binary mask, detect external contours and keep the biggest contour in the frame. Finally, get the bounding rectangle of this contour and use numpy slicing to crop the original image.

# Imports:
import numpy as np
import cv2

# Set image path
imagePath = "D://opencvImages//2fzmatjM.jpg"

# Load image:
inputImage = cv2.imread(imagePath)
# Deep copy of the input:
inputImageCopy = inputImage.copy()

# Resize because the original image is HUGE:
(h, w) = inputImage.shape[:2]
resizePercent = 10
resizedWidth = int(w * resizePercent / 100)
resizedHeight = int(h * resizePercent / 100)

# Resize image
inputImage = cv2.resize(inputImage, (resizedWidth, resizedHeight), interpolation=cv2.INTER_LINEAR)

# Get binary image:
grayImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2HSV)

lowerValues = np.array([0, 0, 40])
upperValues = np.array([179, 255, 255])

# Create HSV mask:
hsvImage = cv2.cvtColor(inputImage.copy(), cv2.COLOR_BGR2HSV)
tempMask = cv2.inRange(hsvImage, lowerValues, upperValues)

# Apply some morphology:
kernelSize = (3, 3)
morphoKernel = cv2.getStructuringElement(cv2.MORPH_RECT, kernelSize)
tempMask = cv2.morphologyEx(tempMask, cv2.MORPH_CLOSE, morphoKernel, iterations=1)

This will produce the following binary mask:

Then, just detect the biggest contour and keep its bounding rectangle. Also, don’t forget to upscale the rectangle to crop the original image:

# Find the largest external contour:
contours, _ = cv2.findContours(tempMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Area filter:
maxArea = 0
maxRectangle = ()

for c in contours:

    # Blob Area:
    blobArea = cv2.contourArea(c)

    # Keep largest blob only:
    if blobArea > maxArea:
        # Set max Area:
        maxArea = blobArea

        # Get the bounding rectangle:
        boundRect = cv2.boundingRect(c)

        # Get the dimensions of the bounding rect:
        rectX = int(boundRect[0])
        rectY = int(boundRect[1])
        rectWidth = int(boundRect[2])
        rectHeight = int(boundRect[3])

        # Into a nice tuple:
        maxRectangle = (rectX, rectY, rectWidth, rectHeight)

# Upscale rectangle:
upscaledRectangle = []
for dimension in maxRectangle:
    upscaledDimension = int(dimension * 100 / resizePercent)
    upscaledRectangle.append(upscaledDimension)

# Crop image:
croppedImage = inputImageCopy[upscaledRectangle[1]:upscaledRectangle[1] + upscaledRectangle[3],
               upscaledRectangle[0]:upscaledRectangle[0] + upscaledRectangle[2]]

Which produces the final image. Again, resized, because the image is gargantuan:

Note: The border is not straight. In fact, this looks like a photo of a monitor and the black border isn’t really solid black. Also, you are mixing different image processing libraries which is not great because they are not compatible with each other.

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