欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > [C++][opencv]基于opencv实现photoshop算法图像剪切

[C++][opencv]基于opencv实现photoshop算法图像剪切

2024/10/24 13:24:15 来源:https://blog.csdn.net/FL1623863129/article/details/141141920  浏览:    关键词:[C++][opencv]基于opencv实现photoshop算法图像剪切

【测试环境】

vs2019

opencv==4.8.0

【效果演示】

【核心实现代码】


//图像剪切
//参数:src为源图像, dst为结果图像, rect为剪切区域
//返回值:返回0表示成功,否则返回错误代码
int imageCrop(InputArray src, OutputArray dst, Rect rect)
{Mat input = src.getMat();if (input.empty()) {return -1;}//计算剪切区域:  剪切Rect与源图像所在Rect的交集Rect srcRect(0, 0, input.cols, input.rows);rect = rect & srcRect;if (rect.width <= 0 || rect.height <= 0) return -2;//创建结果图像dst.create(Size(rect.width, rect.height), src.type());Mat output = dst.getMat();if (output.empty()) return -1;try {//复制源图像的剪切区域 到结果图像input(rect).copyTo(output);return 0;}catch (...) {return -3;}
}//========================  主程序开始 ==========================static string window_name = "Draw a Rect to crop";
static Mat src;  //源图片
bool  isDrag = false;
Point point1; //矩形的第一个点
Point point2; //矩形的第二个点static void callbackMouseEvent(int mouseEvent, int x, int y, int flags, void* param)
{switch (mouseEvent) {case EVENT_LBUTTONDOWN:point1 = Point(x, y);point2 = Point(x, y);isDrag = true;break;case EVENT_MOUSEMOVE:if (isDrag) {point2 = Point(x, y);Mat dst = src.clone();Rect rect(point1, point2); //得到矩形rectangle(dst, rect, Scalar(0, 0, 255));//画矩形imshow(window_name, dst); //显示图像}break;case EVENT_LBUTTONUP:if (isDrag) {isDrag = false;Rect rect(point1, point2); //得到矩形imageCrop(src, src, rect); //图像剪切imshow(window_name, src); //显示图像}break;}return;
}

 【完整演示代码下载】

https://download.csdn.net/download/FL1623863129/89633023

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com