91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 3.25KB
    文件類型: .rar
    金幣: 1
    下載: 0 次
    發(fā)布日期: 2024-05-11
  • 語言: Python
  • 標(biāo)簽: 透明背景??png??透明??

資源簡介

去除圖像中的白色背景,得到透明背景的保留主體的png圖像的python代碼;

資源截圖

代碼片段和文件信息

import?cv2
import?numpy?as?np
import?os?sys

def?CropImage(img?x?y):
????minx?=?x
????maxx?=?0
????miny?=?y
????maxy?=?0
????for?i?in?range(x):
????????for?j?in?range(y):
????????????if?img[ij3]?!=?0:
????????????????minx?=?min(minx?i)
????????????????maxx?=?max(maxx?i)
????????????????miny?=?min(miny?j)
????????????????maxy?=?max(maxy?j)

????ret?=?img[minx:maxx+1?miny:maxy+1]
????return?ret?maxx-minx+1?maxy-miny+1

def?remove_white_bg_as_contours(img_list?output_dir):
????#print(img_list)
????for?img_path?in?img_list:
????????#print(img_path)
????????img?=?cv2.imread(img_path)
????????if?img?is?None:
????????????print(img_path)
????????????continue
????????##?(1)?Convert?to?gray?and?threshold
????????gray?=?cv2.cvtColor(img?cv2.COLOR_BGR2GRAY)
????????th?threshed?=?cv2.threshold(gray?230?255?cv2.THRESH_BINARY_INV)

????????##?(2)?Morph-op?to?remove?noise
????????kernel?=?cv2.getStructuringElement(cv2.MORPH_ELLIPSE?(1111))
????????morphed?=?cv2.morphologyEx(threshed?cv2.MORPH_CLOSE?kernel)

????????##?(3)?Find?the?max-area?contour
????????cnts?=?cv2.findContours(threshed?cv2.RETR_EXTERNAL?cv2.CHAIN_APPROX_SIMPLE)[-2]
????????whc=img.shape
????????img=np.transpose(img[201])
????????z?=?np.ones([1wh]).astype(img.dtype)*255
????????img?=?np.vstack((imgz))
????????img?=?np.transpose(img[120])

????????stencil?=?np.zeros([wh4]).astype(img.dtype)
????????color?=?[255?255?255255]
????????cv2.fillPoly(stencil?cnts?color)
????????result?=?cv2.bitwise_and(img?stencil)
????????output_path?=?os.path.join(output_dir?os.path.split(img_path)[-1]+‘.png‘)
??

評論

共有 條評論

相關(guān)資源