首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

用OpenCV实现Photoshop算法(二): 图像剪切

  • 25-02-19 08:21
  • 2810
  • 10612
blog.csdn.net

系列文章:

用OpenCV实现Photoshop算法(一): 图像旋转

用OpenCV实现Photoshop算法(二): 图像剪切

用OpenCV实现Photoshop算法(三): 曲线调整

用OpenCV实现Photoshop算法(四): 色阶调整

用OpenCV实现Photoshop算法(五): 亮度对比度调整

用OpenCV实现Photoshop算法(六): 变为黑白图像

用OpenCV实现Photoshop算法(七): 调整色相饱和度

用OpenCV实现Photoshop算法(八): 可选颜色

用OpenCV实现Photoshop算法(九): 高反差保留


二、图像剪切

用OpenCV 写一个图像剪切函数 imageCrop() 如下:

  1. //图像剪切
  2. //参数:src为源图像, dst为结果图像, rect为剪切区域
  3. //返回值:返回0表示成功,否则返回错误代码
  4. int imageCrop(InputArray src, OutputArray dst, Rect rect)
  5. {
  6. Mat input = src.getMat();
  7. if( input.empty() ) {
  8. return -1;
  9. }
  10. //计算剪切区域: 剪切Rect与源图像所在Rect的交集
  11. Rect srcRect(0, 0, input.cols, input.rows);
  12. rect = rect & srcRect;
  13. if ( rect.width <= 0 || rect.height <= 0 ) return -2;
  14. //创建结果图像
  15. dst.create(Size(rect.width, rect.height), src.type());
  16. Mat output = dst.getMat();
  17. if ( output.empty() ) return -1;
  18. try {
  19. //复制源图像的剪切区域 到结果图像
  20. input(rect).copyTo( output );
  21. return 0;
  22. } catch (...) {
  23. return -3;
  24. }
  25. }


然后,编写测试程序如下:

  1. #include
  2. #include "opencv2/core.hpp"
  3. #include "opencv2/imgproc.hpp"
  4. #include "opencv2/highgui.hpp"
  5. using namespace std;
  6. using namespace cv;
  7. //图像剪切
  8. //参数:src为源图像, dst为结果图像, rect为剪切区域
  9. //返回值:返回0表示成功,否则返回错误代码
  10. int imageCrop(InputArray src, OutputArray dst, Rect rect)
  11. {
  12. Mat input = src.getMat();
  13. if( input.empty() ) {
  14. return -1;
  15. }
  16. //计算剪切区域: 剪切Rect与源图像所在Rect的交集
  17. Rect srcRect(0, 0, input.cols, input.rows);
  18. rect = rect & srcRect;
  19. if ( rect.width <= 0 || rect.height <= 0 ) return -2;
  20. //创建结果图像
  21. dst.create(Size(rect.width, rect.height), src.type());
  22. Mat output = dst.getMat();
  23. if ( output.empty() ) return -1;
  24. try {
  25. //复制源图像的剪切区域 到结果图像
  26. input(rect).copyTo( output );
  27. return 0;
  28. } catch (...) {
  29. return -3;
  30. }
  31. }
  32. //======================== 主程序开始 ==========================
  33. static string window_name = "Draw a Rect to crop";
  34. static Mat src; //源图片
  35. bool isDrag = false;
  36. Point point1; //矩形的第一个点
  37. Point point2; //矩形的第二个点
  38. static void callbackMouseEvent(int mouseEvent, int x, int y, int flags, void* param)
  39. {
  40. switch(mouseEvent) {
  41. case CV_EVENT_LBUTTONDOWN:
  42. point1 = Point(x,y);
  43. point2 = Point(x,y);
  44. isDrag = true;
  45. break;
  46. case CV_EVENT_MOUSEMOVE:
  47. if ( isDrag ) {
  48. point2 = Point(x,y);
  49. Mat dst = src.clone();
  50. Rect rect (point1, point2); //得到矩形
  51. rectangle(dst, rect, Scalar(0,0,255));//画矩形
  52. imshow(window_name, dst); //显示图像
  53. }
  54. break;
  55. case CV_EVENT_LBUTTONUP:
  56. if (isDrag) {
  57. isDrag = false;
  58. Rect rect (point1, point2); //得到矩形
  59. imageCrop(src, src, rect); //图像剪切
  60. imshow(window_name, src); //显示图像
  61. }
  62. break;
  63. }
  64. return;
  65. }
  66. int main()
  67. {
  68. //read image file
  69. src = imread("building.jpg");
  70. if ( !src.data ) {
  71. cout << "error read image" << endl;
  72. return -1;
  73. }
  74. //create window
  75. namedWindow(window_name);
  76. imshow(window_name, src);
  77. //set mouse event call back
  78. setMouseCallback(window_name, callbackMouseEvent, NULL );
  79. waitKey();
  80. return 0;
  81. }

运行结果,画一个框后,切下,OK




注:本文转载自blog.csdn.net的JoStudio的文章"http://blog.csdn.net/c80486/article/details/52498539"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top