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

opencv识别一张图片的多个红框,并截取红框的内容

需求

 需要获取图片的红框的内容,实体的图片我就不放了

获取红框

先截取获得图片的多个轮廓

import cv2  
import numpy as np  # 加载图像并转换为灰度图像  
image = cv2.imread('image6.jpg')  
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # 应用高斯模糊以减少噪声  
blur = cv2.GaussianBlur(gray, (5, 5), 0)  # 应用HSV颜色空间转换  
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)  
lower_red = np.array([0, 50, 50])  
upper_red = np.array([10, 255, 255])  
mask = cv2.inRange(hsv, lower_red, upper_red)  # 应用膨胀操作来放大边框内的内容和边框  
kernel = np.ones((5,5),np.uint8)  
dilated = cv2.dilate(mask,kernel,iterations = 1)  # 获取边界框坐标  
contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  # 遍历每个轮廓并找到最大的红色边框  
max_contour = None  
max_area = 0  
for contour in contours:  area = cv2.contourArea(contour)  # if area > max_area:  #     max_contour = contour  #     max_area = area  x, y, w, h = cv2.boundingRect(contour)  # 裁剪图像以显示边界框内的内容及其周围10px内容  crop_image = image[max(y-10, 0):min(y+h+10, image.shape[0]), max(x-10, 0):min(x+w+10, image.shape[1])]  # 在裁剪后的图像上绘制红色矩形框以突出显示边界框内的内容及其周围10px内容  cv2.rectangle(crop_image, (max(x-10, 0), max(y-10, 0)), (min(x+w+10, image.shape[1]), min(y+h+10, image.shape[0])), (0, 0, 255), 2)  # 在裁剪后的图像上绘制红色矩形框以突出显示边界框内的内容及其周围10px内容  #cv2.imshow('Content with Border and Surrounding Area', crop_image)  # 显示带有红色边框和周围10px内容的裁剪后的图像  cv2.imwrite(f'red_border_{x}_{y}_{w}_{h}.jpg', crop_image)  cv2.waitKey(0)  cv2.destroyAllWindows()# 获取最大轮廓的边界框坐标  
# x, y, w, h = cv2.boundingRect(max_contour)  # # 裁剪图像以显示边界框内的内容及其周围10px内容  
# crop_image = image[max(y-10, 0):min(y+h+10, image.shape[0]), max(x-10, 0):min(x+w+10, image.shape[1])]  # # 在裁剪后的图像上绘制红色矩形框以突出显示边界框内的内容及其周围10px内容  
# cv2.rectangle(crop_image, (max(x-10, 0), max(y-10, 0)), (min(x+w+10, image.shape[1]), min(y+h+10, image.shape[0])), (0, 0, 255), 2)  # 在裁剪后的图像上绘制红色矩形框以突出显示边界框内的内容及其周围10px内容  
# cv2.imshow('Content with Border and Surrounding Area', crop_image)  # 显示带有红色边框和周围10px内容的裁剪后的图像  # cv2.waitKey(0)  
# cv2.destroyAllWindows()

识别红框

import cv2
import numpy as np# 加载图像
image = cv2.imread('red_border_1038_1886_6_6.jpg')# 将图像转换为灰度
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# 二值化图像
_, threshold = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)# 找到图像中的轮廓
contours, _ = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)# 遍历每个轮廓,判断是否是闭合的圆
for contour in contours:# 进行轮廓近似,获取近似的多边形轮廓epsilon = 0.01 * cv2.arcLength(contour, True)approx = cv2.approxPolyDP(contour, epsilon, True)# 计算近似轮廓的周长approx_length = cv2.arcLength(approx, True)# 计算原始轮廓的周长contour_length = cv2.arcLength(contour, True)# 判断近似轮廓的周长是否接近于原始轮廓的周长if approx_length >= 0.9 * contour_length:# 绘制闭合的圆cv2.drawContours(image, [approx], -1, (0, 255, 0), 2)cv2.putText(image, 'Closed Circle', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)print("存在")# 显示结果图像
cv2.imshow('Result', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

http://www.lryc.cn/news/156251.html

相关文章:

  • 数据库-事务
  • MySQL 使用开源审计插件
  • Python入门教程 | Python3 集合(Set)
  • 视频汇聚/视频云存储/视频监控管理平台EasyCVR安全检查的相关问题及解决方法2.0
  • 【C++模拟实现】反向迭代器的实现
  • Kubernetes技术--k8s核心技术持久化存储
  • 【80天学习完《深入理解计算机系统》】第十四天 复习第三章
  • 库中是如何实现string类的?
  • 无涯教程-JavaScript - WORKDAY.INTL函数
  • STM32--蓝牙
  • java 实现原型模式
  • maven本地安装jar包install-file,解决没有pom的问题
  • 【C++学习笔记】5、变量作用域
  • Python中的装饰器
  • 什么是RESTful API,Spring MVC如何支持RESTful架构
  • cin、cin.getline()、getline()的用法【C++】
  • 单向链表(c/c++)
  • 像linux 一样清理Windows C盘
  • 在Linux 下制作启动盘以及dd命令使用
  • C语言插入排序
  • SQL-DCL
  • Elasticsearch 中的向量搜索:设计背后的基本原理
  • Jquery会议室布局含门入口和投影位置调整,并自动截图
  • 高精度乘法模板(fft)
  • C# 现状简单说明
  • el-table滚动加载、懒加载(自定义指令)
  • 不关闭Tamper Protection(篡改保护)下强制卸载Windows Defender和安全中心所有组件
  • 从一到无穷大 #13 How does Lindorm TSDB solve the high cardinality problem?
  • 三维模型OBJ格式轻量化的纹理压缩和质量关系分析
  • 【每日一题】54. 螺旋矩阵