c++ opencv roi

举报
风吹稻花香 发表于 2021/06/06 00:13:22 2021/06/06
【摘要】   最新的: cv::Mat img; a= cv::Mat m0 = img(cv::Rect(i0, i1, w, h)) 以前的:  //opencv#include "opencv2/opencv.hpp"#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/im...

 


最新的:
cv::Mat img;
a=
cv::Mat m0 = img(cv::Rect(i0, i1, w, h))

以前的: 


  
  1. //opencv
  2. #include "opencv2/opencv.hpp"
  3. #include "opencv2/highgui/highgui.hpp"
  4. #include "opencv2/imgproc/imgproc.hpp"
  5. /******************************************************************************************
  6. Function: Screenshot
  7. Description: 矩形截图
  8. Input: src:原图片 rect:截图范围
  9. Output: dst:截图后的图片
  10. Return: 截图成功返回true,失败返回false
  11. *******************************************************************************************/
  12. bool Screenshot(IplImage* src, IplImage* dst, CvRect rect)
  13. {
  14. try {
  15. cvSetImageROI(src, rect);
  16. cvCopy(src, dst, 0);
  17. cvResetImageROI(src);
  18. return true;
  19. }
  20. catch (cv::Exception e)
  21. {
  22. }
  23. }
  24. /******************************************************************************************
  25. Function: SafeResetSizeOfRect
  26. Description: 安全重置矩形大小
  27. Input: src:原图片 rect:截图范围
  28. Return: 无
  29. *******************************************************************************************/
  30. void SafeResetSizeOfRect(IplImage* src, CvRect& rect)
  31. {
  32. try
  33. {
  34. rect.x = rect.x < 0 ? 0 : rect.x;
  35. rect.y = rect.y < 0 ? 0 : rect.y;
  36. rect.width = rect.width < 0 ? 0 : rect.width;
  37. rect.height = rect.height < 0 ? 0 : rect.height;
  38. if (rect.x > src->width || rect.y > src->height)
  39. {
  40. rect = cvRect(0, 0, src->width, src->height);
  41. }
  42. rect.width = std::min(rect.width, src->width - rect.x);
  43. rect.height = std::min(rect.height, src->height - rect.y);
  44. }
  45. catch (cv::Exception e)
  46. {
  47. }
  48. }
  49. //调用举例
  50. IplImage *src = 0;
  51. IplImage *dst = 0;
  52. src = cvLoadImage("D:\\1.jpg", CV_LOAD_IMAGE_COLOR);
  53. //定义截图范围
  54. CvRect rect = cvRect(720, 610, 300, 320);
  55. //截图
  56. SafeResetSizeOfRect(src, rect);
  57. dst = cvCreateImage(cvSize(rect.width, rect.height), src->depth, src->nChannels);
  58. Screenshot(src, dst, rect);
  59. //保存图片
  60. cvSaveImage("D:\\rect.jpg", dst);
  61. //释放内存
  62. cvReleaseImage(&src);
  63. cvReleaseImage(&dst);

 

文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/106267174

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。