Python提取圖片的ROI


圖像處理經常需要提取圖片的ROI,本文使用Python提取圖片的ROI。

使用的Module是PIL (Pillow),一個圖像處理庫,用到的函數為類 Image 中的 crop 方法。

函數原型為:

Image.crop(box=None)

Returns a rectangular region from this image. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate.
This is a lazy operation. Changes to the source image may or may not be reflected in the cropped image. To break the connection, call the load() method on the cropped copy.
Parameters:    box – The crop rectangle, as a (left, upper, right, lower)-tuple.
Return type:    Image
Returns:    An Image object.

知道矩形的左上角的坐標和右下角的坐標,即可構造box,例如下面的代碼

box = (100, 100, 400, 400)
region = im.crop(box)

知道如何提取除ROI時,上面例子為 region,保存ROI到圖像則使用類 Image 的 save 方法

region.save(filename)

 

給出一個Demo,使用人臉數據庫GENKI部分的圖像做實驗,該數據的數字子集GENKI-SZSL提供人臉區域的坐標和大小。提取代碼提供如下

from PIL import Image
import os

src = '.'

imlist = open(src + '/GENKI-SZSL_Images.txt', 'r').readlines()

rs = [float(line.split()[1]) for line in open(src + '/GENKI-SZSL_labels.txt', 'r').readlines()]
cs = [float(line.split()[0]) for line in open(src + '/GENKI-SZSL_labels.txt', 'r').readlines()]
ss = [float(line.split()[2]) for line in open(src + '/GENKI-SZSL_labels.txt', 'r').readlines()]

for i in range(0, len(rs)):
    path = src + '/images/' + imlist[i].strip()
    filename = src + '/output/' + imlist[i].strip()

    try:
        im = Image.open(path)
    except:
        continue
    
    r = rs[i]
    c = cs[i]
    s = ss[i]

    xLeft   = int(c - s/2)
    yUpper  = int(r - s/2)
    xRight  = int(c + s/2)
    yLower  = int(r + s/2) 

    region = im.crop((xLeft, yUpper, xRight, yLower))
    region.save(filename)

代碼打包下載:http://pan.baidu.com/s/1dD4opKP 密碼:ygu7

Pillow的項目文檔地址: http://pillow.readthedocs.org


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM