我对python非常陌生,在并行化算法的一部分时遇到了问题。 考虑一个输入图像,它需要在像素级以某种方式设置阈值。由于算法只考虑一个特定的区域来计算阈值,所以我想在一个单独的线程/进程中运行图像的每个块。这就是我被困的地方。我找不到这些线程在同一个图像上工作的方法,也找不到如何将结果合并到新图像中。 因为我通常来自java世界,所以我通常会解决我不想干涉其他线程的问题。因此,我只是试图通过每个进程的形象。你知道吗
def thresholding(img):
stepSize = int(img.shape[0] / 10)
futures = []
with ProcessPoolExecutor(max_workers=4) as e:
for y in range(0, img.shape[0], stepSize):
for x in range(0, img.shape[1], stepSize):
futures.append(e.submit(thresholdThread, y, x, img))
concurrent.futures.wait(futures)
return img
def thresholdThread(y, x, img):
window_size = int(img.shape[0] / 10)
window_shape = (window_size, window_size)
window = img[y:y + window_shape[1], x:x + window_shape[0]]
upper_bound, lower_bound, avg = getThresholdBounds(window, 0.6)
for y_2 in range(0, window.shape[0]):
for x_2 in range(0, window.shape[1]):
tmp = img[y + y_2, x + x_2]
img[y + y_2, x + x_2] = tmp if (tmp >= upper_bound or tmp <= lower_bound) else avg
return str(avg)
就我对python的理解而言,这是行不通的,因为每个进程都有自己的img
副本。但是由于img是numpy的floats类型ndarray,我不知道是否以及如何使用所描述的共享对象here。你知道吗
仅供参考:我使用的是python3.6.9。我知道3.7已经发布了,但是重新安装所有的东西以便我可以使用spyder和openCV并不是那么容易。你知道吗
您没有利用任何Numpy的矢量化技术,这些技术可以显著减少处理时间。我假设这就是为什么你想在窗口/图像块上进行多进程操作-我不知道Docker是什么,所以我不知道这是否是多进程方法的一个因素。你知道吗
这里是一个矢量化的解决方案,警告可能会从操作中排除底部和右边缘像素。如果不能接受,无需进一步阅读。你知道吗
示例中的右和下边缘窗口的大小很可能与其他窗口不同。看起来你任意选择了一个因子10来分割你的图像-如果10是一个任意的选择,你可以很容易地优化底部和右边缘的三角洲-我会在答案的末尾发布这个函数。你知道吗
图像需要重塑为面片,以矢量化操作。我使用了^{} function
sklearn.feature_extraction.image._extract_patches
,因为它很方便,并且允许创建不重叠的补丁(这似乎是您想要的)。注意下划线前缀-它以前是一个公开的函数image.extract_patches
,但现在已经被弃用了。这个函数使用numpy.lib.stride_tricks.as_strided
—可能只是reshape
数组,但我没有尝试过。你知道吗设置
定义面片大小(参见下面的
opt_size
)并重塑图像。你知道吗patches
是原始数组的一个视图,它将在原始数组中看到更改。它的形状是(8, 9, 608, 394, 3)
有8x9
,(608,394,3)
个窗口/补丁。你知道吗找到每个面片的上下界;将每个像素与其面片的边界进行比较;为每个在其边界之间并且需要更改的像素提取索引。你知道吗
找到每个面片的平均值,然后更改所需的像素值
将所有功能组合在一起的函数
测试你的天真过程-我希望我执行正确。在4864x3546彩色图像的基础上改进了35倍。可能还有进一步的优化,也许一些向导会对此发表评论。你知道吗
使用块系数10进行测试:
indices
是一个index array。它是数组的元组,每个维度一个。indices[0][0],indices[1][0],indices[2][0]
将是3d数组中一个元素的索引。完整元组可用于索引数组的多个元素。你知道吗np.logical_and(patches > lower, patches < upper)
返回布尔数组,^{True
的所有元素的索引。你知道吗相关问题 更多 >
编程相关推荐