1

i was capturing live video from my web camera to Mat objects. is their any efficient way to convert a MAT object in to gray scaled image frame without using any API such as openCV... I have tried it using openCV. but i like to implement in to c++...is their any way to do it?

gamal
  • 1,587
  • 2
  • 21
  • 43
  • OpenCV is C++. Do you mean to do it from scratch? – Javi Sep 12 '14 at 18:41
  • I just want to implement a method which convert Mat object to gray scaled image in c++ language. is it possible to do that? i mean more faster than the openCV. – gamal Sep 12 '14 at 18:58
  • Do you think you can do it faster than openCV? The only way to do it faster is probably go with CUDA implementations. – Javi Sep 12 '14 at 19:00

2 Answers2

3

I would recommend you use OpenCV. OpenCV already contains optimized implementations for converting between various color spaces (i.e. even between RGB (actually BGR for OpenCV) to greyscale).

See for more details: http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html.

OpenCV is allready implemented in C++.

If you really want to implement you own for didactical purposes (I don't see any reason why you would do it otherwise) then the simple way to do it would be to iterate the R G B values in the Mat and apply the formula:

resultingVlue = 0.299 * R + 0.587 * G + 0.114 * B

(See also Stack overflow Question Converting RGB to grayscale/intensity for a more detailed discussion on why the R G B components typically get weighted differently)

Assuming here you want to convert RGB to gray. For other color space conversions, please look at the OpenCv documentation that also details how the transformations are done (see link provided above).

More so, OpenCV is open source. This means if you want to see how a optimal implementation might look like, you can download the source code and take a look.

Community
  • 1
  • 1
ds27680
  • 1,993
  • 10
  • 11
0

Google tells me that you have to average the values of the R,G and B values of each pixel. Some algorithms are discussed here

http://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/

The simplest is to convert each color R, G and B values by the average (R+G+B)/3. Check the above links for the results of a few different averages.

rpsml
  • 1,486
  • 14
  • 21