关于学习 使用opencv识别验证码缺口遇到的问题
import cv2GAUSSIAN_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, , -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)
原理我大概懂了,实操起来到处是困难,运行后每一个轮廓满足条件,我觉得应该是3个判断阈值函数设置有问题,可是我就是不知道哪里错了,希望论坛的大佬能指点我,万分感谢。代码如上,图片使用的是这个:https{删除}://smms.app/image/N6qywYfZU3Senlz 根据您提供的代码,有一些可能导致问题的地方:
1. 高斯模糊参数:
GAUSSIAN_BLUR_KERNAL_SIZE = (5, 5) 设置了高斯模糊的内核大小。您可以尝试调整这个参数来适应您的验证码图片,如果图片较小,可以减小内核大小。
2. Canny 边缘检测参数:
GANNY_THERSHOLD1 = 200 和 GANNY_THERSHOLD2 = 200 是用于设置 Canny 边缘检测的阈值参数。您可以尝试调整这两个阈值来获取更好的边缘检测效果。
3. 轮廓查找参数:
contours, _ = cv2.findContours(image, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) 中的 cv2.RETR_CCOMP 和 cv2.CHAIN_APPROX_SIMPLE 分别是轮廓检测的两个参数。您可以尝试使用其他参数组合来获取更准确的轮廓。
此外,您也可以尝试调整其他阈值函数中的参数和阈值范围,以便更好地适应您的验证码图片。希望这些提示对您有帮助!
如果问题已经解决,请设置最佳答案 你的代码中有几个问题导致判断条件无法生效:
1.get_contour_area_threshold 函数中计算面积阈值的公式错误。应该将0.1和0.2乘以0.1和0.2,而不是乘以0.8和1.2。正确的代码如下:
def get_contour_area_threshold(image_width, image_height):
contour_area_min = image_width * image_height * 0.1 * 0.1 * 0.1
contour_area_max = image_width * image_height * 0.2 * 0.2 * 0.2
return contour_area_min, contour_area_max
2.get_arc_length_threshold 函数中计算轮廓周长阈值的公式错误。应该将0.1和0.2分别乘以2,而不是乘以0.8和1.2。正确的代码如下:
def get_arc_length_threshold(image_width, image_height):
arc_length_min = ((image_width * 0.1) + (image_height * 0.1)) * 2
arc_length_max = ((image_width * 0.2) + (image_height * 0.2)) * 2
return arc_length_min, arc_length_max
3.get_offset_threshold 函数中计算偏移阈值的公式错误。应该将0.3和0.8分别乘以 image_width ,而不是乘以1。正确的代码如下:
def get_offset_threshold(image_width):
offset_min = 0.3 * image_width
offset_max = 0.8 * image_width
return offset_min, offset_max
修改以上问题后,再次运行代码,判断条件应该可以正确生效了。希望对你有帮助! 很抱歉,作为AI机器人,我无法查看您的代码或使用图片链接。但是,我可以向您提供一些检查图像轮廓的方法,希望对您有所帮助。
1. 首先,您需要获取图像的轮廓。使用OpenCV函数findContours()可以找到轮廓的起始点和结束点。请确保在调用该函数前将图像转换为二进制形式(可以使用阈值处理等方法)。
2. 检查每个轮廓的面积,可以使用OpenCV函数contourArea()计算得出。如果一个轮廓的面积太小,可能是不需要的噪声或错误的对象。
3. 检查每个轮廓的周长,可以使用OpenCV函数arcLength()计算得出。如果一个轮廓的周长太小,也可能是不需要的噪声或错误的对象。
4. 检查每个轮廓的凸包,在OpenCV中使用convexHull()函数实现。如果一个轮廓不是凸多边形,则可能是需要进一步处理或排除的对象。
5. 检查每个轮廓的形状。可以使用OpenCV函数approxPolyDP()来近似估算轮廓的形状。如果一个轮廓的形状不符合预期,可能需要进一步处理或排除。
6. 如果以上方法无法解决问题,您可以尝试手动打印或可视化每个轮廓的数据,以确定哪个阈值设置不正确或哪个轮廓出现了问题。
希望这些方法可以对您有所帮助。如果您有任何其他问题,请随时提出。
此内容为ChatGPT回答,如果胡说八道,请不要理会
如果对你有帮助,请设置一个最佳答案!
页:
[1]