简体   繁体   English

如何使用循环将图像保存在python-opencv的不同文件夹中

[英]How to use a loop to save images in different folders in python-opencv

I have more than 20 images that I want to classify based on pixel correlation.我有 20 多张图像要根据像素相关性进行分类。 I am able to perform all the procedures but the problem is just saving the image into the corresponding class.我能够执行所有程序,但问题只是将图像保存到相应的 class 中。 Suppose that the pixel correlation values are [0.48, 0.20, 0.57, 0.53, 0.06, 0.52, 0.55, 0.57, 0.51, 0.49, ..., 0.25]假设像素相关值为[0.48, 0.20, 0.57, 0.53, 0.06, 0.52, 0.55, 0.57, 0.51, 0.49, ..., 0.25]

And I want to use each index of the above values based on some thresholds to classify the images into Normal , Abnormal and Ambiguous classes.我想根据一些阈值使用上述值的每个索引将图像分类为NormalAbnormalAmbiguous类。 With this snippet below, I achieved the following results however, The saved images are in an unsupported format使用下面的这段代码,我获得了以下结果,但是保存的图像格式不受支持

for filename in os.listdir(folder):
if filename.endswith('.jpg'):
    img = cv2.imread(os.path.join(folder, filename))
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
r = []
for correlation in range(len(correlation_matrices)):
    symmetry = correlation_matrices[correlation][0,1]
    r.append(symmetry)
#print(r)

threshold1 = 0.45
threshold2 = 0.35

for index in range(len(r)):

    
    if (rounded_value[index] >= threshold1):
        print('Normal Image')
        cv2.imwrite('./uniform_luminance/Normal' + str(index) + '.jpg', img[index])
    
    elif ((rounded_value[index] < threshold1) and rounded_value[index] >= threshold2):
        print('Ambiguous Image')
        cv2.imwrite('./ambiguous_luminance/Ambiguous' + str(index) + '.jpg', img[index])
    
    elif (rounded_value[index] < threshold2):
        print('Abnormal Image')
        cv2.imwrite('./non_uniform_luminance/Abnormal' + str(index) + '.jpg', img[index])

I achieved the following我实现了以下

The saved images are not similar to the input images however classified based on the thresholds保存的图像与输入图像不同,但基于阈值进行分类

While the input images as follows而输入图像如下

Are you asking how to correct the format of the saved images?您是否在问如何更正已保存图像的格式? Or why the saved images are different than your input ones?或者为什么保存的图像与您输入的图像不同?

In case you are asking the later:如果您要问后者:

Assuming your snippet is inside a loop that reads each image separately:假设您的代码段位于一个单独读取每个图像的循环中:

I believe your error is on img[index].我相信你的错误是在 img [index] 上。 You are saving the image as a column, including all of the rows thus creating an image as shown.您将图像保存为一列,包括所有行,从而创建如图所示的图像。 Keep in mind that an image is a 2d array and with [] you can specify which values you want to manipulate.请记住,图像是二维数组,使用 [] 您可以指定要操作的值。

Image[columns, rows]图像[列,行]

Since openvcv documentation says that imwrite takes parameters first the filename and second the image, change your code to:由于openvcv文档说 imwrite 首先采用文件名和图像的参数,因此将代码更改为:

cv2.imwrite('./uniform_luminance/Normal' + str(index) + '.jpg', img)

or或者

cv2.imwrite('./uniform_luminance/Normal' + str(index) + '.jpg', img[:,:])

img[:, :] the: is used to include all values. img[:, :] the: 用于包含所有值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM