如何使用 OpenCV 使用多处理并行处理图像?

2024-01-11

我的文件夹中有一组图像,我想使用一些 OpenCV 函数对其进行预处理。功能

detectAndaligncrop

使用 OpenCV 对其图像路径进行预处理并返回输出图像。 我可以使用以下方法来做到这一点:

for image_path in files_list:
   cropped_image, _=detectAndaligncrop(im)
   cv2.imwrite("ouput_folder/{}".format(os.path.basename(image_path)),cropped_im*255.)

但这不起作用:

jobs=[]
for im_no, im in enumerate(files_list):
    p=multiprocessing.Process(target=saveIm,args=[im])
    jobs.append(p)
    p.start()
for j in jobs:
    j.join()

其中 saveIm 是:

im,lm=detectAndaligncrop(im_path)
        fname="output_path/cropped2/{}".format(os.path.basename(im_path))
        cv2.imwrite(fname,im)

我已经验证它调用了 detectorAndaligncrop 函数,但不处理从该行开始的图像

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

在 detectorAndaligncrop 内部调用,因为每个图像都会调用“before cvtColor”,而“after cvtColor”则不会:

def detectAndaligncrop(impath):
    image=cv2.imread(impath)
    image_float=np.float32(image)/255.0
    print ("before cvtcolor")
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    print ("after cvtcolor")
    return gray, 1

另外,我尝试过:

with ThreadPoolExecutor(max_workers=32) as execr:
    res=execr.map(saveIm,files_list)

这可以工作,但并不比简单地运行 for 循环更快。是因为GIL吗?


我需要一种多重处理方法来预处理图像,然后再将其输入神经网络。我发现这个页面叫做令人尴尬的并行 for 循环 https://pythonhosted.org/joblib/parallel.html其中对数组/列表中的元素并行运行数学任务。我想知道这是否可以扩展到图像(毕竟图像只不过是数组,大的 3D 数组!)

我决定执行添加加权 https://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#addweighted从 OpenCV 到图像集合的操作。使用此操作,您可以对两个图像应用不同的权重并将它们相加。它用于尽可能混合图像see here https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_core/py_image_arithmetics/py_image_arithmetics.html#image-blending

我执行了这个功能with and withoutjoblib 在我的桌面上查找一组图像并比较它们的性能。最后我提到了图像的数量以及所使用的这些图像的集体大小。

Code:

import os
import time

#--- Importing the required library ---
from joblib import delayed

#--- Choosing all available image formats of images from my desktop ---
path = r'C:\Users\Jackson\Desktop'
img_formats = ['.png', '.jpg', '.jpeg']

#--- Defining the addWeighted function from OpenCV ---
def weight(im):
    addweighted = cv2.addWeighted(im, 0.7, cv2.GaussianBlur(im, (15, 15), 0), 0.3, 0)
    return addweighted


#--- Using joblib library-----
start_time = time.time()

new_dir = os.path.join(path, 'add_Weighted_4_joblib')
if not os.path.exists(new_dir):
    os.makedirs(new_dir)

def joblib_loop():
    for f in os.listdir(path):
        if any(c in f for c in img_formats):
            img = cv2.imread(os.path.join(path, f))
            r = delayed(weight)(img)
            cv2.imwrite(os.path.join(new_dir, f + '_add_weighted_.jpg'), r)

elapsed_time = time.time() - start_time
print('Using Joblib : ', elapsed_time)

#--- Without joblib ---
start_time = time.time()

#--- Check whether directory exists if not make one
new_dir = os.path.join(path, 'add_Weighted_4')
if not os.path.exists(new_dir):
    os.makedirs(new_dir)

for f in os.listdir(path):
    if any(c in f for c in img_formats):
        img = cv2.imread(os.path.join(path, f))
        r = weight(img)
        cv2.imwrite(os.path.join(new_dir, f + '_add_weighted_.jpg'), r)

elapsed_time = time.time() - start_time
print('Without Joblib : ', elapsed_time)

这是我得到的结果:

('Using Joblib : ', 0.09400010108947754)
('Without Joblib : ', 15.386000156402588)

正如你所看到的使用joblib疯狂地加快操作速度!

现在让我向您展示我的桌面上有多少张图像以及它们的总大小是多少:

overall_size = 0
count = 0
#for f in os.listdir(path):
for  f in os.listdir(path):
    if any(c in f for c in img_formats):
        img = cv2.imread(os.path.join(path, f))
        overall_size+= img.size
        count+= 1

print('Collective Size of all {} images in the predefined path is {} MB'.format(count, overall_size/10**6))

结果:

Collective size of all 14 images in the predefined path is 58 MB
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 OpenCV 使用多处理并行处理图像? 的相关文章

随机推荐

  • Netbeans:编译时将文本文件移动到 dist 文件夹中

    我有一个文本文件 假设textfile txt 存储在项目文件夹中网豆 7 3 e g project folder textfile txt src package package subpackage MyClass java 当我编译
  • 在构造函数中初始化静态最终字段

    public class A private static final int x public A x 5 final意味着变量只能分配一次 在构造函数中 static意味着它是一个类实例 我不明白为什么这会被禁止 这些关键词在哪里互相干
  • 用笑话模拟第 3 方库构造函数

    我正在用玩笑编写单元测试 并且必须测试一个从第三方库调用构造函数的函数 测试的目标是检查调用是否使用了良好的参数 第 3 方库是 Popper js 我做了一个jest spyOn Popper prototype constructor
  • 设计管理的会话不会传播到子域

    我正在使用 Devise 来管理 Rails 3 1 应用程序中的身份验证 它在我的生产服务器中运行得很好 但我刚刚设置了一个新的测试服务器 如果我登录主站点 访问子域无法识别会话 它要求我重新登录 我不记得在哪里可以解决此信息的问题 看起
  • 如何在 Azure 广告 B2C 上使用自定义角色?

    我的 API 需要三种类型的用户 我想使用自定义角色定义来管理它 是否可以在 Azure B2c 上创建角色 然后通过 Microsoft Graph API 将这些角色分配给用户 我正在朝着同一个目标努力 所以这是我到目前为止发现的 将自
  • 如何在C中初始化char **?

    我对 C 还很陌生 我应该做一个简单的单词搜索谜题 所以对于 字典 我做了 char dictionary DOG ELEPHANT CAT ETC 但是当我尝试编译时 我收到一条警告 提示字典中的每个单词 标量初始值设定项中存在多余元素
  • MVC 视图的命名空间问题 - Razor 引擎

    我将 System Web DataVisualization 的引用添加到我的 MVC 项目中 现在 当我尝试将命名空间添加到 web config 时 出现错误 CS0234 命名空间 System Web UI 中不存在类型或命名空间
  • Intel芯片上的半精度浮点运算

    Intel芯片上可以进行半精度浮点运算吗 我知道如何加载 存储 转换半精度浮点数 1 但我不知道如何在不转换为单精度浮点数的情况下对它们进行加 乘 1 https software intel com en us articles perf
  • Flutter doctor - Windows 版本(无法确认安装的 Windows 版本是否为 10 或更高版本)

    我刚刚在主频道上将 Flutter 表单 3 5 0 12 0 pre 168 更新为 3 6 0 1 0 pre 35 我在主通道上 因为在稳定通道上我在键盘输入方面遇到了麻烦 但这不应该与这个 问题 有任何关系 实际上这不是一个真正的问
  • 操作完成后如何重置 EditText?

    我想在按下按钮后将我的 EditText 重置回空的 空间 或 提示 该按钮将使用 EditText 字段的输入完成活动 我与 Android 的冒险就这样开始了 干杯 谢谢 SEND SMS btnSendSMS Button findV
  • 将 ffmpeg 与 Python 2.7 结合使用

    我一直尝试在 Python 2 7 中安装 pyffmpeg 但没有成功 我找到了一个 Python 2 6 的包 但我无法让它工作 所以 我一直在考虑2 7 我在这个网站上看过其他人之前的帖子 但他们没有帮助 有人对此有经验吗 最终 我想
  • Flexbox 中的align-self 属性不起作用?

    我试图理解弹性盒 我想让 第一个 块拉伸以匹配浏览器的整个宽度 并使 第二个 块固定大小并左对齐 所以我用了align items flex end在父级 并尝试使用拉伸第一个块align self stretch在 第一个 块中 这是行不
  • 如何使 Angular.js 应用程序在服务器同步可用时离线

    我正在寻找一种方法或一个库来处理这个问题 这个想法是将数据存储在本地存储中 并在连接可用时同步到后端 http pouchdb com http pouchdb com 看起来是一个很酷的解决方案 但有人在生产中使用过它吗 UPDATE 现
  • 具有多个 LIKE 的选择命令

    功能性 用户将文本输入到文本框A中 在数据库记录中搜索与用户输入相对应的名字和姓氏 问题 我得到的结果似乎只是搜索 名字 字段 而不是名字和姓氏字段 Example 搜索 Mike 返回 迈克 史密斯 迈克 琼斯 搜索 琼斯 Change
  • Python 中二维多项式的“polyfit”等价物

    我想找到一个最小二乘解a中的系数 z a0 a1 x a2 y a3 x 2 a4 x 2 y a5 x 2 y 2 a6 y 2 a7 x y 2 a8 x y 给定数组x y and z长度为 20 基本上我正在寻找相当于numpy p
  • C++ 类型的解引用迭代器

    我试图创建一个函数来对所有元素求和std vector template
  • 如何在 JavaScript 中检查空值?

    如何在 JavaScript 中检查空值 我写了下面的代码 但它不起作用 if pass null cpass null email null cemail null user null alert fill all columns ret
  • 从 C# 调用 C++ 函数

    我有以下 C 函数 void FillAndReturnString char someString char sourceString test someString new char 5 memcpy someString source
  • python中的递归函数如何存储和处理变量?

    我对下面的代码很困惑 def a x print x if x gt 0 a x 1 print x I am confused with this print statement a 5 上面的代码输出 5 4 3 2 1 0 0 1 2
  • 如何使用 OpenCV 使用多处理并行处理图像?

    我的文件夹中有一组图像 我想使用一些 OpenCV 函数对其进行预处理 功能 detectAndaligncrop 使用 OpenCV 对其图像路径进行预处理并返回输出图像 我可以使用以下方法来做到这一点 for image path in