cv03_rect.cpp 960 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include <opencv2/opencv.hpp>
  2. #pragma comment(lib, "opencv_world4100.lib")
  3. using namespace cv;
  4. int main()
  5. {
  6. Mat image = imread("./img/lena.webp");
  7. Rect rect1(256, 256, 128, 128);
  8. Rect rect2(224, 224, 128, 128);
  9. Mat roi1;
  10. image(rect1).copyTo(roi1); // 将image中rect1的部分复制到roi1中
  11. imshow("1", roi1);
  12. waitKey(0);
  13. Mat roi2;
  14. image(rect2).copyTo(roi2); // 将image中rect2复制到roi2中
  15. imshow("2", roi2);
  16. waitKey(0);
  17. cv::Rect rect3 = rect1 & rect2; // rect1和rect2的交集
  18. Mat roi3;
  19. image(rect3).copyTo(roi3);
  20. imshow("3", roi3);
  21. waitKey(0);
  22. Rect rect4 = rect1 | rect2; // rect1和rect2的并集(两幅图的最小外接矩形)
  23. Mat roi4;
  24. image(rect4).copyTo(roi4);
  25. imshow("4", roi4);
  26. waitKey(0);
  27. Rect rect5(10, 10, 128, 128);
  28. roi1.copyTo(image(rect5)); // 将rect1复制到image的指定区域rect5中
  29. imshow("5", image);
  30. waitKey(0);
  31. }