从图像中删除白色背景,并使用PHP使其透明 [英] Remove white background from the image and make it transparent using PHP

查看:1043
本文介绍了从图像中删除白色背景,并使用PHP使其透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这样的代码:

  $ im = new Imagick(test.jpg); 
$ im-> paintTransparentImage($ im-> getImageBackgroundColor(),0,500);
$ im-> setImageFormat('png');
$ im-> writeImage('finish.png');

这就是结果(我手动添加粉红色背景以更好地查看问题):





当我增加模糊时然后更多白色像素在对象旁边消失,但随后更多的白色像素也在对象内消失。



我在网站上尝试了相同的图像结果如下:





更新:



添加 $ im - > despeckleimage(); $ im-> paintTransparentImage 之前产生更好的结果:





唯一需要做的就是用白色像素填充小空白区域。有没有办法做到这一点?

解决方案

我使用 GrabCut算法获得了即时解决方案。我使用OpenCV 3和python来获得你想要的输出。不幸的是我不知道 php 我也不知道 imagik



代码

 导入numpy为np 
import cv2

img = cv2.imread('Dress.jpg',1)
cv2..imshow('原始图片',img)
mask = np.zeros(img.shape [:2 ],np.uint8)

bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)

rect =(60,20,325,503)

cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
mask2 = np .where((mask == 2)|(mask == 0),0,1).astype('uint8')
nimg = img * mask2 [:,:,np.newaxis]

cv2.imshow(提取的图片,nimg)

cv2.waitKey()
cv2.destroyAllWindows()

我首先使用 mask 变量创建了一个黑色背景。



这是我获得的。





访问



我应用了阈值并获得了以下图像:





现在我执行了'形态学结束'操作并获得了这个:





最后,我使用您在开头t上传的原始图片屏蔽了上面的图片o获得这个:




I got this code to do that:

$im = new Imagick("test.jpg");
$im->paintTransparentImage($im->getImageBackgroundColor(), 0, 500);
$im->setImageFormat('png');
$im->writeImage('finish.png');

And this is the result (I added manually pink background to see the problems better):

When I increase the fuzz then more white pixels disappear next to the object but then more white pixels also disappear inside the object.

I tried the same image on a website and the result there is:

Which is pretty perfect. Does someone know how to do that?

In case someone need the original image for testing:

UPDATE:

Adding $im->despeckleimage(); before $im->paintTransparentImage makes a better result:

The only thing needs to be done is fill fill the small empty areas with white pixels. Is there any way to do that?

解决方案

I obtained an instant solution using the GrabCut Algorithm. I used OpenCV 3 with python to get your desired output. Unfortunately I do not know php nor do I know imagik.

CODE:

import numpy as np
import cv2

img = cv2.imread('Dress.jpg',1)
cv2..imshow('Original image',img)
mask = np.zeros(img.shape[:2],np.uint8)

bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)

rect = (60,20,325,503)

cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
nimg = img*mask2[:,:,np.newaxis]

cv2.imshow("Extracted image",nimg)

cv2.waitKey()
cv2.destroyAllWindows()

I first created a black background using the mask variable.

This is what I obtained.

Visit THIS PAGE for details of this algorithm and the parameters used. You can work this out for masking on a colored background also.

I beleive the same can be tried out with imagik also. Hope this helps :)

EDIT:

This is in response to the edit you made to the question.

The following was the image uploaded by you:

I applied threshold and obtained the following image:

Now I performed 'morphological closing' operation and obtained this:

Finally I masked the image above with the original image you uploaded in the beginning to obtain this:

这篇关于从图像中删除白色背景,并使用PHP使其透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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