图像处理-opencv-python

一些笔记

1.cv2图像三通道是BGR,mxnet三通道是RGB

img = cv2.imread(path)

# mxnet三通道输入是严格的RGB格式,而cv2.imread的默认是BGR格式,因此需要做一个转换

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

2.压缩图片:


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 + "**/*.png", recursive=True)  # 搜索该目录下的所有jpg和png文件
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 + "文件错误,忽略")

3.转换成灰度图、二值图:

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来决定),所赋予的值,此处50是阈值,255是所赋予的值
        cv2.imwrite('{0}\\{1}'.format(target_file_path, i), im_fixed)



rgb_to_gray(target_file_path)

4.Python OpenCV实现鼠标画框:

来源:https://blog.csdn.net/guyuealian/article/details/88013421

来源:https://blog.csdn.net/weixin_38646522/article/details/86095200

我融合和以上两个网址的代码:


# 在图片上点击后显示点和坐标的一种方法(使用opencv python)
# get_pic_x_y()来源:https://blog.csdn.net/huzhenwei/article/details/82900715
# on_mouse()来源:https://blog.csdn.net/guyuealian/article/details/88013421
img = cv2.imread("./1.png")
def on_mouse(event, x, y, flags, param):
    global img, point1, point2, g_rect
    img2 = img.copy()
    if event == cv2.EVENT_LBUTTONDOWN:  # 左键点击,则在原图打点
        print("1-EVENT_LBUTTONDOWN")
        point1 = (x, y)
        print("point1:", point1)
        cv2.circle(img2, point1, 8, (0, 255, 0), thickness=2)  # cv2.circle(img2, point1, 8(这是圈的半径), (0, 255, 0), thickness=2(这是圈的粗细))
        cv2.imshow('image', img2)

    elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON):  # 按住左键拖曳,画框
        print("2-EVENT_FLAG_LBUTTON")
        cv2.rectangle(img2, point1, (x, y), (255, 0, 0), thickness=2)
        cv2.imshow('image', img2)

    elif event == cv2.EVENT_LBUTTONUP:  # 左键释放,显示
        print("3-EVENT_LBUTTONUP")
        point2 = (x, y)
        cv2.rectangle(img2, point1, point2, (0, 0, 255), thickness=2)
        cv2.imshow('image', img2)
        print("point1:{0} point2:{1}".format(point1, point2))
        if point1 != point2:
            min_x = min(point1[0], point2[0])
            min_y = min(point1[1], point2[1])
            width = abs(point1[0] - point2[0])
            height = abs(point1[1] - point2[1])
            g_rect = [min_x, min_y, width, height]
            cut_img = img[min_y:min_y + height, min_x:min_x + width]
            cv2.imshow('ROI', cut_img)
def get_pic_x_y():  # 运行这个函数,上面那个被这个调用
    cv2.namedWindow("image")
    cv2.setMouseCallback("image", on_mouse)
    cv2.imshow("image", img)
    while (True):
        try:
            cv2.waitKey(100)
        except Exception:
            cv2.destroyWindow("image")
            break
    cv2.waitKey(100)
    cv2.destroyAllWindow()

get_pic_x_y()

Debug

Bug1: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)

Bug2:

Traceback (most recent call last):
    cv.imshow("image", img)
cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:352: 
error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

solution:路径中不能有中文

参考网址:

python中opencv图像处理实验(一)---灰度变换

python-opencv函数总结之(一)threshold、adaptiveThreshold、Otsu 二值化

python中使用cv2读取显示保存图片

python读入图片,转化为array

import numpy as np
from PIL import Image
im = Image.open('./test.jpg')
im2 = np.array(im)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值