从 1000 张图像池中读取 50 张图像作为每个像素矩阵



我有 1000 张尺寸为 800x600 像素的灰度图像。 每个像素都有一个值 0...255。

我想从这 1000 张图片中挑选出 50 张图片。 在这 50 张图像中,我想找出最大灰度值 (0...255(。 由此,我只想构建一个具有最大格拉斯泰值(0...255(的新图像。

最后,我用魔杖取 1000 张图像中的每一个,并将每个像素除以新图像像素并乘以 255。

我从选择前 50 张选择的图像开始:

from random import seed
from random import sample
# seed random number generator
seed(1)
# prepare a sequence
sequence = [i for i in range(1000)]
print(sequence)
# select a subset without replacement
subset = sample(sequence, 50)
print("Chosed 50 random images: ", subset)

然后我开始循环阅读图像。从 50 张拾取的图像中读出最大像素值:

for i in range(0,49):
for d in range(0, 599):
for s in range(0, 799):
print("Chosed image: ", subset[i], "Chosed pixel (rundownstairs): ", d, "Chosed pixel (run sidewise): ", s )   

但是我不知道读取矩阵中的像素并进行数学矩阵计算。

由于我没有你的图像,所以我生成了n随机图像。 我用这些图像做了一个数组,并找到了它们的坐标max值。然后我组织了这些索引,以便可以读取: 有了listOfCordinates,您应该能够开始使用像素进行操作。 请注意,我使用了numpy,我不知道这是否是一个问题。

import numpy as np
import cv2
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import glob, os
os.chdir("C:/Usersiareval1DocumentsOCLASSclass_def") #your folder
n = 5 #Number oof images
img_arr = [] #array with images
n_nad_files = []
for i in range(0,n):
n_nad_files.append(glob.glob("*.png")[np.random.randint(n)])
for file in n_nad_files: #set the file termination
img = cv2.imread(file, 0)
img_arr.append(img)

dim_x = img_arr[0].shape[0]
dim_y = img_arr[0].shape[1]
img_with_max = np.zeros((dim_x, dim_y))

cnt = 0
for image in img_arr:
print("Max values in image " +  str(cnt))
for x in range (0, dim_x):
for y in range (0, dim_y):
img_with_max[x][y] = max(img_with_max[x][y], image[x][y])
cnt += 1

如果要绘制图像,可以

plt.imshow(YOUR_IMAGE,cmap=cm.bone) #show your array with the selected colour
plt.show() #show the image

对于图像处理,我建议您opencv,您必须安装它。要读取灰度图像,您可以使用它,您将获得一个numpy数组:

import cv2
import numpy as np

img = cv2.imread('image_file_path', 0)

要查找数组中的最大值,您可以使用以下内容:

max_value = np.max(img)

对于将数学运算应用于整个数组,只需执行以下操作:

mod_img = img / max_value

最新更新