Mat的C ++代码转换

时间:2014-04-01 06:43:21

标签: c++ image opencv image-processing computer-vision

我正在尝试将this second answer代码转换为c ++,我所做的并不是给我合适的结果,这是我的代码:

{
Mat img = imread("messi5.jpg");
int level_n = 2;
Mat p = Mat::zeros(img.cols*img.rows, 3, CV_32F);
vector<Mat> bgr;
    cv::split(img, bgr);
    //Divide each pixel color with 127 for level 2
    for(int i=0; i<img.cols*img.rows; i++) {
        p.at<float>(i,0) = bgr[0].data[i] / 127.0;
        p.at<float>(i,1) = bgr[1].data[i] / 127.0;
        p.at<float>(i,2) = bgr[2].data[i] / 127.0;
    }
vector<Mat> Img2 = p[bgr];
Mat out;
cv::transform(img,out,p);
imshow ("output" , out);
}

我没有理解的是我如何将这些颜色除以127除以矩阵,我错了?

我正在尝试的其他方式是

vector<Mat> bgr;
Mat blue , green , red;
    cv::split(img, bgr);
    blue = bgr[0]/127.0;
    if (blue > 128)
    {
        blue = 255;
    }
    else
    {
        blue = 0;
    }

红色和绿色相同

1 个答案:

答案 0 :(得分:1)

为什么不做到(2级):

Mat img = imread("messi5.jpg");
for(int i=0;i<img.rows;i++)
    for(int j=0;j<img.cols;j++) {
        cv::Vec3b p = img.at<cv::Vec3b>(i,j);
        for(int k = 0;k < img.channels();k++)
             p[k] = p[k] > 127 ? 255 : 0;
        img.at<cv::Vec3b>(i,j) = p;
}
// do whatever you want with processed image img