作业:

利用 OpenCV 实现:分别找出 images 中 3 张图片里的蓝色指示牌和绿色指示牌,并将它们
写入到 green、blue 两个文件夹中。
要求:
1. 用矩形框出指示牌
2. 写入的指示牌需要 尽量拉正
运行结果如下图:

思路:

1、转化成HSV,找出相应颜色对应的范围,用inRange函数提取出来。(这个一定要注意,不同的颜色的范围,我也是刚开始做,在找范围(scalar(a,b,c)时不断尝试才提取出来。)

2、找出相应轮廓并将轮廓画出。

3、用最小外接矩形,找出要提取的矩阵。

4、将找出矩形的四个顶点按照顺时针顺序排除。

5、运用三点法的仿射变换,找出变化前和变化后的坐标位置。

#include<iostream>
#include<cmath>
#include<opencv2\opencv.hpp>
using namespace std;
using namespace cv;
int main()
{Mat srcImage = imread("D://RM//OpenCv的学习//图像中提取一特定矩形//test2.png");if (!srcImage.data){cout << "读取图片错误,请重新输入正确路径!\n";system("pause");return -1;}Mat srcHSV;cvtColor(srcImage, srcHSV, COLOR_BGR2HSV);Mat dstImage;int ZYW;cout << "如果提取蓝色牌子请输入1 ,如果提取绿色牌子请输入2" << endl;cin >> ZYW;if(ZYW==1){ inRange(srcHSV, Scalar(100, 120, 125), Scalar(124, 220, 255), dstImage); }else if(ZYW==2){ inRange(srcHSV, Scalar(56, 90, 80), Scalar(90, 255, 200), dstImage); }imshow("【利用inRange()函数实现阈值化】", dstImage);Mat element = getStructuringElement(MORPH_RECT, Size(1, 2));vector<vector<Point>> contours;vector<Point> contour;vector<Vec4i> hierarchy;findContours(dstImage, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point());//轮廓绘制Mat drawImage;double width = srcImage.cols;double height = srcImage.rows;Mat dst = Mat::zeros(srcImage.size(), CV_8UC3);cout << contours.size() << endl;for (int t = 0; t < contours.size(); t++){double length2 = arcLength(contours[t], true);cout << "第" << t << "个轮廓长度=" << length2 << endl;if (length2 > 800) {contour = contours[t];Scalar color = Scalar(250, 120, 220);drawContours(srcImage, contours, t, color, 2, 8, hierarchy, 0, Point(0, 0));}}namedWindow("output", WINDOW_AUTOSIZE);imshow("output", srcImage);RotatedRect rr = minAreaRect(contour);Point2f points[4];rr.points(points);Point2f points2[4];cout << "center.x = " << rr.center.x << "center.y = " << rr.center.y << endl;for (int i = 0; i < 4; i++) {if (points[i].x < rr.center.x) {if (points[i].y < rr.center.y) {points2[0] = points[i];}else if (points[i].y > rr.center.y) {points2[3] = points[i];}}else if (points[i].x > rr.center.x) {if (points[i].y > rr.center.y) {points2[2] = points[i];}else if (points[i].y < rr.center.y) {points2[1] = points[i];}}}for (int i = 0; i < 4; i++) {cout << points2[i].x << "  " << points2[i].y << endl;}double chang = sqrt((points2[1].y - points2[0].y) * (points2[1].y - points2[0].y) + (points2[1].x - points[0].x) * (points2[1].x - points[0].x));double kuan = sqrt((points2[3].y - points2[0].y) * (points2[3].y - points2[0].y) + (points2[3].x - points2[0].x) * (points2[3].x - points2[0].x));Point2f srcPoints[3];Point2f dstPoints[3];srcPoints[0] = points2[0];srcPoints[1] = points2[1];srcPoints[2] = points2[3];dstPoints[0] = Point2f(points2[0].x, points2[0].y);dstPoints[1] = Point2f(points2[0].x + chang, points2[0].y);dstPoints[2] = Point2f(points2[0].x, points2[0].y + kuan);Mat M1 = getAffineTransform(srcPoints, dstPoints);warpAffine(srcImage, srcImage, M1, srcImage.size());imshow("正", srcImage);Rect rect1(points[1].x, points[1].y, chang, kuan);Mat after_1;srcImage(rect1).copyTo(after_1);imshow("蓝色指示牌1", after_1);imwrite("result3.png", after_1);waitKey(0);return 0;
}

运行结果如下:

                                        

第一次学,方法可能不是很好,若有不足请大家指正!!!

从图像中提取特定颜色区域。相关推荐

  1. Python Opencv cv2提取图像中某种特定颜色区域(例如黑字白纸背景下的红色公章提取),并将纯色背景透明化

    拜拜PHOTOSHOP- 领导突然让我帮他把公章从图片中抠出来,在确保了不是要做坏事的情况下,我打开了PHOTOSHOP,用魔棒工具一点一点抠,但由于魔棒工具的原理是对比临近区域像素差值,导致封闭字体 ...

  2. 提取图像感兴趣区域_从图像中提取感兴趣区域

    提取图像感兴趣区域 Welcome to the second post in this series where we talk about extracting regions of intere ...

  3. ITK:在一幅图像中提取感兴趣区域ROI

    ITK:在一幅图像中提取感兴趣区域ROI 内容提要 输出结果 C++实现代码 内容提要 在给定图像中提取给定的关注区域(ROI) 输出结果 C++实现代码 #include "itkImag ...

  4. matlab手动抠取圆形区域_图像中提取圆形子区域matlab code

    图像中提取圆形区域子图像 在图像中提取矩形区域非常容易,应用冒号表达式即可: 例如: IMG_Out=IMG_In(300:400,400:700); 就可以轻松提取到图像中100*300大小矩形区域 ...

  5. 【教程】PDF开发工具Spire.PDF 教程:使用C#从PDF中的特定矩形区域中提取文本

    Spire.PDF 是一个专业的PDF组件,能够独立地创建.编写.编辑.操作和阅读PDF文件,支持 .NET.WPF和Silverlight三个版本,本文介绍了如何通过Spire.PDF使用C#从PD ...

  6. 从单一图像中提取文档图像:ICCV2019论文解读

    从单一图像中提取文档图像:ICCV2019论文解读 DewarpNet: Single-Image Document Unwarping With Stacked 3D and 2D Regressi ...

  7. 使用OpenCV和Python从图像中提取形状

    Welcome to the first post in this series of blogs on extracting features from images using OpenCV an ...

  8. 使用 OpenCV 和 Tesseract 对图像中的感兴趣区域 (ROI) 进行 OCR

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 在这篇文章中,我们将使用 OpenCV 在图像的选定区域上应用 O ...

  9. Python+Opencv寻找图像中最亮的区域

    目录 一.场景需求解读 二.算法原理简介 三.算法代码实现 四.代码运行步骤 五.算法效果展示和分析 六.思维扩展 参考资料 注意事项 一.场景需求解读   在有些现实场景中,我们需要去使用算法自动的 ...

最新文章

  1. 在Python上使用OpenCV检测和跟踪行人
  2. 辞职后五险一金的处理方式
  3. linux网络管理证书,计算机网络管理工程师技术水平证书有什么用
  4. Redis为什么是单线程还这么快?
  5. python调用其它文件函数或类
  6. RAC的搭建(一)--安装环境准备
  7. oracle 加密怎么解密,oracle加密encrypt,解密decrypt,
  8. iTween 动画类型
  9. linux etc profile生效,让/etc/profile文件修改后立即生效
  10. spring-boot+spring-session集成
  11. python函数设置默认参数_深入讲解Python函数中参数的使用及默认参数的陷阱
  12. 使用 jQuery 和 KnockoutJS 开发在线股票走势图应用
  13. IDEA如何使用SVN插件
  14. 冲顶,百万答题助手改进(python)
  15. 软件安利Macs Fan Control Pro Mac(风扇转速调整及温度监控工具)
  16. hdu5651xiaoxin juju needs help(大组合数模板)
  17. 阿里云 部署SpringBoot和Vue项目 亲测可用(第一次部署经验贴)
  18. EXCEL怎么隔3行插入1空行?
  19. web网页设计—— 中国餐饮协会(HTML+CSS)
  20. IPAD成功完美越狱

热门文章

  1. 泛微9.0明细表必填规则
  2. python读-Python之文件读写
  3. Swift 周报 第六期
  4. OkHttp调用第三方接口
  5. Linux知识点求职应聘必备
  6. NOMSQL数据库之Mongodb
  7. 【语义分割】类别不平衡损失函数合集
  8. 六 系统可靠性分析与设计
  9. OpenGL版本查看
  10. 雷达图的4种绘制方法