小编这次要给大家分享的是如何用python按照图像灰度值统计并筛选图片,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。
我就废话不多说了,大家还是直接看代码吧!
import PIL.Image
import numpy
import os
import shutil
def sum_right(path):
img = PIL.Image.open(path)
array = numpy.array(img)
num = array.sum(axis=0)
print(type(num))
res_left = 0
res_right = 0
for i in range(256,512):
res_right += num[i]
print(res_right)
if __name__ == '__main__':
dir2 = r"C:\Users\Howsome\Desktop\tst"
dir1 = r"C:\Users\Howsome\Desktop\AB"
names = os.listdir(dir1)
n = len(names)
print("文件数量",n)
res = 0
average_5 = 25565356
average_25 = 26409377
average_5_right = 10006019
#average_tmp = (average_25+average_5)//2
count = 0
#show(os.path.join(dir1, "uni4F6C.png"))
for i in range(n):
#取图片
img = PIL.Image.open(os.path.join(dir1,names[i]))
file = os.path.join(dir1,names[i])
rmfile = os.path.join(dir2,names[i])
array = numpy.array(img)
num = array.sum(axis=0)
res_right = 0
for i in range(256, 512):
res_right += num[i]
average_5_right += res_right/n
if res_right > average_5_right:
shutil.copyfile(file, rmfile)
os.remove(file)
count += 1
print(average_5_right)
print(count)
补充知识:python遍历灰度图像像素方法总结
啥也不说了,看代码吧!
import numpy as np
import matplotlib.pyplot as plt
import cv2
import time
img = cv2.imread('lena.jpg',0)
# 以遍历每个像素取反为例
# 方法1
t1 = time.time()
img1 = np.copy(img)
rows,cols = img1.shape[:2]
for row in range(rows):
for col in range(cols):
img[row,col] = 255 - img[row,col]
t2 = time.time()
print('方法1所需时间:',t2-t1)
# 方法2
t3 = time.time()
img2 = np.copy(img)
rows,cols = img2.shape[:2]
img2 = img2.reshape(rows*cols)
# print(img2)
for i in range(rows*cols):
img2[i] = 255-img2[i]
img2 = img2.reshape(rows,cols)
# print(img2)
t4 = time.time()
print('方法2所需时间:',t4-t3)
# 方法3
t5 = time.time()
img3 = np.copy(img)
# 使用多维迭代生成器
for (x,y), pixel in np.ndenumerate(img3):
img3[x,y] = 255-pixel
t6 = time.time()
print('方法3所需时间:',t6-t5)
测试结果:
方法1所需时间: 0.14431977272033691 方法2所需时间: 0.13863205909729004 方法3所需时间: 0.24196243286132812
看完这篇关于如何用python按照图像灰度值统计并筛选图片的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。