import cv2
GAUSSIAN_BLUR_KERNAL_SIZE = (5, 5)
GAUSSIAN_BLUR_SIGMA_X = 0
GANNY_THERSHOLD1 = 200
GANNY_THERSHOLD2 = 200
import random
def get_gussian_blur_image(image):
return cv2.GaussianBlur(image, GAUSSIAN_BLUR_KERNAL_SIZE, GAUSSIAN_BLUR_SIGMA_X)
def get_canny_image(image):
return cv2.Canny(image, GANNY_THERSHOLD1, GANNY_THERSHOLD2)
def get_contours(image):
contours, _ = cv2.findContours(image, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
return contours
# 以下是显示图像的代码,不做修改
import cv2
import matplotlib.pyplot as plt
# 读取图像
image_raw = cv2.imread('2222.png')
# 获取图像尺寸
image_height, image_width, _ = image_raw.shape
# 高斯模糊处理
image_gaussian_blur = get_gussian_blur_image(image_raw)
# Canny边缘检测
image_canny = get_canny_image(image_gaussian_blur)
# 轮廓查找
contours = get_contours(image_canny)
# 显示原始图像
plt.subplot(2, 2, 1)
plt.imshow(cv2.cvtColor(image_raw, cv2.COLOR_BGR2RGB))
plt.title('Original Image')
# 显示高斯模糊后的图像
plt.subplot(2, 2, 2)
plt.imshow(cv2.cvtColor(image_gaussian_blur, cv2.COLOR_BGR2RGB))
plt.title('Gaussian Blur Image')
def get_contour_area_threshold(image_width, image_height):
contour_area_min = image_width * image_height * 0.1 * 0.1 * 0.8
contour_area_max = image_width * image_height * 0.2 * 0.2 * 1.2
return contour_area_min, contour_area_max
def get_arc_length_threshold(image_width, image_height):
arc_length_min = ((image_width * 0.1) + (image_height * 0.1)) * 2 * 0.8
arc_length_max = ((image_width * 0.2) + (image_height * 0.2)) * 2 * 1.2
return arc_length_min, arc_length_max
def get_offset_threshold(image_width):
offset_min = 0.3 * image_width
offset_max = 0.8 * image_width
return offset_min, offset_max
import random
import matplotlib.pyplot as plt
# 绘制轮廓
for contour in contours:
color = (random.randint(0, 255) / 255, random.randint(0, 255) / 255, random.randint(0, 255) / 255)
cv2.drawContours(image_raw, [contour], -1, color, 2)
# 显示Canny边缘检测后的图像和轮廓
plt.subplot(2, 2, 3)
plt.imshow(image_canny, cmap='gray')
plt.title('Canny Image')
plt.subplot(2, 2, 4)
plt.imshow(cv2.cvtColor(image_raw, cv2.COLOR_BGR2RGB))
for contour in contours:
color = (random.randint(0, 255) / 255, random.randint(0, 255) / 255, random.randint(0, 255) / 255)
plt.plot(contour[:, 0, 0], contour[:, 0, 1], color=color, linewidth=2)
plt.title('Contours')
# 调整子图布局
plt.tight_layout()
# 保存最后的图片
plt.savefig('contours.png')
# 显示图像
plt.show()
contour_area_min, contour_area_max = get_contour_area_threshold(image_width, image_height)
arc_length_min, arc_length_max = get_arc_length_threshold(image_width, image_height)
offset_min, offset_max = get_offset_threshold(image_width)
offset = None
print(len(contours))
count = 1
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
if offset_min < x < offset_max and arc_length_min < cv2.arcLength(contour,
True) < arc_length_max and contour_area_min < cv2.contourArea(
contour) < contour_area_max:
cv2.rectangle(image_raw, (x, y), (x + w, y + h), (0, 0, 255), 2)
offset = x
print(1)
# 保存带有矩形框的图像
cv2.imwrite('image_label.png', cv2.cvtColor(image_raw, cv2.COLOR_BGR2RGB))
print('offset', offset)