opencv-将分离合并图像(Red通道>125置255<=置0)

#include <iostream>
#include <opencv2/opencv.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <string>

using namespace std;
using namespace cv;

int main(){
string path = "F:/";
Mat img = imread(path+"000586.jpg");

Mat out_img = img.clone();
Mat_<Vec3b>::iterator it = out_img.begin<Vec3b>(); 
Mat_<Vec3b>::iterator itend = out_img.end<Vec3b>();
int a, b;
a = out_img.size[0];
b = out_img.size[1];
cout << a << " " <<b<< endl; //原图的宽和高

for (; it < itend; it++){ //对R通道的每个像素进行处理:大于125置255;小于或等于置0;
if ((*it)[0]>125) (*it)[0] = 255;
else (*it)[0] = 0;
}

vector<Mat> channels; //通道分离
split(img, channels);
Mat image1 = channels.at(0);

vector<Mat> channels2;
split(out_img, channels2);
Mat image2 = channels2.at(0);

namedWindow("ori");
imshow("ori", image1);
waitKey(0);

namedWindow("test");
imshow("test", image2);

//通道合并
Mat mergeImage;
merge(channels2, mergeImage);
imshow("合并", mergeImage);

waitKey(0);
return 0;
}

  

原文地址:https://www.cnblogs.com/byteHuang/p/8169187.html