Recently in doing OPENCV multi-channel operation found on-line examples are very few, in order to facilitate the memory of their own write a demo.
Example in VS2010 + OpenCV 2.4.9 compiled through, as long as the configuration of the OpenCV Lib path can be run.
This method is suitable for C + + operations under Windows/linux, both Windows and Ubuntu have tried.
#include <opencv2/opencv.hpp> #include <iostream> #pragma comment (lib, "Opencv_core249d.lib") using Namespace Std;using namespace Cv;void testchannels () {filestorage fs;string file = "Channel_methods.xml"; Fs.open (file, Filestorage::write), if (!fs.isopened ()) {Cerr << "error:could not open '" << file << Endl;} vec2f need to correspond to the number of channels defined by the matrix and data type CV_32FC2, if the 4-channel float type should be defined as vec4f//the first usage of Mat code (1, ten, CV_32FC2, Scalar::all (0)); mat_<vec2f> _code = code; for (int k = 0; k < k++) {_code (0, k) [0]++; _code (0, k) [1]=1;} Code = _CODE;FS << "method_1st" << code;//The second usage of the Mat code_2 (1, ten, CV_32FC2, Scalar::all (0)); for (int k = 0; K & Lt 10; k++) {code_2.at<vec2f> (0, k) [0]++; code_2.at<vec2f> (0, k) [1]=2;} FS << "method_2nd" << code_2;//third usage vector<mat> split_code;split (code, Split_code); for (int k = 0; K < ; 10; k++) {split_code[0].at<float> (0, K) + +; split_code[1].at<float> (0, k) = 3;} Mat Merge_code (1, CV_32FC2, Scalar:: All (0)), merge (Split_code, Merge_code), FS << "method_3rd" << merge_code;//release resources such as Code.release ();}int Main (int argc, char** argv) {testchannels ();}
OPENCV Mat Multi-Channel C + + operation method (for Windows/linux)