42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
# -*- coding: utf-8 -*-
|
||
# @Time : 2024/2/18 14:23
|
||
# @Author : len
|
||
# @File : find_qr_block.py
|
||
# @Software: PyCharm
|
||
# @Comment :
|
||
|
||
import cv2
|
||
import numpy as np
|
||
|
||
# 读取图像
|
||
image_path = './android_img/img_240218/img_5.png' # 替换为你的图像文件路径
|
||
image = cv2.imread(image_path)
|
||
|
||
# 灰度化
|
||
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||
|
||
# 应用阈值处理或者Canny边缘检测
|
||
# ret, thresh = cv2.threshold(gray, 127, 255, 0) # 二值化阈值处理
|
||
# thresh = cv2.Canny(image, 50, 150) # 或者使用Canny边缘检测
|
||
thresh = cv2.Canny(gray, 50, 150) # 或者使用Canny边缘检测
|
||
|
||
# 寻找轮廓
|
||
contours, _ = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
|
||
|
||
# 筛选和绘制轮廓
|
||
for cnt in contours:
|
||
# 轮廓近似
|
||
epsilon = 0.05 * cv2.arcLength(cnt, True)
|
||
approx = cv2.approxPolyDP(cnt, epsilon, True)
|
||
|
||
# 如果近似轮廓有4个顶点,则可能是矩形或方块
|
||
if len(approx) == 4:
|
||
# 绘制轮廓
|
||
cv2.drawContours(image, [approx], -1, (0, 255, 0), 3)
|
||
|
||
# 显示图像
|
||
cv2.imshow('gray', gray)
|
||
cv2.imshow('Image with Blocks', image)
|
||
cv2.waitKey(0)
|
||
cv2.destroyAllWindows()
|