简体   繁体   English

在python中使用PIL修剪图像中的空白

[英]Trimming the white space in an image using PIL in python

I am doing handwritten digit recognition using SciKit-learn so for that I need to crop the clicked picture so I have prepared a template on the Word. 我正在使用SciKit-learn进行手写数字识别,因此我需要裁剪单击的图片,因此我在Word上准备了模板。 Now what I want is the image to be cropped along the border so that I can crop it further to extract the digits. 现在我想要的是沿着边框裁剪图像,以便可以进一步裁剪以提取数字。
Sample Image is given below: 示例图片如下:

在此处输入图片说明

For cropping the image I am using this Code. 为了裁剪图像,我正在使用代码。

Below is the parent Image from which the above rectangle has been cropped: 下面是裁剪了上面矩形的父图像:
在此处输入图片说明

Note: The parent image has a border too(which is not visible in the image) so trimming the white space might help in getting a modified parent image so that predefined (height, width) would be almost same for various crops to be done on the image. 注意:父图像也有边框(在图像中不可见),因此修剪空白可能有助于获取修改后的父图像,以便对要进行的各种作物的预定义(高度,宽度)几乎相同图片。

You could apply this pipeline: convert to grayscale -> apply thresholding (convert to white & black) -> find contours -> choose the contours of the right shape. 您可以应用此管道:转换为灰度->应用阈值处理(转换为白色和黑色)->查找轮廓->选择正确形状的轮廓。

Here is example code: 这是示例代码:

#!/usr/bin/env python

import cv2

BLACK_THRESHOLD = 200
THIN_THRESHOLD = 10
ANNOTATION_COLOUR = (222,0,222)

img = cv2.imread('template.png')
orig = img.copy()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, thresh=BLACK_THRESHOLD, maxval=255, type=cv2.THRESH_BINARY_INV)[1]

# Optional: save thesholded image
cv2.imwrite("temp_thres.png", thresh)

# Find contours on the thresholded image
contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
for cont in contours:
    # Find bounding rectangle of a contour
    x,y,w,h = cv2.boundingRect(cont)
    # Skip thin contours (vertical and horizontal lines)
    if h<THIN_THRESHOLD or w<THIN_THRESHOLD:
        continue
    # Does the countour has the right shape (roughly four times longer than high)?
    if 3*h<w<5*h:
        roi = orig[y:y+h,x:x+w]
        cv2.imwrite("four_letters.png",roi)

    # Optional: draw annotations
    cv2.rectangle(img,(x,y),(x+w,y+h),ANNOTATION_COLOUR,3)

# Optional: save annotated image
cv2.imwrite("temp_cont.png",img)

(You can delete the three optional steps. They are just for generating images temp_thres.png and temp_cont.png .) (您可以删除这三个可选步骤。它们只是用于生成图像temp_thres.pngtemp_cont.png 。)

Input image template.png : 输入图像template.png

输入图像:空白模板

Thresholded image temp_thres.png : 阈值图像temp_thres.png

黑白阈值图像

Found contours temp_cont.png : 找到轮廓temp_cont.png

带有两个区域的原始图像

Four letter space four_letters.png : 四个字母的空间four_letters.png

裁剪四个字母的空间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM