Ask Your Question
0

Implementation of Matlab function rgb2ntsc in opencv using c++

asked 2015-09-04 05:00:20 -0600

Tess gravatar image

updated 2015-09-04 05:05:18 -0600

I am trying to implement rgb2ntsc colorspace conversion in OpenCV using C++.

for(int i=0;i<rn;i++) {
    for(int j=0;j<cn;j++) {
        Vec3b intensity = image.at<Vec3b>(i,j);
        R = intensity.val[2];
        G = intensity.val[1];
        B = intensity.val[0];
        Y = 0.299*R+0.587*G+0.114*B;
        I = 0.596*R-0.274*G-0.322*B;
        Q = 0.211*R-0.523*G+0.312*B;
        Result.at<Vec3b>(i,j)[0] = (float)Y;
        Result.at<Vec3b>(i,j)[1] = (float)I;
        Result.at<Vec3b>(i,j)[2] = (float)Q;
    }
}

I have done this for the implementation of rgb2ntsc. But my output image is not similar to the output of Matlab's rgb2ntsc function. Here are my input image, output image and matlab output image. Can I do any additional operation?

input image My output image Matlab output image

edit retag flag offensive close merge delete

Comments

maybe this helps cvtcolor

pklab gravatar imagepklab ( 2015-09-04 05:34:04 -0600 )edit
1

you need saturate_cast , before reassigning to uchar values, to avoid over/underflow

then, maybe you should better use transform , instead of those for loops (it also does automatic saturation)

berak gravatar imageberak ( 2015-09-04 06:15:49 -0600 )edit

@berak:transform, helped me.... thanks.

Tess gravatar imageTess ( 2015-09-04 13:37:01 -0600 )edit
1

oh, just curious, - what's your final transform matrix ? (did not make a real answer (hey, @pklab ;), because my efforts did not look exactly the same as yours.

berak gravatar imageberak ( 2015-09-04 14:03:17 -0600 )edit

in other words, @Tess , if you could make an answer (or a comment, we'd convert it) - highly welcome !

berak gravatar imageberak ( 2015-09-04 14:04:24 -0600 )edit

touché :) my comment want to become an answer but I was feeling it as partial so it remains at the background as comment.Thanks to @berak to have laid out the transform funct.

pklab gravatar imagepklab ( 2015-09-06 10:37:21 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2015-09-04 14:44:24 -0600

Tess gravatar image

updated 2015-09-04 15:00:38 -0600

berak gravatar image

@ berak :Code:-

Mat image =imread(argv[1],1);
Mat k=(Mat_<float>(3,3) << 0.299,0.587,0.114, 0.596,-0.274,-0.322,0.211,-0.523,0.312);
Mat Result;
cvtColor(image,image,CV_BGR2RGB);
transform(image,Result,k); 
cvtColor(Result,Result,CV_RGB2BGR);
edit flag offensive delete link more

Comments

i'm still sure, there is a way to rearrange the transform Mat, so you don't need both cvtColor calls...

berak gravatar imageberak ( 2015-09-04 15:04:01 -0600 )edit
1

@berak: as simple as swapping 1st and 3rd columns of the Mat

LorenaGdL gravatar imageLorenaGdL ( 2015-09-04 15:16:44 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-09-04 04:57:51 -0600

Seen: 549 times

Last updated: Sep 04 '15