/**
* 矩形拟合
* @param mats
* @return
*/
vector<Rect> PublicCardFrameDetection::RectContainFit(vector<Rect> rects,Mat src) { //获取图像中心坐标
int src_w = src.cols;
int src_h = src.rows;
Point2i center = (Point2i(src_w/2),Point2i(src_h/2));
Rect rect_center (int(src_w/3),int(src_h/3),int(src_w/3),int(src_h/3));
// rectangle(src,rect_center.tl(),rect_center.br(),cv::Scalar(255,255,255),5,8);
//存放结果Rect区域
vector<Rect> results;
vector<Rect> roi_tl; //左上
vector<Rect> roi_tr; //右上
vector<Rect> roi_bl; //左下
vector<Rect> roi_br; //右下
vector<Rect> roi_center; //中间
int size = rects.size();
for (auto rct : rects){
Point2i tl_pt = rct.tl();
Point2i tr_pt = Point2i(rct.tl().x + src_w,rct.tl().y);
Point2i br_pt = rct.br();
Point2i bl_pt = Point2i(rct.br().x - src_w,rct.br().y);
Point2i center_pt = (tl_pt+br_pt)/2;
//rect是否落在中间3*3的九宫格区域
if ((tl_pt.x < rect_center.tl().x && tl_pt.y < rect_center.tl().y) &&
(br_pt.x < rect_center.br().x && br_pt.y < rect_center.br().y)){
if ((tl_pt.x + rct.width < rect_center.tl().x + rect_center.width) &&
(tl_pt.y + rct.height < rect_center.tl().y + rect_center.height))
roi_center.emplace_back(rct);
} //rect在中心点center左上方
if (center_pt.y < center.y && center_pt.x > center.x){
// if (br_pt.x < center.x && br_pt.y < center.y &&
// bl_pt.x < center.x && bl_pt.y < center.y)
roi_tr.emplace_back(rct);
}
//rect在中心点center右上方
if (center_pt.y < center.y && center_pt.x > center.x){
// if (br_pt.x < center.x && br_pt.y < center.y &&
// bl_pt.x < center.x && bl_pt.y < center.y)
roi_tl.emplace_back(rct);
}
//rect在中心点center左下方
if (center_pt.y > center.y && center_pt.x > center.x){
// if (tl_pt.x > center.x && tl_pt.y > center.y &&
// tr_pt.x < center.x && tr_pt.y < center.y)
roi_br.emplace_back(rct);
}
//rect在中心点左下方
if (center_pt.y > center.y && center_pt.x < center.x){
// if (tl_pt.x > center.x && tl_pt.y > center.y &&
// tr_pt.x < center.x && tr_pt.y < center.y)
roi_bl.emplace_back(rct);
} }//for_end //若rect落在正中间,说明识别单卡片,直接返回results结果
// if (!roi_center.empty()){
// rectangle(src,roi_center[0].tl(),roi_center[0].br(),cv::Scalar(0,0,255),5,8);
// results.emplace_back(roi_center[0]);
// imshow("rectangle",src);
// waitKey(0);
// return results;
// } else {
//否则,说明识别多卡片,对卡片进行排序
//顺序为:逆时针==>左上,左下,右下,左上
//若四个方向的集合为空,存入原图分割后的四个象限
if (roi_tl.empty()) {
Rect tl(0,0,src_w/2,src_h/2);
roi_tl.emplace_back(tl);
}
if (roi_bl.empty()) {
Rect bl(0,src_h/2,src_w/2,src_h/2);
roi_bl.emplace_back(bl);
}
if (roi_br.empty()) {
Rect br(src_w/2,src_h/2,src_w/2,src_h/2);
roi_br.emplace_back(br);
}
if (roi_tr.empty()) {
Rect tr(src_w/2,0,src_w/2,src_h/2);
roi_tr.emplace_back(tr);
}
//Scalr(BGR)
rectangle(src,roi_tl[0].tl(),roi_tl[0].br(),cv::Scalar(0,0,255),5,8);
rectangle(src,roi_bl[0].tl(),roi_bl[0].br(),cv::Scalar(255,0,0),5,8);
rectangle(src,roi_br[0].tl(),roi_br[0].br(),cv::Scalar(0,255,0),5,8);
rectangle(src,roi_tr[0].tl(),roi_tr[0].br(),cv::Scalar(255,0,255),5,8);
imshow("rectangle",src);
waitKey(0);
results.emplace_back(roi_tl[0]);
results.emplace_back(roi_bl[0]);
results.emplace_back(roi_br[0]);
results.emplace_back(roi_tr[0]);
// }
return results;
}

最新文章

  1. 裸奔单片机GUI
  2. Rational Rose2007下载安装教程以及问题处理
  3. CSS - Transform(Translate) abnormal shadow in firefox
  4. Toolbar标题栏
  5. 【转】JSP中的9大隐藏对象
  6. 我的R代码备份
  7. C#3.0 语言基础扩充
  8. 编tuxedo遇到服务问题
  9. yaf for ubuntu安装
  10. 加入大型的js文件如jQuery文件,Eclipse会报错
  11. [Alpha阶段]第九次Scrum Meeting
  12. 【easy】226. Invert Binary Tree 反转二叉树
  13. topcoder srm 630 div1 (2-SAT and SCC template)
  14. 一个模拟——抢票部分功能的 简单版(主要实例化一下 Lock 的使用)
  15. POJ-3693/HDU-2459 Maximum repetition substring 最多重复次数的子串(需要输出具体子串,按字典序)
  16. python之路——3
  17. Ubuntu12.04 内核树建立
  18. 移动端1px细线解决方案总结
  19. WinForm一次只打开一个程序
  20. Qt使用gtest进行C++单元测试-01

热门文章

  1. Struts 2 基础篇【转】
  2. 【DFS与BFS】洛谷 P1135 奇怪的电梯
  3. oracle 以SYSDBA远程连接数据库
  4. Linux基础命令---httpd守护进程
  5. 分布式系统为什么不用自增id,要用雪花算法生成id???
  6. MFC入门示例之组合框(CComboBox)、列表框(CListBox)
  7. Spring中Bean的装配方式
  8. ActiveMQ(二)——ActiveMQ的安装和基本使用
  9. 【Python】数据处理分析,一些问题记录
  10. dart系列之:浏览器中的舞者,用dart发送HTTP请求