简体   繁体   English

如何在 python 中加快此图像蒙版创建过程?

[英]How can I speed up this image mask creation process in python?

I need to create masks for 100.000 images, this code runs on cpu and creates ~500 masks a hour.我需要为 100.000 个图像创建蒙版,此代码在 cpu 上运行并每小时创建约 500 个蒙版。 Is there a way I can speed this up either by parallelising or running code on gpu?有没有办法通过在 GPU 上并行化或运行代码来加快速度? I'm okay with solutions that make me heavily rewrite code as long as it speeds up the process.我可以接受让我大量重写代码的解决方案,只要它加快进程。

I tried compiling opencv library myself with cuda support, however I couldn't get most of cv2 methods I use here to run on gpu.我尝试使用 cuda 支持自己编译 opencv 库,但是我无法获得我在这里使用的大部分 cv2 方法来在 gpu 上运行。

This is my code这是我的代码

Edit #1编辑 #1

Added import list and comments to code.向代码添加了导入列表和注释。

Added input and output images.添加了输入和输出图像。

import cv2
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import glob
import sys
import os
import skimage.color
import skimage.filters
import skimage.io
import skimage.viewer


grayScale = cv2.imread(filename,cv2.IMREAD_REDUCED_GRAYSCALE_4)#read image as grayscale with size reduction

kernel = cv2.getStructuringElement(1,(17,17))

blackhat = cv2.morphologyEx(grayScale, cv2.MORPH_BLACKHAT, kernel)

ret,thresh2 = cv2.threshold(blackhat,10,255,cv2.THRESH_BINARY)

dst = cv2.inpaint(newimg,thresh2,1,cv2.INPAINT_TELEA)  #4 lines above are used to remove hair from image

mask = np.zeros(dst.shape[:2],np.uint8)

h,w,c = dst.shape

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

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

rect = (int(0.1*w),int(0.1*h),int(0.8*w),int(0.8*h))

cv2.grabCut(dst,mask,rect,bgdModel,fgdModel,1,cv2.GC_INIT_WITH_RECT) #removes some background from image
#code for k means clustering starts here

mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')

dst = dst*mask2[:,:,np.newaxis]

vectorized = dst.reshape((-1,3))

vectorized = np.float32(vectorized)

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) #11 lines above are used to remove some background from image

K = 4 
attempts=1

  ret,label,center=cv2.kmeans(vectorized,K,None,criteria,attempts,cv2.KMEANS_PP_CENTERS)

center = np.uint8(center)

labels = label.flatten()

res = center[label.flatten()]

result_image = res.reshape((dst.shape)) #k means clustering ends here

gray = cv2.cvtColor(result_image, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(gray, 10, 20, cv2.THRESH_BINARY)

result_image[thresh == 0] = 255

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))

erosion = cv2.erode(result_image, kernel, iterations = 1)

blur = skimage.color.rgb2gray(erosion)

blur = skimage.filters.gaussian(blur, sigma=float(1)) 

histogram, bin_edges = np.histogram(blur, bins=256, range=(0, 1))

index = next((i for i, x in enumerate(histogram) if x), None)

mask = blur > bin_edges[index+1] #10 lines above are used to create mask

mask = abs(mask-255) #inverts mask

array = np.array(mask, dtype='uint8') 

finimg = cv2.resize(array,None,fx=4.0,fy=4.0) #returns image to original size

plt.imsave("Masks/"+filename, finimg, cmap = plt.cm.gray) #saves result image

input image - skin mole image output image - mask of skin mole输入图像 - 皮肤痣图像输出图像 - 皮肤痣面具

You might try using kmeans processing in Python/Opencv as a first step.您可以尝试在 Python/Opencv 中使用 kmeans 处理作为第一步。 Then get the inner contour and use that for your mask.然后获取内部轮廓并将其用于您的面罩。 Draw the inner contour as white filled on a black background.将内部轮廓绘制为黑色背景上的白色填充。 You may need to use morphology to clean the kmeans results first您可能需要先使用形态学来清理 kmeans 结果

Input:输入:

在此处输入图片说明

Kmeans 2: Kmeans 2:

在此处输入图片说明

Kmeans 3: Kmeans 3:

在此处输入图片说明

Kmeans 4: Kmeans 4:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM