手机域名网站怎么做深圳找网站建设公司

当前位置: 首页 > news >正文

手机域名网站怎么做,深圳找网站建设公司,电脑网站你懂我意思正能量,国外购物网站系统滑块验证码是一种常见的验证码形式#xff0c;通过拖动滑块与背景图像中的缺口进行匹配#xff0c;验证用户是否为真人。本文将详细介绍基于图像处理的滑块验证码匹配技术#xff0c;并提供优化代码以提高滑块位置偏移量的准确度#xff0c;尤其是在背景图滑块阴影较浅的情… 滑块验证码是一种常见的验证码形式通过拖动滑块与背景图像中的缺口进行匹配验证用户是否为真人。本文将详细介绍基于图像处理的滑块验证码匹配技术并提供优化代码以提高滑块位置偏移量的准确度尤其是在背景图滑块阴影较浅的情况下。 一、背景知识 1.1 图像处理概述 图像处理是指对图像进行分析和操作以达到增强图像、提取特征、识别模式等目的。常用的图像处理技术包括高斯模糊、Canny 边缘检测、轮廓提取等。 1.2 滑块验证码的原理 滑块验证码通过用户拖动滑块使滑块图像与背景图像中的缺口对齐从而验证用户的操作。实现滑块验证码匹配的关键在于精确检测背景图像中缺口的位置。 二、技术实现 2.1 代码实现 import base64 import os from datetime import datetime from typing import Union, Optionalimport cv2 import numpy as npclass SliderCaptchaMatch:def init(self,gaussian_blur_kernel_size(5, 5),gaussian_blur_sigma_x0,canny_threshold1200,canny_threshold2450,save_imagesFalse,output_path):初始化SlideMatch类:param gaussian_blur_kernel_size: 高斯模糊核大小默认(5, 5):param gaussian_blur_sigma_x: 高斯模糊SigmaX默认0:param canny_threshold1: Canny边缘检测阈值1默认200:param canny_threshold2: Canny边缘检测阈值2默认450:param save_images: 是否保存过程图片默认False:param output_path: 生成图片保存路径默认当前目录self.GAUSSIAN_BLUR_KERNEL_SIZE gaussian_blur_kernel_sizeself.GAUSSIAN_BLUR_SIGMA_X gaussian_blur_sigma_xself.CANNY_THRESHOLD1 canny_threshold1self.CANNY_THRESHOLD2 canny_threshold2self.save_images save_imagesself.output_path output_pathdef _remove_alpha_channel(self, image):移除图像的alpha通道:param image: 输入图像:return: 移除alpha通道后的图像if image.shape[2] 4: # 如果图像有alpha通道alpha_channel image[:, :, 3]rgb_channels image[:, :, :3]# 创建一个白色背景white_background np.ones_like(rgb_channels, dtypenp.uint8) * 255# 使用alpha混合图像与白色背景alpha_factor alpha_channel[:, :, np.newaxis] / 255.0image_no_alpha rgb_channels * alpha_factor white_background * (1 - alpha_factor)return image_no_alpha.astype(np.uint8)else:return imagedef _get_gaussian_blur_image(self, image):对图像进行高斯模糊处理:param image: 输入图像:return: 高斯模糊处理后的图像return cv2.GaussianBlur(image, self.GAUSSIAN_BLUR_KERNEL_SIZE, self.GAUSSIAN_BLUR_SIGMA_X)def _get_canny_image(self, image):对图像进行Canny边缘检测:param image: 输入图像:return: Canny边缘检测后的图像return cv2.Canny(image, self.CANNY_THRESHOLD1, self.CANNY_THRESHOLD2)def _get_contours(self, image):获取图像的轮廓:param image: 输入图像:return: 轮廓列表contours, _ cv2.findContours(image, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)return contoursdef _get_contour_area_threshold(self, image_width, image_height):计算轮廓面积阈值:param image_width: 图像宽度:param image_height: 图像高度:return: 最小和最大轮廓面积阈值contour_area_min (image_width * 0.15) * (image_height * 0.25) * 0.8contour_area_max (image_width * 0.15) * (image_height * 0.25) * 1.2return contour_area_min, contour_area_maxdef _get_arc_length_threshold(self, image_width, image_height):计算轮廓弧长阈值:param image_width: 图像宽度:param image_height: 图像高度:return: 最小和最大弧长阈值arc_length_min ((image_width * 0.15) (image_height * 0.25)) * 2 * 0.8arc_length_max ((image_width * 0.15) (image_height * 0.25)) * 2 * 1.2return arc_length_min, arc_length_maxdef _get_offset_threshold(self, image_width):计算偏移量阈值:param image_width: 图像宽度:return: 最小和最大偏移量阈值offset_min 0.2 * image_widthoffset_max 0.85 * image_widthreturn offset_min, offset_maxdef _is_image_file(self, file_path: str) - bool:检查字符串是否是有效的图像文件路径valid_extensions (.jpg, .jpeg, .png, .bmp, .gif, .tiff)return os.path.isfile(file_path) and file_path.lower().endswith(valid_extensions)def _is_base64(self, s: str) - bool:检查字符串是否是有效的 base64 编码try:if isinstance(s, str):# Strip out data URI scheme if presentif data: in s and ; in s:s s.split(,)[1]base64.b64decode(s)return Truereturn Falseexcept Exception:return Falsedef _read_image(self, image_source: Union[str, bytes], imread_flag: Optional[int] None) - np.ndarray:读取图像:param image_source: 图像路径或base64编码:param imread_flag: cv2.imread 和 cv2.imdecode 的标志参数 (默认: None):return: 读取的图像if isinstance(image_source, str):if self._is_image_file(image_source): # 如果是文件路径if imread_flag is not None:return cv2.imread(image_source, imread_flag)else:return cv2.imread(image_source)elif self._is_base64(image_source): # 如果是 base64 编码# 剥离数据URI方案如果存在if data: in image_source and ; in image_source:image_source image_source.split(,)[1]img_data base64.b64decode(image_source)img_array np.frombuffer(img_data, np.uint8)if imread_flag is not None:image cv2.imdecode(img_array, imread_flag)else:image cv2.imdecode(img_array, cv2.IMREAD_UNCHANGED)if image is None:raise ValueError(Failed to decode base64 image)return imageelse:raise ValueError(The provided string is neither a valid file path nor a valid base64 string)else:raise ValueError(image_source must be a file path or base64 encoded string)def get_slider_offset(self, background_source: Union[str, bytes], slider_source: Union[str, bytes],out_file_name: str None) - int:获取滑块的偏移量:param background_source: 背景图像路径或base64编码:param slider_source: 滑块图像路径或base64编码:param out_file_name: 输出图片的文件名: 默认为当前时间戳:return: 滑块的偏移量background_image self._read_image(background_source)slider_image self._read_image(slider_source, cv2.IMREAD_UNCHANGED)out_file_name out_file_name if out_file_name else datetime.now().strftime(%Y%m%d%H%M%S.%f)[:-3]if background_image is None:raise ValueError(Failed to read background image)if slider_image is None:raise ValueError(Failed to read slider image)slider_image_no_alpha self._remove_alpha_channel(slider_image)image_height, image_width, _ background_image.shapeimage_gaussian_blur self._get_gaussian_blur_image(background_image)image_canny self._get_canny_image(image_gaussian_blur)contours self._get_contours(image_canny)if self.save_images:# 创建输出目录if not os.path.exists(self.output_path):os.makedirs(self.output_path)cv2.imwrite(os.path.join(self.output_path, f{out_file_name}_image_canny.png), image_canny)cv2.imwrite(os.path.join(self.output_path, f{out_file_name}_image_gaussian_blur.png), image_gaussian_blur)contour_area_min, contour_area_max self._get_contour_area_threshold(image_width, image_height)arc_length_min, arc_length_max self._get_arc_length_threshold(image_width, image_height)offset_min, offset_max self._get_offset_threshold(image_width)offset Nonefor contour in contours:x, y, w, h cv2.boundingRect(contour)if contour_area_min cv2.contourArea(contour) contour_area_max and \arc_length_min cv2.arcLength(contour, True) arc_length_max and \offset_min x offset_max:cv2.rectangle(background_image, (x, y), (x w, y h), (0, 0, 255), 2)offset x# 匹配滑块模板在背景中的位置result cv2.matchTemplate(background_image, slider_image_no_alpha, cv2.TM_CCOEFFNORMED), _, _, max_loc cv2.minMaxLoc(result)slider_x, slider_y max_locoffset slider_xcv2.rectangle(background_image, (slider_x, slider_y),(slider_x slider_image_no_alpha.shape[1], slider_y slider_image_no_alpha.shape[0]),(255, 0, 0), 2)if self.save_images:cv2.imwrite(os.path.join(self.output_path, f{out_file_name}_image_label.png), background_image)return offset 2.2 代码说明 图像预处理通过高斯模糊和Canny边缘检测增强图像的对比度和亮度提高滑块识别率。多图像融合通过多次处理图像并融合结果以减小噪声对检测结果的影响。动态调整阈值根据图像的直方图动态调整Canny边缘检测的阈值提高对不同图像的适应性。轮廓检测通过 _get_contours 函数获取图像的轮廓并根据轮廓面积和弧长进行筛选。滑块匹配通过模板匹配方法 cv2.matchTemplate 匹配滑块在背景图中的位置。 2.3 优化策略 对比度和亮度增强通过提高图像的对比度和亮度使得滑块和背景的区别更加明显增强滑块匹配的准确度。多图像融合融合多张处理后的图像减小单张图像中的噪声对结果的影响。动态调整参数根据图像内容动态调整Canny边缘检测的阈值使得算法对不同类型的图像都有较好的适应性。 2.4 安装依赖 要运行上述代码需要安装以下 Python 库 pip install numpy opencv-python slider_captcha_match 2.5 使用方法 在安装完所需库后您可以按照以下步骤使用滑块验证码匹配功能 初始化SliderCaptchaMatch类配置高斯模糊、Canny边缘检测等参数。读取背景图像和滑块图像可以是文件路径或base64编码。获取滑块偏移量调用get_slider_offset函数返回滑块的准确偏移量。 from slider_captcha_match import SliderCaptchaMatchfrom datetime import datetimeimport cv2# 初始化 SliderCaptchaMatch 类slider_captcha_match SliderCaptchaMatch(save_imagesTrue,output_pathoutput)# 读取背景图像和滑块图像background_source path_to_background_image.jpgslider_source path_to_slider_image.png# 获取滑块偏移量offset slider_captcha_match.get_slider_offset(background_source, slider_source)print(f滑块偏移量: {offset})# 输出结果保存路径out_file_name datetime.now().strftime(%Y%m%d%H%M%S.%f)[:-3]print(f结果图像保存路径: output/{out_file_name}_image_label.png) 三、测试与验证 为了验证优化后的滑块验证码匹配技术进行多次测试比较不同情况下的滑块偏移量检测结果并记录背景图、滑块图、中间预处理图和代码标注的滑块位置的图以及缺口坐标位置偏移量计算。 Response for row 1: offset手动标注155缺口坐标代码计算155.0 Response for row 2: offset手动标注119缺口坐标代码计算118.5 Response for row 2: offset手动标注223缺口坐标代码计算224.0 四、总结 本文介绍了基于图像处理的滑块验证码匹配技术并通过多种优化策略提高了滑块位置偏移量的检测准确度。通过对图像进行预处理、融合多张图像、动态调整阈值等方法可以有效提高滑块验证码在不同背景下的识别率。希望这篇文章能够对从事图像处理和验证码研究的读者有所帮助。 参考资料 OpenCV 官方文档NumPy 官方文档本Github项目源码地址