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()