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

【opnecv】检测桌子上多余的物品

目录

  • 效果展示
  • 实现原理
  • 整体代码

效果展示

在这里插入图片描述

实现原理

1、转化成灰度图

 Mat image = imread("C:\\Users\\chenxinyu\\Desktop\\物品.jpg",IMREAD_COLOR);if (image.empty())return -1;Mat outImage;cvtColor(image, outImage, COLOR_BGR2GRAY);

2、绘制轮廓

在绘制轮廓之前,我们需要进行高斯模糊降噪。

 Mat outBlur,outCanny;GaussianBlur(outImage, outBlur,Size(3,3),1.5);Canny(outBlur, outCanny,100,150);

减少无关噪声对轮廓的影响。
在这里插入图片描述

3、二值化操作

对原图像进行二值化操作,再与轮廓图相加,丰富饱满轮廓图。

 Mat outThreshold;threshold(outImage, outThreshold, 120, 255, THRESH_BINARY_INV);Mat outThreshold1;threshold(outImage, outThreshold1, 220, 255, THRESH_BINARY);Mat outOr;bitwise_or(outCanny, outThreshold, outOr);bitwise_or(outOr, outThreshold1, outOr);

注:第二次二值化是为针对图像中白色蛋壳。

在这里插入图片描述

4、膨胀和腐蚀

去掉多余的线条,让图像更加饱满。

Mat outElement;
outElement=getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
morphologyEx(outOr, outOr, MORPH_CLOSE, outElement);
morphologyEx(outOr, outOr, MORPH_OPEN, outElement);

在这里插入图片描述

5、找轮廓、标识

vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(outOr, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
for (size_t i = 0; i < contours.size(); i++) {// 计算面积double area = contourArea(contours[i]); // 标注瑕疵Rect bbox = boundingRect(contours[i]);rectangle(image, bbox, Scalar(0, 0, 255), 2);  // 红色边界框
}

在这里插入图片描述

整体代码

int demo4()
{Mat image = imread("C:\\Users\\chenxinyu\\Desktop\\物品.jpg",IMREAD_COLOR);if (image.empty())return -1;imshow("111.jpg", image);Mat outImage;cvtColor(image, outImage, COLOR_BGR2GRAY);imshow("222.jpg", outImage);画轮廓Mat outBlur;GaussianBlur(outImage, outBlur,Size(3,3),1.5);imshow("333.jpg", outBlur);Mat outCanny;Canny(outBlur, outCanny,100,150);imshow("444.jpg", outCanny);Mat outCanny1;Canny(outImage, outCanny1, 100, 150);imshow("text1.jpg", outCanny1);//二值分化Mat outThreshold;threshold(outImage, outThreshold, 120, 255, THRESH_BINARY_INV);imshow("555.jpg", outThreshold);Mat outThreshold1;threshold(outImage, outThreshold1, 220, 255, THRESH_BINARY);imshow("text.jpg", outThreshold1);Mat outOr;bitwise_or(outCanny, outThreshold, outOr);bitwise_or(outOr, outThreshold1, outOr);imshow("666.jpg", outOr);Mat outElement;outElement=getStructuringElement(MORPH_ELLIPSE, Size(3, 3));morphologyEx(outOr, outOr, MORPH_CLOSE, outElement);morphologyEx(outOr, outOr, MORPH_OPEN, outElement);imshow("777.jpg", outOr);vector<vector<Point>> contours;vector<Vec4i> hierarchy;findContours(outOr, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);for (size_t i = 0; i < contours.size(); i++) {// 忽略小面积区域(噪声)double area = contourArea(contours[i]);// 标注瑕疵Rect bbox = boundingRect(contours[i]);rectangle(image, bbox, Scalar(0, 0, 255), 2);  // 红色边界框}imshow("888.jpg", image);waitKey(0);
}
http://www.xdnf.cn/news/12087.html

相关文章:

  • 《复制粘贴的奇迹:小明的原型工厂》
  • python打卡第44天
  • AI大模型学习三十二、飞桨AI studio 部署 免费Qwen3-235B与Qwen3-32B,并导入dify应用
  • CSS 选择器全解析:分组选择器/嵌套选择器,从基础到高级
  • 关于如何运用AI的思考
  • Day44 Python打卡训练营
  • ATM存取钱项目
  • 【DeepSeek 学大模型推理】Fused Residual LayerNorm with Reduce-Scatter
  • MySQL事务:从ACID特性到高并发优化的深度解析
  • day 44
  • K8S主机漏洞扫描时检测到kube-服务目标SSL证书已过期漏洞的一种永久性修复方法
  • 【论文写作】如何撰写基于模型拼接(A+B)的创新性论文
  • leetcode 二叉搜索树中第k小的元素 java
  • SiFli 567+emmc Standby休眠报错问题
  • 重装系统+驱动+磁盘分区
  • day19 leetcode-hot100-37(二叉树2)
  • 5.29-6.4解决问题归纳
  • 银行用户信誉等级
  • 前端面试宝典---vite原理解析
  • Numpy——结构化数组和Numpy文件
  • 【电赛培训课程】电子设计竞赛工程基础知识
  • 使用qt 定义全局钩子 捕获系统的键盘事件
  • 《人性的弱点》核心总结
  • AI基础认知
  • 【Python连接数据库基础 06】Pandas与SQL协同:解锁大规模数据处理新境界,让分析效率飙升10倍
  • 代理IP:6G标准化进程中的隐形推手
  • 如何在 React 中监听 div 的滚动事件
  • Pendulum:优雅处理 Python 中的日期与时间
  • vue3动态插入iframe,内容被取消的原因
  • pack 布局管理器