2013-06-25 38 views
-2

我有一个后续代码B像素图片:生成从R,G,在C++

double R = (double)(img->imageData + img->widthStep*i)[j*3]; 
    double G = (double)(img->imageData + img->widthStep*i)[j*3+1]; 
    double B = (double)(img->imageData + img->widthStep*i)[j*3+2]; 

,然后我把一些条件为R,G,B。 现在我想要生成新的新的图像像素值R,G,B在C++中。

非常感谢您的帮助!

回答

0

嗨,你应该在那看一看:

#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颜色空间的更精确的结果。

+0

嗨。非常感谢你的回答。但在这种情况下,我想从相机显示图像,然后我改变一些值(R,G,B)并将该像素设置为从相机接收的当前图像。我怎样才能做到这一点? –

+0

使用VideoCapture修改您的框架和我给予您的代码片段,并对修改后的框架执行cv :: imshow。 (看看这里的视频截图:http://opencv.willowgarage.com/documentation/cpp/reading_and_writing_images_and_video.html) – Poko

+0

hii。我想从你的codea/s中检测皮肤区域:r = rgbChannels.at(0); g = rgbChannels.at(1); b = rgbChannels.at(2); 无符号字符max1,max2,min1,min2,R = 0,G = 0,B = 0; (unsigned int y = 0; y G)? R:G; \t max2 =(max1> B)? MAX1:B; \t min1 =(R 95&amp; G> 40 && B> 20 &&(max2-min2)> 15 &&(RG)> 15 && R& )= R; \t \t g.at (y,x)= G; \t \t b.at (y,x)= B; \t} 但合并后,图像不会改变任何东西。 –