How to convert between image formats in OpenCV

  • The cv::cvtColor function call can be used to convert between different image formats in OpenCV.

  • The header file to include is opencv2/opencv.hpp.

  • The library file to link with on Windows is opencv_imgproc249.lib for Release mode and opencv_imgproc249d.lib for Debug mode. This assumes that I am using OpenCV 2.4.9.

  • As an example of usage, to convert from RGB to BGR format:

cv::Mat in_mat; // Holds RGB image
cv::Mat out_mat; // Output in BGR format
cv::cvtColor(in_mat, out_mat, CV_RGB2BGR);
  • CV_RGB2GRAY convert 3-channel RGB image to 1-channel grayscale image.

Tried with: OpenCV 2.4.9 and Windows 7 Professional 64-bit


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s