本文主要是介绍使用OpenCV计算滑块缺口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.参考文章:https://blog.csdn.net/qq_27371025/article/details/133072065
2.实现过程:接口中传入base64 图片,base64转化为image ,通过图片获取缺口信息
实现步骤:
2.1 安装:cv2
opencv-python 是 OpenCV(Open Source Computer Vision Library)的 Python 绑定。OpenCV 是一个开源的计算机视觉和机器学习库,包含了大量的图像处理函数和计算机视觉算法。通过 opencv-python,Python 开发者能够方便地使用这些功能和算法来处理图像和视频数据。
pip install opencv-python
pip install opencv-contrib-python
def show(name):'''展示圈出来的位置'''cv2.imshow('Show', name)cv2.waitKey(0)cv2.destroyAllWindows()def _tran_canny(image):"""消除噪声"""image = cv2.GaussianBlur(image, (3, 3), 0)return cv2.Canny(image, 50, 150)def detect_displacement(img_slider_path, image_background_path):"""detect displacement"""# # 参数0是灰度模式image = cv2.imread(img_slider_path, 0)template = cv2.imread(image_background_path, 0)# 寻找最佳匹配res = cv2.matchTemplate(_tran_canny(image), _tran_canny(template), cv2.TM_CCOEFF_NORMED)# 最小值,最大值,并得到最小值, 最大值的索引min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)top_left = min_loc[0] # 横坐标# 展示圈出来的区域x, y = min_loc # 获取x,y位置坐标w, h = image.shape[::-1] # 宽高cv2.rectangle(template, (x, y), (x+w, y+h), (0, 0, 255), 2) #左上 右下 边框颜色,线条厚度# show(template)top_left = x+wreturn top_leftdef base64_change_image(base64_str,type):# 移除base64字符串中的前缀(例如 'data:image/jpeg;base64,')base64_img_bytes = base64_str.split(',')[1]# 解码base64字符串为字节img_bytes = base64.b64decode(base64_img_bytes)# 将字节转换为numpy数组nparr = np.frombuffer(img_bytes, np.uint8)# 将numpy数组转换为OpenCV图像img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)# 检查图像是否正确加载if img is None:print("Error: 图像未正确加载")else:if type == "left":cv2.imwrite('img/left.jpg', img)return "img/left.jpg"elif type == "back":cv2.imwrite('img/back.jpg', img)return "img/back.jpg"else:cv2.imwrite('img/icon.jpg', img)return "img/icon.jpg"def get_click_points(image_path):# 读取图片image = cv2.imread(image_path)# 转换为灰度图gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# 二值化处理_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)# 查找轮廓contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)# 遍历轮廓,寻找点points = []for contour in contours:# 获取轮廓的边界框x, y, w, h = cv2.boundingRect(contour)# 可以根据实际情况过滤小轮廓if w > 5 and h > 5:# 标记轮廓cv2.rectangle(image, (x-10, y), (x + w-10, y + h), (0, 255, 0), 2)# 保存点击位置points.append((x + w // 2, y + h // 2))# 显示图片cv2.imshow('Image with points', image)cv2.waitKey(0)cv2.destroyAllWindows()return points@app.route('/SlidingBlock')
def sliding_block():start_now = datetime.now()start_now_time = start_now.strftime("%Y-%m-%d %H:%M:%S")print("开始时间:", start_now_time)date = request.get_json()left_image_str = date['left_image']left_image = base64_change_image(left_image_str, "left")back_image_str = date['back_image']back_image = base64_change_image(back_image_str, "back")skewing = detect_displacement(left_image, back_image)print(f"偏移:{skewing}")data = {'skewing': skewing}last_now = datetime.now()last_now_time = last_now.strftime("%Y-%m-%d %H:%M:%S")print("结束时间:", last_now_time)return jsonify(data)
这篇关于使用OpenCV计算滑块缺口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!