跟进问题利用Python库进行图像处理
我想使用多处理并行分析几个图像:
class SegmentationType(object):
DISPLAY_NAME = "invalid"
def __init__(self, filename, path):
self.filename = filename
self.path = path
self.input_data = None
self.output_data = None
def read_image(self):
self.input_data = cv2.imread(self.path + self.filename)[1]
def write_image(self):
cv2.imwrite(self.path + self.filename.split('.')[0] + '_' + self.DISPLAY_NAME + '.png', self.output_data)
def process(self):
# override in derived classes to perform an actual segmentation
pass
def start_pipeline(self):
self.read_image()
self.process()
self.write_image()
class HSV_Segmenter(SegmentationType):
DISPLAY_NAME = 'HSV'
def process(self):
source = rgb_to_hsv(self.input_data)
self.output_data = treshold_otsu(source)
class LabSegmenter(SegmentationType):
DISPLAY_NAME = 'LAB'
def process(self):
source = rgb_to_lab(self.input_data)
self.output_data = global_threshold(source)
segmenter_class = {
'hsv': HSV_Segmentation,
'lab': LAB_Segmenter
}.get(procedure)
if not segmenter_class:
raise ArgumentError("Invalid segmentation method '{}'".format(procedure))
for img in images:
os.chdir(img_dir)
processor = = segmenter_class(img, img_dir, procedure)
processor.start_pipeline()到目前为止我尝试过的是:
image_lst = os.listdir(my_image_path)
# We split the list into sublist with 5 elements because of 512 GB RAM limitation
if len(image_lst) > 4:
nr_of_sublists = (int(len(image_lst)/2.5))
image_sub_lst =(np.array_split(image_lst, nr_of_sublists))
else:
image_sub_lst = [image_lst]
# We do the analysis for each sublist
for sub_lst in image_sub_lst:
print (sub_lst)
pool = multiprocessing.Pool(8)
# Call the processor
processor = = segmenter_class(img, img_dir, procedure)
processor.start_pipeline()
# How to call map???
pool.map(?, sub_lst)
pool.terminate()发布于 2020-07-05 18:57:33
版本的警告
下面的许多建议假设您使用的是Python 3。
class SegmentationType(object):可以是
class SegmentationType:DISPLAY_NAME = "invalid"不应该真正分配一个值。相反,
DISPLAY_NAME: strimread文献资料非常无助:它说imread返回"retval“。考虑到您的用法,很明显现实更加复杂,因为您正在对其进行索引。试着拆开包装:
_, self.input_data = cv2.imread(self.path + self.filename)process应该raise NotImplementedError在基础上。
您有一个工厂字典,应该转换成一个方法,如下所示
def get_segmenter(name: str) -> Type[SegmentationType]:
return {
t.DISPLAY_NAME: t
for t in (HSVSegmenter, LABSegmenter)
}[name]这两种情况都没有:
nr_of_sublists = (int(len(image_lst)/2.5))
image_sub_lst =(np.array_split(image_lst, nr_of_sublists))需要外括号。
这肯定是打错了?这将不会运行:
processor = = segmenter_class(img, img_dir, procedure)这一点也不会:
pool.map(?, sub_lst)https://codereview.stackexchange.com/questions/244920
复制相似问题