我正在掃描舊照片,因此我有掃描儀的圖像和白色背景。我的目標是拍攝照片,消除白色背景。我怎樣才能做到這一點 ?如何使用python在白色背景上裁剪圖像?
一個例子畫面如下:
我的簡單的方法:
import os
import time
from PIL import Image
from collections import Counter
import numpy as np
def get_cropped_image(image, crop_folder, threshold):
image_name = image.split("\\")[-1]
im = Image.open(image)
pixels = im.load()
width, height = im.size
rows = []
for h_index in xrange(height):
row = []
for w_index in xrange(width):
row.append(pixels[((w_index, h_index))])
color_count = Counter(row)[(255, 255, 255)]/float(len(row))
rows.append([h_index, color_count])
columns = []
for w_index in xrange(width):
column = []
for h_index in xrange(height):
column.append(im.getpixel((w_index, h_index)))
color_count = Counter(column)[(255, 255, 255)]/float(len(column))
columns.append([w_index, color_count])
image_data = csv.writer(open("image_data.csv", "wb")).writerows(zip(rows, columns))
rows_indexes = [i[0] for i in rows if i[1] < threshold]
columns_indexes = [i[0] for i in columns if i[1] < threshold]
x1, y1, x2, y2 = columns_indexes[0], rows_indexes[0], columns_indexes[-1], rows_indexes[-1]
im.crop((x1, y1, x2, y2)).save(os.path.join(cropped_folder, "c_" + image_name))
結帳blob檢測算法和方法。 SimpleCV庫有一些。結帳:http://simplecv.readthedocs.org/en/1.0/cookbook/ – darxtrix 2014-10-11 04:26:16
你有我們可以看到的示例圖片嗎? – rayryeng 2014-10-11 22:02:41
他們是個人照片,所以我無法顯示。想象一下,將打印的照片放在掃描儀上並進行掃描。它使用打印的照片和白色背景創建圖像。 – GiannisIordanou 2014-10-11 22:31:27