使用PIL用附近的颜色填充空图像空间

使用PIL用附近的颜色填充空图像空间

本文介绍了使用PIL用附近的颜色填充空图像空间(又称修复)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PIL创建图像:





我需要填写空白区域(描绘为黑色)。我可以很容易地用静态颜色填充它,但我想做的是用附近的颜色填充像素。例如,边界之后的第一个像素可能是填充像素的高斯模糊。或者也许是我知道OpenCV有它,不知道PIL。



您的示例:





我是用Mathematica做的。



编辑



根据你的要求,代码是:

  i = Import [http://i.stack.imgur.com/uEPqc.png]; 
Inpaint [i,ColorNegate @ Binarize @ i,Method - > NavierStokes]

ColorNegate @ ...部分创建替换蒙版。
仅使用 Inpaint [] 命令完成填充。


I create an image with PIL:

I need to fill in the empty space (depicted as black). I could easily fill it with a static color, but what I'd like to do is fill the pixels in with nearby colors. For example, the first pixel after the border might be a Gaussian blur of the filled-in pixels. Or perhaps a push-pull type algorithm described in The Lumigraph, Gortler, et al..

I need something that is not too slow because I have to run this on many images. I have access to other libraries, like numpy, and you can assume that I know the borders or a mask of the outside region or inside region. Any suggestions on how to approach this?

UPDATE:

As suggested by belisarius, opencv's inpaint method is perfect for this. Here's some python code that uses opencv to achieve what I wanted:

import Image, ImageDraw, cv

im = Image.open("u7XVL.png")
pix = im.load()

#create a mask of the background colors
# this is slow, but easy for example purposes
mask = Image.new('L', im.size)
maskdraw = ImageDraw.Draw(mask)
for x in range(im.size[0]):
    for y in range(im.size[1]):
        if pix[(x,y)] == (0,0,0):
            maskdraw.point((x,y), 255)

#convert image and mask to opencv format
cv_im = cv.CreateImageHeader(im.size, cv.IPL_DEPTH_8U, 3)
cv.SetData(cv_im, im.tostring())
cv_mask = cv.CreateImageHeader(mask.size, cv.IPL_DEPTH_8U, 1)
cv.SetData(cv_mask, mask.tostring())

#do the inpainting
cv_painted_im = cv.CloneImage(cv_im)
cv.Inpaint(cv_im, cv_mask, cv_painted_im, 3, cv.CV_INPAINT_NS)

#convert back to PIL
painted_im = Image.fromstring("RGB", cv.GetSize(cv_painted_im), cv_painted_im.tostring())
painted_im.show()

And the resulting image:

解决方案

A method with nice results is the Navier-Stokes Image Restoration. I know OpenCV has it, don't know about PIL.

Your example:

I did it with Mathematica.

Edit

As per your reuquest, the code is:

i = Import["http://i.stack.imgur.com/uEPqc.png"];
Inpaint[i, ColorNegate@Binarize@i, Method -> "NavierStokes"]

The ColorNegate@ ... part creates the replacement mask.The filling is done with just the Inpaint[] command.

这篇关于使用PIL用附近的颜色填充空图像空间(又称修复)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 05:42