1

I am trying to convert this second answer code into c++ , What I did is not giving me appropriate result , here is my code :

{
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);
}

What I didn't understand is how I put these colour's which I divided by 127 into Matrix , where I am going wrong?

Other way i am trying is

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

same for red and green

Community
  • 1
  • 1
AHF
  • 1,070
  • 2
  • 15
  • 47
  • 1
    "What I didn't understand is how I put these colour's which I divided by 127 into Matrix", to which matrix ?, you mean `vector Img2` – Haris Apr 01 '14 at 06:54
  • no I mean these values on that image , you may better understand when you visit the link I mentioned – AHF Apr 01 '14 at 07:03

1 Answers1

1

Why don't just do it in place (for level 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
marol
  • 4,034
  • 3
  • 22
  • 31
  • I want to apply the same thing on 8UC4 images , input in 8UC4 and output is 8UC4 too , i am using opencv with java in android (android ndk) , i change Vec3b to Vec4b but still no output , what need to change ? – Rocket Aug 10 '14 at 19:12
  • @Munieb It shouln't be a problem. Could you describe more in details what problems you face when you change Vec3b to Vec4b? – marol Aug 11 '14 at 14:49
  • its just not showing the output , but i do it in alternate way – Rocket Aug 11 '14 at 16:42