程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了OpenCV Python:3通道float32图像读取的快速解决方案?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决OpenCV Python:3通道float32图像读取的快速解决方案??

开发过程中遇到OpenCV Python:3通道float32图像读取的快速解决方案?的问题如何解决?下面主要结合日常开发的经验,给出你关于OpenCV Python:3通道float32图像读取的快速解决方案?的解决方法建议,希望对你解决OpenCV Python:3通道float32图像读取的快速解决方案?有所启发或帮助;

使用这种方法,可能没有太多的事情可以加快图像读取速度。我以为Matplotlib可能会更快,因为它直接以浮点数和RGB顺序读取,但是即使转换类型和通道顺序后,它也比OpenCV慢三倍。PIL比Matplotlib快一点,但仍然是OpenCV的两倍,因此无济于事,并且scikit- image的速度与PIL大致相同:

import matplotlib.image as mpimg
import cv2
import numpy as np
from skimage import io
from PIL import Image

import timeit
times = range(1000)

# matplotlib
start_time = timeit.default_timer()
for t in times:
    img = mpimg.imread('img1.png')
print("mpimg.imread(): ", timeit.default_timer() - start_time, "s")

# OpenCV
start_time = timeit.default_timer()
for t in times:
    img = cv2.cvtcolor(
        cv2.imread('img1.png'), cv2.color_BGR2RGB).astype(np.float32)/255.0
print("cv2.imread(): ", timeit.default_timer() - start_time, "s")

# scikit-image
start_time = timeit.default_timer()
for t in times:
    img = io.imread('img1.png').astype(np.float32)/255.0
print("io.imread(): ", timeit.default_timer() - start_time, "s")

# PIL
start_time = timeit.default_timer()
for t in times:
    img = np.asarray(Image.open('img1.png')).astype(np.float32)/255.0
print("Image.open(): ", timeit.default_timer() - start_time, "s")

相反,最好通过读取所有图像并将其保存为更好的格式以进行读取(即直接从字节读取)进行预处理,而不是使用图像读取器。您可以将图像序列化(刺入)到.p.pickle文件中,然后将数据直接加载到列表中。这样,您只需要一次执行一次缓慢加载即可。正如DanMašek在下面的注释中指出的那样,对文件进行腌制意味着将其解压缩为原始数据,因此文件大小 大得多。您可以使用正确的类型和频道顺序创建与现在相同的列表(缓冲区),然后对列表进行腌制;培训时间到了,您可以加载泡菜文件;它的 方式 更快,超级简单:

with open(Training_file, mode='rb') as f:
    Training_data = pickle.load(f)

解决方法

我需要类型为3通道RBG排序的彩色图像,float32其值在[0.0,1.0]每个彩色通道的间隔中。

这是我目前的解决方案:

def read_images(imagelist):
    buffer = list()
    for f in imagelist:
        # load single image,convert to float32
        img = cv2.imread(f).astype(np.float32)
        # changE interval from [0,255] to [0.0,1.0]
        img /= 255.0
        # leave out alpha chAnnel,if any
        if img.shape[2] == 4:
           img = img[:,:,0:3]
        buffer.append(img)
    return np.array(buffer)

此后,在图像处理程序中,我改变BGR到RGB排序(因为cv2imread默认读取BGR顺序图像)。

对于大型图像集,此过程非常耗时:我正在加载数千张图像进行预处理,然后将图像提供给TensorFlow中实现的某些神经网络。

有没有办法改善这种方法的性能?

大佬总结

以上是大佬教程为你收集整理的OpenCV Python:3通道float32图像读取的快速解决方案?全部内容,希望文章能够帮你解决OpenCV Python:3通道float32图像读取的快速解决方案?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。