opencv-python的简单练习

python 复制代码
import cv2
import numpy as np

image = cv2.imread('demo111.png')
img_copy=image.copy()
# 转换图像到HSV颜色空间
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

# 红色
red_min1=np.array([0,43,46])
red_max1=np.array([10,255,255])
red_min2=np.array([156,43,46])
red_max2=np.array([180,255,255])

mask_red1=cv2.inRange(hsv,red_min1,red_max1)
mask_red2=cv2.inRange(hsv,red_min2,red_max2)

img_mask=cv2.bitwise_or(mask_red1,mask_red2)

#黄色
lower_yellow = np.array([20, 100, 100])
upper_yellow = np.array([30, 255, 255])
mask_yellow = cv2.inRange(hsv, lower_yellow, upper_yellow)
#绿色
lower_green = np.array([40, 100, 100])
upper_green = np.array([80, 255, 255])
mask_green = cv2.inRange(hsv, lower_green, upper_green)

contours_red, _ = cv2.findContours(img_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

contours_yellow, _ = cv2.findContours(mask_yellow, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 绿色
contours_green, _ = cv2.findContours(mask_green, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)


cv2.drawContours(img_copy,contours_red,-1,(193,182,255),2)
cv2.drawContours(img_copy,contours_green,-1,(0,255,0),2)
cv2.drawContours(img_copy,contours_yellow,-1,(193,0,255),2)


# 显示结果
cv2.imshow('image', image)
cv2.imshow('image_copy', img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()
python 复制代码
import cv2
import numpy as np


def detect_color(image, lower_bound, upper_bound, label):
    # 转换图像到HSV颜色空间
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

    # 创建掩膜
    mask = cv2.inRange(hsv, lower_bound, upper_bound)
    # 查找轮廓
    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    # 检测并绘制轮廓
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area > 1000:  # 设置面积阈值
            x, y, w, h = cv2.boundingRect(cnt)
            cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
            cv2.putText(image, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

    return image


# 读取图像
image = cv2.imread('duck.png')

# 定义颜色范围
# 红色
lower_red = np.array([0, 100, 100])
upper_red = np.array([10, 255, 255])

# 黄色
lower_yellow = np.array([20, 100, 100])
upper_yellow = np.array([30, 255, 255])

# 绿色
lower_green = np.array([40, 100, 100])
upper_green = np.array([80, 255, 255])

# 检测并绘制颜色
image = detect_color(image, lower_red, upper_red, 'Red')
image = detect_color(image, lower_yellow, upper_yellow, 'Yellow')
image = detect_color(image, lower_green, upper_green, 'Green')

# 显示结果
cv2.imshow('Color Detection', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
python 复制代码
import cv2
import numpy as np

# 读取图像
image = cv2.imread('02.png')

# 1. 将图像转换为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 2. 对灰度图进行二值化处理
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

# 3. 使用形态学变换去除噪声(开运算)
kernel = np.ones((5, 5), np.uint8)
opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)

# 4. 检测图像中的边缘
edges = cv2.Canny(opening, 50, 150)

# 5. 查找并绘制图像中的轮廓
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# 6. 逐一遍历轮廓,输出所有四边形的周长和面积
for contour in contours:
    # 近似轮廓为多边形
    approx = cv2.approxPolyDP(contour, 0.04 * cv2.arcLength(contour, True), True)

    # 判断是否为四边形
    if len(approx) == 4:
        # 计算周长
        perimeter = cv2.arcLength(contour, True)

        # 计算面积
        area = cv2.contourArea(contour)

        # 输出周长和面积
        print(f"Perimeter: {perimeter}, Area: {area}")

        # 绘制轮廓
        cv2.drawContours(image, [contour], -1, (0, 255, 0), 2)

# 显示结果
cv2.imshow('Result', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
python 复制代码
import cv2
import numpy as np

# 1. 读取一张包含车牌的图像
image = cv2.imread('license_plate.jpg')

# 2. 将图像转换为灰度图以简化处理
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 3. 使用高斯滤波器平滑图像,减少噪声干扰
blurred = cv2.GaussianBlur(gray, (5, 5), 0)

# 4. 应用Canny边缘检测算法检测图像中的边缘
edges = cv2.Canny(blurred, 50, 150)

# 5. 查找图像中的轮廓
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# 6. 逐一遍历轮廓
for contour in contours:
    # 7. 设定一个面积双阈值,只保留面积在该阈值的轮廓
    area = cv2.contourArea(contour)
    if area > 1000 and area < 5000:  # 面积阈值可以根据实际情况调整
        # 8. 计算这些轮廓的长宽比
        x, y, w, h = cv2.boundingRect(contour)
        ratio = float(w) / h
        if 2 < ratio < 5.5:
            # 在原图上用矩形框标出,这些轮廓可能是车牌的候选区域
            cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

# 显示结果
cv2.imshow('Result', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
相关推荐
机器之心4 分钟前
「世界模型」也被泼冷水了?邢波等人揭开五大「硬伤」,提出新范式
人工智能
费弗里8 分钟前
Python全栈应用开发利器Dash 3.x新版本介绍(4)
python·dash
甲丁10 分钟前
国内 Claude Code 接入指南(免费获得国内代理$100额度)
人工智能
机器之心12 分钟前
刚刚,为对抗哥大退学生开发的AI作弊器,哥大学生造了个AI照妖镜
人工智能
辣辣y17 分钟前
python基础day08
开发语言·python
Binary_ey18 分钟前
AR/VR 显示画质失真?OAS百叶窗波导案例破难题
人工智能·ar·vr·软件需求·光学软件
运营黑客21 分钟前
Grok 4,来了。
人工智能·学习·ai·aigc
xunberg30 分钟前
【MCP 实战派】Node-RED MCP 插件实践指南:从安装到常见问题解析
人工智能·开源
二二孚日30 分钟前
自用华为ICT云赛道AI第一章知识点-机器学习概览
人工智能·华为
weisian15130 分钟前
人工智能-基础篇-24-RAG和LLM到底怎么理解和区分?(LLM是深度训练的大语言生成模型,RAG是LLM更智能的补充技术)
人工智能