python模糊图片过滤的方法

yipeiwu_com6年前Python基础

最近在做人脸识别清洗样本的工作,发现经过人脸对齐后存在部分图片十分模糊,所以用opencv滤了一下。

原理就是使用了cv2.Laplacian()这个方法,代码如下。图片越模糊,imageVar的值越小,图像越模糊。

#-*-coding:utf-8-*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import os
import cv2
import shutil

THRESHOLD = 30.0

dst_root = r'/media/unionpay/0009FFAB000A9861/CASIA&KFZX_CLEAR'
for fpath, dirs, fs in os.walk('/media/unionpay/0009FFAB000A9861/CASIA&KFZX'):
 i = 0
 for dir in dirs:
  i += 1
  if i%100 == 0:
   print (str(i)+'folders processed current:'+dir)
  abs_dir = os.path.join(fpath, dir)
  for _, __, fs in os.walk(abs_dir):
   clear_img_list = []
   for f in fs:
    item = os.path.join(_, f)
    image = cv2.imread(os.path.join("/media/unionpay/0009FFAB000A9861/CASIA&KFZX/0000447", item))
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    imageVar = cv2.Laplacian(gray, cv2.CV_64F).var()
    if not imageVar < THRESHOLD:
     clear_img_list.append(item)
   dst_folder = os.path.join(dst_root, dir)
   if len(clear_img_list) >= 15:
    if not os.path.exists(dst_folder):
     os.mkdir(dst_folder)
    for item in clear_img_list:
     dst_path = os.path.join(dst_folder, item.split('/')[-1])
     shutil.copy(item, dst_path)

以上这篇python模糊图片过滤的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

用Python抢过年的火车票附源码

用Python抢过年的火车票附源码

前言:大家跟我一起念,Python大法好,跟着本宝宝用Python抢火车票 首先我们需要splinter 安装: pip install splinter -i http://pyp...

python多行字符串拼接使用小括号的方法

多行字符串拼接使用小括号 s = ('select *' 'from atable' 'where id=888') print s, type(s) #输出 select...

CentOS 6.X系统下升级Python2.6到Python2.7 的方法

第一步:升级python CentOs 6.x的系统默认安装的Python版本是2.6.x,想升级到Python2.7.x,从官方下载源文件,然后解压、编译 wget http:...

pytorch 使用单个GPU与多个GPU进行训练与测试的方法

如下所示: device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")#第一行代码 model.t...

python文件处理fileinput使用方法详解

这篇文章主要介绍了python文件处理fileinput使用方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一、介绍 fil...