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设计】。

相关文章

Python3模拟登录操作实例分析

Python3模拟登录操作实例分析

本文实例讲述了Python3模拟登录操作。分享给大家供大家参考,具体如下: 模拟登录_要求: 1. 用户输入账号密码进行登录 2. 用户信息保存在文件内 3. 用户密码输入错误三次后锁定...

基于python的selenium两种文件上传操作实现详解

基于python的selenium两种文件上传操作实现详解

方法一、input标签上传 如果是input标签,可以直接输入路径,那么可以直接调用send_keys输入路径,这里不做过多赘述,前文有相关操作方法。 方法二、非input标签上传 这种...

python读取一个目录下所有txt里面的内容方法

实例如下所示: import os allFileNum = 0 def printPath(level, path): global allFileNum ''''' 打印一...

Python与shell的3种交互方式介绍

概述 考虑这样一个问题,有hello.py脚本,输出”hello, world!”;有TestInput.py脚本,等待用户输入,然后打印用户输入的数据。那么,怎么样把hello.py输...

在Python中实现函数重载的示例代码

在Python中实现函数重载的示例代码

假设你有一个函数connect,它有一个参数address,这个参数可能是一个字符串,也可能是一个元组。例如: connect('123.45.32.18:8080') connec...