opencv简单应用

该博客介绍了如何使用OpenCV库在Python中进行图像处理,包括将图像转换为HSV色彩空间,执行图像变换,过滤操作,提取图像中的直线、轮廓和特定区域,以及进行图像增强,如白平衡调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转换成HSV色彩空间

图像变换

图像过滤

提取直线、轮廓、区域

图像增强-白平衡等

import matplotlib.pyplot as plt


def plt_save(image, title=''):
    plt.imshow(X=image, cmap='gray')
    plt.title(title)
    file_path = './output/' + title + '.png'
    plt.savefig(file_path)
    print(file_path)
    pass


def plt_show(image, title=''):
    plt.imshow(X=image, cmap='gray')
    plt.title(title)
    plt.show()
#main.py
import cv2

from image_filtering import show_filtering
from image_outline import show_outline
from image_transformation import show_transformation
from utils import plt_save
from image_color import show_hsv
from image_enhancement import show_enhancement

if __name__ == '__main__':
    file_path = './images/000000507081.jpg'

    origin = cv2.imread(file_path)
    origin = origin[:, :, [2, 1, 0]]

    # x, y = origin.shape[0:2]
    # origin = cv2.resize(origin, (int(y / 3), int(x / 3)))
    plt_save(image=origin, title='Origin')

    # --------------------图像色彩--------------------
    # 转换成HSV色彩空间
    show_hsv(origin)

    # --------------------图像变换--------------------
    show_transformation(origin)

    # --------------------图像过滤--------------------
    show_filtering(origin)

    # --------------------提取直线、轮廓、区域--------------------
    show_outline(origin)

    # -------------------- 图像增强-白平衡等--------------------
    show_enhancement(origin)

    print('done')
#image_transformation.py
import cv2
import numpy as np

from utils import plt_save


def show_transformation(src):
    # 用形态学滤波器腐蚀和膨胀图像
    element_3x3 = np.ones((3, 3), np.uint8)

    # 腐蚀 3x3
    eroded = cv2.erode(src=src, kernel=element_3x3)
    plt_save(image=eroded, title='eroded')

    # 膨胀 3x3 3次
    dilated = cv2.dilate(src=src, kernel=element_3x3, iterations=3)
    plt_save(image=dilated, title='dilated 3 times')

    # 腐蚀 7x7
    element_7x7 = np.ones((7, 7), np.uint8)
    eroded_7x7 = cv2.erode(src=src, kernel=element_7x7, iterations=1)
    plt_save(image=eroded_7x7, title='eroded 7x7')

    # 腐蚀 3x3 3次
    eroded_3 = cv2.erode(src=src, kernel=element_3x3, iterations=3)
    plt_save(image=eroded_3, title='eroded 3 times')

    # 用形态学滤波器开启和闭合图像
    image_gray = cv2.cvtColor(src=src, code=cv2.COLOR_RGB2GRAY)
    # plt_save(image=image_gray, title='image_gray')

    # Close the image
    element_5x5 = np.ones((5, 5), np.uint8)

    closed = cv2.morphologyEx(src=image_gray, op=cv2.MORPH_CLOSE, kernel=element_5x5)
    plt_save(image=closed, title='closed')

    # Open the image
    opened = cv2.morphologyEx(src=image_gray, op=cv2.MORPH_OPEN, kernel=element_5x5)
    print(opened.shape)
    plt_save(image=opened, title='opened')

    closed = cv2.morphologyEx(src=image_gray, op=cv2.MORPH_CLOSE, kernel=element_5x5)
    closed_opened = cv2.morphologyEx(src=closed, op=cv2.MORPH_OPEN, kernel=element_5x5)
    print(closed_opened.shape)
    plt_save(image=closed_opened, title='Closed-Opened')

    opened = cv2.morphologyEx(src=image_gray, op=cv2.MORPH_OPEN, kernel=element_5x5)
    opened_closed = cv2.morphologyEx(src=opened, op=cv2.MORPH_CLOSE, kernel=element_5x5)
    plt_save(image=opened_closed, title='Opened-Closed')

    # 在灰度图像中应用形态学运算
    edge = cv2.morphologyEx(src=image_gray, op=cv2.MORPH_GRADIENT, kernel=element_3x3)
    plt_save(image=255 - edge, title='Gradient-Edge')

    # Apply threshold to obtain a binary image
    threshold = 80
    _, thresh_binary = cv2.threshold(src=edge, thresh=threshold, maxval=255, type=cv2.THRESH_BINARY)
    plt_save(image=thresh_binary, title='Gradient-Edge-Thresh-Binary-Edge')

    # 7x7 Black Top-hat Image
    black_hat = cv2.morphologyEx(src=image_gray, op=cv2.MORPH_BLACKHAT, kernel=element_7x7)
    plt_save(image=255 - black_hat, title='7x7-Black-Top-hat')

    # Apply threshold to obtain a binary image
    threshold = 25
    _, thresh_binary = cv2.threshold(src=black_hat, thresh=threshold, maxval=255, type=cv2.THRESH_BINARY)
    plt_save(image=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值