你好,你应该看看:
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int ac, char **av){
cv::Mat src = cv::Mat(Size(1920, 1080), CV_8UC3, Scalar(0, 0, 0));
std::vector<cv::Mat> rgbChannels;
cv::cvtColor(src.clone(), src, CV_BGR2RGB);
cv::split(src, rgbChannels);
cv::Mat r = rgbChannels.at(0);
cv::Mat g = rgbChannels.at(1);
cv::Mat b = rgbChannels.at(2);
unsigned char max1, max2, min1, min2, R=0,G=0,B=0;
for (unsigned int y =0; y < r.rows; y++){
for (unsigned int x =0; x < r.cols; x++){
max1= (R>G)? R:G;
max2 = (max1>B)? max1:B;
min1= (R<G)? R:G;
min2 = (min1<B)? min1:B;
if( R>95 && G>40 && B> 20 && (max2-min2)>15 && (R-G)>15 && R>G && R>B) {
r.at<uchar>(y, x)=R;
g.at<uchar>(y, x)=G;
b.at<uchar>(y, x)=B;
}
}
}
rgbChannels.clear();
rgbChannels.push_back(r);
rgbChannels.push_back(g);
rgbChannels.push_back(b);
cv::Mat RGBoutput;
cv::merge(rgbChannels, RGBoutput);
cv::imwrite("output.png", RGBoutput);
return 0;
}
顺便说一句,如果您想对颜色进行操作,您应该查看 HSV 颜色空间以获得更精确的结果。