通过在Java / Python中删除照片中的黑色边框,从图像中裁剪出精确的文档纸 [英] Crop exact document paper from image by removing black border from photos in Java/Python

查看:194
本文介绍了通过在Java / Python中删除照片中的黑色边框,从图像中裁剪出精确的文档纸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为索引卡拍摄了一些照片,但是现在我遇到的问题是,拍摄当然并不完美,每张照片上都有黑色边框。我想裁剪照片,以便只留下索引卡没有边框。



在线我可以找到类似的问题,但这仅与计算机图像有关,因此您可以假设黑色具有RGB(0,0,0),而我不是这种情况。中间还有黑色的文字,我不想删掉。



您有什么想法可以解决这个问题吗?



以下是示例图片(德语):



解决方案

想法是对图像进行阈值处理以获得二进制图像,然后找到轮廓并使用轮廓区域进行排序。最大轮廓应为索引卡。然后,我们可以应用



结果





结果较暗,因此要增加对比度,请查看使用OpenCV自动调整一张纸的彩色照片的对比度和亮度。而且它略有倾斜,因此您应该执行倾斜校正。看看 Python OpenCV倾斜校正如何删除-歪斜图像,然后检测图像方向角



我将把这些步骤留给您:)



代码

  from imutils.perspective import four_point_transform 
import cv2
import numpy

#加载图像,灰度,高斯模糊,大津的阈值
image = cv2.imread( 1.jpg)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
blur = cv2。 GaussianBlur(gray,(5,5),0)
thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

#查找轮廓一种nd最大轮廓的排序
cnts = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts [0]如果len(cnts)== 2否则cnts [1]
cnts = sorted(cnts,key = cv2.contourArea,reverse = True)
displayCnt = None

for c in cnts:
#执行轮廓逼近
围= cv2.arcLength(c,真)
约= cv2.approxPolyDP(c,0.02 *围,真)
如果len(约)== 4:
displayCnt =约
break

#获取鸟瞰图像
变形= four_point_transform(image,displayCnt.reshape(4,2))

cv2.imshow( 脱粒,脱粒)
cv2.imshow(翘曲,变形)
cv2.imshow(图像,图像)
cv2.imwrite( thresh.png,脱粒)
cv2.imwrite( warped.png,warped)
cv2.imwrite( image.png,image)
cv2.waitKey()


I have taken some pictures of index cards but now I have the problem that the photographing was not perfect of course and there is a black border on each photo. I would like to crop the photo so that only the index card is left without a border.

Online I could find a similar problem, but it was only about computer images, so you could assume that the black had RGB (0,0,0), which is not the case with me. In the middle there is also black text, which I don't want to cut out.

Do you have any ideas how I can approach this?

Here is an example picture (it is in German):

解决方案

The idea is to threshold the image to obtain a binary image then find contours and sort using contour area. The largest contour should be the index card. We can then apply a four point perspective transform to obtain a birds eye view of the image. Here's the results:

Binary image

Result

The result is dark so to increase the contrast look at Automatic contrast and brightness adjustment of a color photo of a sheet of paper with OpenCV. Also it's slightly skewed so you should perform skew correction. Take a look at Python OpenCV skew correction, How to de-skew an image, and Detect image orientation angle based on text direction

I'll leave these steps to you :)

Code

from imutils.perspective import four_point_transform
import cv2
import numpy

# Load image, grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread("1.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

# Find contours and sort for largest contour
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
displayCnt = None

for c in cnts:
    # Perform contour approximation
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)
    if len(approx) == 4:
        displayCnt = approx
        break

# Obtain birds' eye view of image
warped = four_point_transform(image, displayCnt.reshape(4, 2))

cv2.imshow("thresh", thresh)
cv2.imshow("warped", warped)
cv2.imshow("image", image)
cv2.imwrite("thresh.png", thresh)
cv2.imwrite("warped.png", warped)
cv2.imwrite("image.png", image)
cv2.waitKey()

这篇关于通过在Java / Python中删除照片中的黑色边框,从图像中裁剪出精确的文档纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆