Remove white background from the image and make it transparent using PHP

前端 未结 1 1907
我在风中等你
我在风中等你 2021-01-27 20:19

I got this code to do that:

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


        
相关标签:
1条回答
  • 2021-01-27 21:00

    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:

    0 讨论(0)
提交回复
热议问题