Ask Your Question
0

Code conversion in opencv C++

asked 2014-04-01 02:48:18 -0500

FLY gravatar image

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

edit retag flag offensive close merge delete

Comments

the 'special' case for level 2 is actually simple:

img = img > 127; // done ! ;)

berak gravatar imageberak ( 2014-04-01 03:49:10 -0500 )edit

but berak when i do it like if (img = img&gt;127) { img == 255 } because i want to fix that value to 255 if its greater then 127 , but it show me error on if

FLY gravatar imageFLY ( 2014-04-01 03:54:54 -0500 )edit
1

yea, your if() is wrong.

don't take above too serious. though it works 'as is', it's more like the 1st answer, not the 'general' case

berak gravatar imageberak ( 2014-04-01 03:58:27 -0500 )edit

its done , thanks

FLY gravatar imageFLY ( 2014-04-01 04:28:12 -0500 )edit

but its img=img>=128; img == 255; img = img< 128; img == 0; not giving me fruitfull result like them

FLY gravatar imageFLY ( 2014-04-01 04:44:08 -0500 )edit

Mat img = imread("messi5.jpg");

img = img > 127;

imshow("!",img);

berak gravatar imageberak ( 2014-04-01 04:48:47 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
3

answered 2014-04-01 06:25:55 -0500

berak gravatar image

updated 2014-04-01 06:33:15 -0500

the same palette idea abid posted on SO, redone in c++ ;)

void posterize(const Mat &img, Mat &res, int bins)
{
    int q = 256/bins;
    Mat_<uchar> pal(1,256); // make a lookup table for intensities
    for ( int c=0; c<256; c++ )
        pal(c) = saturate_cast<uchar>((((c/q) * q) * bins)/(bins-1));
    cerr << pal<< endl;
    LUT(img,pal,res);      // apply the lookup (swap pixel val & lookup val)
}

int main( int argc, const char** argv )
{
    Mat img = imread("messi5.jpg");
    posterize(img,img,8);
    imshow("result",img);
    waitKey();
}

image description

edit flag offensive delete link more
1

answered 2014-04-02 02:19:40 -0500

FLY gravatar image

It can also be done in this way like i got answer on SO here

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

But yes the above answer of @berak is more appropriate and better

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2014-04-01 02:48:18 -0500

Seen: 260 times

Last updated: Apr 02 '14