6/09/2016

OpenCV Mat copyTo, Clone, ROI example code


OpenCV Mat copyTo, Clone, ROI example code

...
Mat img = imread("ss.jpg");
 
 Rect r(img.cols / 4, img.rows / 4, img.cols / 4 * 2, img.rows / 4 * 2);

 //clone #1
 Mat img2 = img.clone();
 bitwise_not(img2, img2);

 //clone #2
 Mat img5 = img(r).clone();


 //copyTo #1
 Mat cimg;
 img.copyTo(cimg);

 //copyTo #2
 Mat cimg2;
 img(r).copyTo(cimg2);

 //copyTo #3
 Mat cimg3( Size(img.cols*2, img.rows), img.type() );
 cimg3.setTo(255);
 img.copyTo(cimg3(Range::all(), Range(0, img.cols)));
 img2.copyTo(cimg3(Range::all(), Range(img2.cols, img2.cols * 2)));

 //set roi
 Mat roi(img, r);
 //invert color
 bitwise_not(roi, roi);


 namedWindow("img2", 0); //make window
 imshow("img2", cimg); //show
 namedWindow("img3", 0); //make window
 imshow("img3", cimg2); //show
 namedWindow("img4", 0); //make window
 imshow("img4", cimg3); //show
 namedWindow("img5", 0); //make window
 imshow("img5", img5); //show
 namedWindow("img", 0); //make window
 imshow("img", img); //show
 
 waitKey(0);

...



No comments:

Post a Comment