图像通道的分离与合并

opencv使用split函数进行通道的分离,merge进行通道的合并

可以使用channels求出图像的通道数,示例如下:

ROI提取感兴趣的区域

 1 #include <opencv2/opencv.hpp>
 2 #include <iostream>
 3 
 4 using namespace cv;
 5 using namespace std;
 6 
 7 int main(int argc, char** argv) {
 8     Mat src = imread("D:/images/test5.png");
 9     if (src.empty()) {
10         printf("could not find image file");
11         return -1;
12     }
13     namedWindow("input", WINDOW_AUTOSIZE);
14 
15     vector<Mat> mv;
16     split(src, mv);
17     int size = mv.size();
18     printf("number of channels : %d 
", size);
19     imshow("blue channel", mv[0]);
20     imshow("green channel", mv[1]);
21     imshow("red channel", mv[2]);
22     bitwise_not(mv[0], mv[0]);
23     Mat dst;
24     merge(mv, dst);
25     imshow("result", dst);
26 
27     // ROI
28     Rect roi;
29     roi.x = 100;
30     roi.y = 100;
31     roi.width = 250;
32     roi.height = 200;
33     Mat sub = src(roi).clone();
34     bitwise_not(sub, sub);
35     rectangle(src, roi, Scalar(255, 255, 0), 1, 8);
36     imshow("roi", sub);
37     imshow("input", src);
38     waitKey(0);
39     destroyAllWindows();
40     return 0;
41 }

结果如下:

 

原文地址:https://www.cnblogs.com/djcsch2001/p/15424278.html