Embedded_game/qianrushi/opencv膨胀.py

45 lines
1023 B
Python
Raw Normal View History

2025-01-02 12:48:11 +08:00
# -*- coding:utf-8 -*-
# @Author len
# @Create 2023/10/22 15:25
import cv2
import numpy as np
# 读取二维码图像文件
image_path = 'data/blue.jpg'
image = cv2.imread(image_path)
height, width, _ = image.shape
center_x = width // 2
center_y = height // 2
quarter_width = width // 4
quarter_height = height // 4
roi_x = center_x - quarter_width
roi_y = center_y - quarter_height
roi_width = quarter_width * 2
roi_height = quarter_height * 2
roi = image[roi_y:roi_y+roi_height, roi_x:roi_x+roi_width]
# 膨胀操作
kernel = np.ones((3, 3), np.uint8)
dilated = cv2.erode(image, kernel, iterations=1)
# 将图像转换为HSV颜色空间
hsv = cv2.cvtColor(dilated, cv2.COLOR_BGR2HSV)
# 计算膨胀后图像的HSV平均值
h_mean, s_mean, v_mean = cv2.mean(dilated)[:3]
print("HSV Average Values:")
print("Hue (H):", h_mean)
print("Saturation (S):", s_mean)
print("Value (V):", v_mean)
cv2.imshow('Image', image)
cv2.imshow('roi', roi)
cv2.imshow('hsv', hsv)
cv2.imshow('Dilated Image', dilated)
cv2.waitKey(0)