Path: blob/master/Contour-Detection-using-OpenCV/cpp/channel_experiments/channel_experiments.cpp
3150 views
#include<opencv2/opencv.hpp>1#include <iostream>23using namespace std;4using namespace cv;56int main() {7// read the image8Mat image = imread("../../input/image_1.jpg");910// B, G, R channel splitting11Mat channels[3];12split(image, channels);1314// detect contours using blue channel and without thresholding15vector<vector<Point>> contours1;16vector<Vec4i> hierarchy1;17findContours(channels[0], contours1, hierarchy1, RETR_TREE, CHAIN_APPROX_NONE);18// draw contours on the original image19Mat image_contour_blue = image.clone();20drawContours(image_contour_blue, contours1, -1, Scalar(0, 255, 0), 2);21imshow("Contour detection using blue channels only", image_contour_blue);22waitKey(0);23imwrite("blue_channel.jpg", image_contour_blue);24destroyAllWindows();2526// detect contours using green channel and without thresholding27vector<vector<Point>> contours2;28vector<Vec4i> hierarchy2;29findContours(channels[1], contours2, hierarchy2, RETR_TREE, CHAIN_APPROX_NONE);30// draw contours on the original image31Mat image_contour_green = image.clone();32drawContours(image_contour_green, contours2, -1, Scalar(0, 255, 0), 2);33imshow("Contour detection using green channels only", image_contour_green);34waitKey(0);35imwrite("green_channel.jpg", image_contour_green);36destroyAllWindows();3738// detect contours using red channel and without thresholding39vector<vector<Point>> contours3;40vector<Vec4i> hierarchy3;41findContours(channels[2], contours3, hierarchy3, RETR_TREE, CHAIN_APPROX_NONE);42// draw contours on the original image43Mat image_contour_red = image.clone();44drawContours(image_contour_red, contours3, -1, Scalar(0, 255, 0), 2);45imshow("Contour detection using red channels only", image_contour_red);46waitKey(0);47imwrite("red_channel.jpg", image_contour_red);48destroyAllWindows();49}505152