OpenCV截取图像ROI,任意形状
2015-12-16 11:37
513 查看
//1.First create mask image as described above
//2.Copy the source image to new Mat dst1 using the mask
//3.Invert your mask and copy destination image to a new Mat dst2
//4.For final result just add up dest1 and dest2 to new Mat
Mat src = imread("src.jpg",1);
Mat dst = imread("dest.bmp",1);
int new_w = 0;
int new_h = 0;
if(src.cols >dst.cols)
new_w = dst.cols;
else
new_w = src.cols;
if(src.rows>dst.rows)
new_h=dst.rows;
else
new_h=src.rows;
Rect rectROI(0,0,new_w,new_h);
Mat mask(new_h, new_w, CV_8UC1, cv::Scalar(0));
Point P1(107,41);
Point P2(300,61);
Point P3(250,280);
Point P4(110,253);
vector< vector<Point> > co_ordinates;
co_ordinates.push_back(vector<Point>());
co_ordinates[0].push_back(P1);
co_ordinates[0].push_back(P2);
co_ordinates[0].push_back(P3);
co_ordinates[0].push_back(P4);
drawContours( mask,co_ordinates,0, Scalar(255),CV_FILLED, 8 );
Mat srcROI=src(rectROI);
Mat dstROI=dst(rectROI);
Mat dst1;
Mat dst2;
srcROI.copyTo(dst1,mask);
imwrite("dst1.jpg",dst1);
bitwise_not(mask,mask);
dstROI.copyTo(dst2,mask);
dstROI.setTo(0);
dstROI=dst1+dst2;
imshow("final result",dst);
result:
//2.Copy the source image to new Mat dst1 using the mask
//3.Invert your mask and copy destination image to a new Mat dst2
//4.For final result just add up dest1 and dest2 to new Mat
Mat src = imread("src.jpg",1);
Mat dst = imread("dest.bmp",1);
int new_w = 0;
int new_h = 0;
if(src.cols >dst.cols)
new_w = dst.cols;
else
new_w = src.cols;
if(src.rows>dst.rows)
new_h=dst.rows;
else
new_h=src.rows;
Rect rectROI(0,0,new_w,new_h);
Mat mask(new_h, new_w, CV_8UC1, cv::Scalar(0));
Point P1(107,41);
Point P2(300,61);
Point P3(250,280);
Point P4(110,253);
vector< vector<Point> > co_ordinates;
co_ordinates.push_back(vector<Point>());
co_ordinates[0].push_back(P1);
co_ordinates[0].push_back(P2);
co_ordinates[0].push_back(P3);
co_ordinates[0].push_back(P4);
drawContours( mask,co_ordinates,0, Scalar(255),CV_FILLED, 8 );
Mat srcROI=src(rectROI);
Mat dstROI=dst(rectROI);
Mat dst1;
Mat dst2;
srcROI.copyTo(dst1,mask);
imwrite("dst1.jpg",dst1);
bitwise_not(mask,mask);
dstROI.copyTo(dst2,mask);
dstROI.setTo(0);
dstROI=dst1+dst2;
imshow("final result",dst);
result:
相关文章推荐
- Centos 6.7 安装RStudio server遇到的问题总结
- CentOS 7升级内核版本后VirtualBox无法启动问题
- win7(64)+vs2010+opencv2.3.1配置问题:应用程序无法正常启动0xc000007b
- Linux Qt工程组织结构与动态库引用总结
- 期中架构心得
- 如何才能成为一名架构师?
- 如何才能成为一名架构师?
- nginx的安装、配置和使用
- 使用@property - 廖雪峰的官方网站
- 在阿里云的CentOS环境中安装配置MySQL的教程
- tomcat与android应用端口交互问题
- 关于 tomcat 集群中 session 共享的三种方法
- vwampserver2.5-apache2.4.9允许外部访问的配置
- RunLoop和线程的关系
- centos虚拟机移动之后,可能会发生网卡无法启动的情况
- PPT分享: Linux启动流程 关于initrd与initramfs的区分及其发展历程
- 实战企业监控Zabbix 2.4.7
- linux 磁盘操作
- linux中阻塞和非阻塞的区别
- Linux 常见文件及目录解析1.0(运维人员必看)