欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

opencv-python 图像预处理

程序员文章站 2022-07-13 08:53:23
...

压缩图片:


from PIL import Image
import os
from glob import glob

fpath = "./data/people/validation/image"  #压缩的目录
size = (512, 512)  # 定义要调整成为的尺寸(PIL会自动根据原始图片的长宽比来缩放适应设置的尺寸)
# glob.glob()用来进行模糊查询,增加参数recursive=True后可以使用**/来匹配所有子目录
files = glob(fpath + "**/*.jpg", recursive=True) + glob(fpath + "**/*.jpg", recursive=True)  # 如果输入的图是jpg,写jpg
total = len(files)  # 总文件数
cur = 1  # 当前文件序号
print("共有" + str(total) + "个文件,开始处理")
print("-----------------------------------")
for infile in files:
    try:
        # f, ext = os.path.splitext(infile) # 分离文件名和后缀
        print("进度:" + str(cur) + "/" + str(total) + "   " + infile)
        img = Image.open(infile)  # 打开图片文件
        if img.width > 100:
            img.thumbnail(size, Image.ANTIALIAS)  # 使用抗锯齿模式生成缩略图(压缩图片)
            img.save(infile, "jpeg")  # 保存成与原文件名一致的文件,会自动覆盖源文件,如果是jpg图写jpeg
        else:
            print(infile + "宽度小于1200px,无需处理,已忽略")
        cur = cur + 1

    except OSError:
        print(infile + "文件错误,忽略")

转换成灰度图、二值图:

import cv2, os

target_file_path = 'G:\\unet-master\\data\\people\\train\\image'

# 将彩色图片转换成灰色图片,不进行备份,直接覆盖
def rgb_to_gray(target_file_path):
    for i in os.listdir(target_file_path):
        # print('{0}\\{1}'.format(target_file_path, i))
        img = cv2.imread('{0}\\{1}'.format(target_file_path, i), cv2.IMREAD_GRAYSCALE)
        cv2.imwrite('{0}\\{1}'.format(target_file_path, i), img)

# 将灰色图片转换成伪彩色图片,好像用不了
def gray_to_rgb(target_file_path):
    for i in os.listdir(target_file_path):
        # print('{0}\\{1}'.format(target_file_path, i))
        img = cv2.imread('{0}\\{1}'.format(target_file_path, i), cv2.COLOR_GRAY2RGB)
        cv2.imwrite('{0}\\{1}'.format(target_file_path, i), img)



# 将彩色图片转换成黑白二值图片,不进行备份,直接覆盖
def to_binary(target_file_path):
    for i in os.listdir(target_file_path):
        # print('{0}\\{1}'.format(target_file_path, i))
        # gray = cv2.imread('{0}\\{1}'.format(target_file_path, i), cv2.IMREAD_GRAYSCALE)
        gray = cv2.imread('{0}\\{1}'.format(target_file_path, i))
        ret, im_fixed = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)  # thresh:阈值, maxval:当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值
        cv2.imwrite('{0}\\{1}'.format(target_file_path, i), im_fixed)



rgb_to_gray(target_file_path)

 

ERROR1:TypeError: Expected cv::UMat for argument 'src'

错误代码:

# 将彩色图片转换成黑白二值图片,不进行备份,直接覆盖,错误代码示例
def to_binary(target_file_path):
    for i in os.listdir(target_file_path):
        gray = '{0}\\{1}'.format(target_file_path, i)
        ret, im_fixed = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)
        cv2.imwrite('{0}\\{1}'.format(target_file_path, i), im_fixed)

solution:没有用cv2.imread(path)来读取图片

正确示例:

# 将彩色图片转换成黑白二值图片,不进行备份,直接覆盖
def to_binary(target_file_path):
    for i in os.listdir(target_file_path):
        gray = cv2.imread('{0}\\{1}'.format(target_file_path, i))
        ret, im_fixed = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)
        cv2.imwrite('{0}\\{1}'.format(target_file_path, i), im_fixed)

参考网址:

https://blog.csdn.net/yawdd/article/details/80180848

讲二值化的博客,讲得很好:

https://blog.csdn.net/sinat_21258931/article/details/61418681