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死活没有反应的解决方法

我用brew安装python3 装完了发现 输入python3毫无反应,检查了 $PATH 也没有任何问题 这个时候回去看安装过程,发现安装时有一个错误: ERROR:The `b...

远程部署工具Fabric详解(支持Python3)

前言 如果你搜一圈 "Fabric "关键字,你会发现 90% 的资料都是过时的,因为现在 Fabric 支持 Python3,但是它又不兼容旧版 Fabric。所以,如果你按照那些教程...

Python之读取TXT文件的方法小结

方法一: <span style="font-size:14px;">#read txt method one f = open("./image/abc.txt")...

Python3实现的字典、列表和json对象互转功能示例

本文实例讲述了Python3实现的字典、列表和json对象互转功能。分享给大家供大家参考,具体如下: python3可以使用json模块操作json json.dumps(): 对jso...

Python tensorflow实现mnist手写数字识别示例【非卷积与卷积实现】

本文实例讲述了Python tensorflow实现mnist手写数字识别。分享给大家供大家参考,具体如下: 非卷积实现 import tensorflow as tf from t...