图像块处理 [英] Image Block Processing
本文介绍了图像块处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的图像很大,我想一次逐块(512 x 512)对其进行处理.我目前正在使用循环执行此操作,但是我知道这不是执行此操作的最有效方法.我还能使用什么来使其更优化?
I have a large image, and I want to process it block by block (512 x 512) at a time. I am currently doing this with loops, but I know that it is not the most efficient way to do this. What else can I use to make it more optimized?
推荐答案
您可以将图像分为几个multiprocess.Array
,在不同的过程中分别处理每个图像,然后重建图像.
这里有一些玩具示例,可以帮助您入门:
You can divide the image into several multiprocess.Array
, process each one separately on a different process, then reconstruct the image.
Here some toy example to get you started:
from multiprocessing import Process, Lock
from multiprocessing.sharedctypes import Value, Array
import numpy as np
def processBlock(arr,i): #Image Processing function, i is an extra argument i used for the process.
block = np.frombuffer(arr.get_obj())
block = block.reshape((512,512,3))
#Some processing
block[:] = i
############
block = block.reshape((-1))
arr[:] = block
fullImage = np.zeros((512*2,512*2,3)) #Create full image of 0s
imageParts = []
imageParts.append(Array('d',fullImage[0:512,512:,:].flatten())) #Divide it into 4 parts
imageParts.append(Array('d',fullImage[0:512,0:512,:].flatten()))
imageParts.append(Array('d',fullImage[512:,0:512,:].flatten()))
imageParts.append(Array('d',fullImage[512:,512:,:].flatten()))
processes = []
for i in range(4): #Process each part simulatinously
p = Process(target = processBlock, args=(imageParts[i],i))
p.start()
processes.append(p)
for i in range(4): #Wait for all
processes[i].join()
#Reconstruct Image
fullImage[0:512,512:,:] = np.frombuffer(imageParts[0].get_obj()).reshape((512,512,3))
fullImage[0:512,0:512,:] = np.frombuffer(imageParts[1].get_obj()).reshape((512,512,3))
fullImage[512:,0:512,:] = np.frombuffer(imageParts[2].get_obj()).reshape((512,512,3))
fullImage[512:,512:,:] = np.frombuffer(imageParts[3].get_obj()).reshape((512,512,3))
这篇关于图像块处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文