Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/Contour-Detection-using-OpenCV/cpp/channel_experiments/channel_experiments.cpp
3150 views
1
#include<opencv2/opencv.hpp>
2
#include <iostream>
3
4
using namespace std;
5
using namespace cv;
6
7
int main() {
8
// read the image
9
Mat image = imread("../../input/image_1.jpg");
10
11
// B, G, R channel splitting
12
Mat channels[3];
13
split(image, channels);
14
15
// detect contours using blue channel and without thresholding
16
vector<vector<Point>> contours1;
17
vector<Vec4i> hierarchy1;
18
findContours(channels[0], contours1, hierarchy1, RETR_TREE, CHAIN_APPROX_NONE);
19
// draw contours on the original image
20
Mat image_contour_blue = image.clone();
21
drawContours(image_contour_blue, contours1, -1, Scalar(0, 255, 0), 2);
22
imshow("Contour detection using blue channels only", image_contour_blue);
23
waitKey(0);
24
imwrite("blue_channel.jpg", image_contour_blue);
25
destroyAllWindows();
26
27
// detect contours using green channel and without thresholding
28
vector<vector<Point>> contours2;
29
vector<Vec4i> hierarchy2;
30
findContours(channels[1], contours2, hierarchy2, RETR_TREE, CHAIN_APPROX_NONE);
31
// draw contours on the original image
32
Mat image_contour_green = image.clone();
33
drawContours(image_contour_green, contours2, -1, Scalar(0, 255, 0), 2);
34
imshow("Contour detection using green channels only", image_contour_green);
35
waitKey(0);
36
imwrite("green_channel.jpg", image_contour_green);
37
destroyAllWindows();
38
39
// detect contours using red channel and without thresholding
40
vector<vector<Point>> contours3;
41
vector<Vec4i> hierarchy3;
42
findContours(channels[2], contours3, hierarchy3, RETR_TREE, CHAIN_APPROX_NONE);
43
// draw contours on the original image
44
Mat image_contour_red = image.clone();
45
drawContours(image_contour_red, contours3, -1, Scalar(0, 255, 0), 2);
46
imshow("Contour detection using red channels only", image_contour_red);
47
waitKey(0);
48
imwrite("red_channel.jpg", image_contour_red);
49
destroyAllWindows();
50
}
51
52