# Implementation of Matlab function rgb2ntsc in opencv using c++

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?

maybe this helps cvtcolor

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:transform, helped me.... thanks.

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.

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

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.