【问题标题】:Python : Get features of several imagesPython:获取多个图像的特征
【发布时间】:2021-02-17 04:32:45
【问题描述】:

我想获取位于同一文件夹中的多个图像的特征。

我的代码如下 - 先决条件(需要库):

import numpy as np
from PIL import Image
import glob
import cv2
import os

图片所在文件夹的定义(6000左右)

images_dir = "TrainImages"

创建定义不同变量并计算它们的函数

def get_data_from_image(image_path):
    cv_img = cv2.imread(image_path)
    (means, stds) = cv2.meanStdDev(cv_img)
    stats = np.concatenate([means, stds]).flatten()
    image_features_list = [stats.tolist()]
    return image_features_list

创建一个扫描和分析图像的变量

image_files = [x.path for x in os.scandir(images_dir)]

循环函数的创建

i = 0
mylist =[]

for i in range (4): # I test only 4 images, could be more
    mylist.append((get_data_from_image(image_files[i])))

运行的东西

image_features_list = get_data_from_image(image_files[i])

看输出

image_features_list

输出只提供一张图片的特征而不是文件夹中的所有图片

[输出]:

[[114.31548828125001, 139.148388671875, 139.57832682291667, 50.54138521536725, 53.82290182999255, 51.946187641459595]]

如果我能找到一个关于如何拥有所有图像(不仅仅是一个)特征的解决方案,我将不胜感激。在这种情况下,请毫不犹豫地更正代码。 谢谢和最诚挚的问候

在友好人士的一些评论之后,这里有一个附加信息,供那些对响应感兴趣的人使用:要查看的输出是 mylist。

mylist

[输出]:

[[[144.28788548752834, 151.28145691609978, 148.6195351473923, 51.50620316379085, 53.36979275398226, 52.2493589172815]], [[56.220865079365076, 59.99653968253968, 60.28386507936508, 66.72797279655177, 65.24673515467009, 64.93141350917332]], [[125.2066064453125, 118.1168994140625, 145.0827685546875, 68.95463582009148, 52.65138276425348, 56.68269683130363]], [[114.31548828125001, 139.148388671875, 139.57832682291667, 50.54138521536725, 53.82290182999255, 51.946187641459595]]]

感谢您的帮助。这是一个很棒的论坛!

【问题讨论】:

  • 请编辑您的代码部分
  • 当您打印 mylist 时会看到什么?为什么在你有 6000 张图片的时候范围是 4?
  • 您好,感谢您的提问,我做了4个范围,只是为了测试并查看结果。我肯定会放超过 4 个,以获得所有图像特征。最诚挚的问候
  • 我还编辑了代码部分,使其更易于阅读,感谢 Cyrex 发现了这一点。祝你有美好的一天
  • 感谢 Cyrex,实际上,要查看的输出确实在 mylist 中。我确实拥有所有功能....

标签: python image feature-extraction


【解决方案1】:

试试这个方法,告诉我它是否成功

import os, os.path
import numpy as np
from PIL import Image
import cv2


def get_data_from_image(image_path):
    cv_img = cv2.imread(image_path)
    (means, stds) = cv2.meanStdDev(cv_img)
    stats = np.concatenate([means, stds]).flatten()
    image_features_list = [stats.tolist()]
    return image_features_list

images_dir = 'C:\\Users\\User\\Directory\\TrainImages\\'

images_names = []
 
with os.scandir(images_dir) as dirs:
    for entry in dirs:
        images_names.append(entry.name)


for image in images_names:

    path = images_dir + image

    image_features_list =  get_data_from_image(path))

    print(image_features_list) 

【讨论】:

  • 嗨 Cyrex,这是为了尝试。我有以下错误:错误:OpenCV(4.4.0)/tmp/pip-req-build-vu_aq9yd/opencv/modules/core/src/mean.dispatch.cpp:516:错误:(-215:断言失败) !_src.empty() 在函数 'meanStdDev'
  • 您是否将 images_dir 更改为您的实际目录?
  • 代码已更新,它应该完全按照它的描述进行。否则,如果不成功,请告诉我。
猜你喜欢
  • 2018-11-27
  • 2017-09-12
  • 1970-01-01
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-26
相关资源
最近更新 更多