Ask Your Question

Easy BGR to Grayscale conversion

asked 2014-05-24 16:53:22 -0600

Marsstar gravatar image

I want to convert an image to Grayscale using this simple code using the new C++ API --> found on the OpenCV original documentation page.


I don't know which one is a placeholder and which one is not.

The changes I made are marked by a pink rectangle. -Are those changes correct?

This is the original code, that was online at the documentation page:

image description

This is the changed code by myself:

image description

Do I have to link the cv.h or cv.hpp, because I've heard, that the GCC compiler only takes .hpp files since its made for C++ and not C. Am I correct?


edit retag flag offensive close merge delete



hi alex,

  • there is no <opencv/cv.hpp> header, you need <opencv2/core/core.hpp> and <opencv2/imgproc/imgproc.hpp>

  • if you want to hardcode the image names, skip the 1st line in main (it's wrong anyway).

and the next time, screenshots look nice, but we can't copy/paste your code to correct it

berak gravatar imageberak ( 2014-05-24 23:19:27 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-05-26 03:51:30 -0600

FLY gravatar image

Let me guide you in a simple way , it looks that you want to move from command line input to the input with in program (I don't know what to call it :) ) , you simple need to do the following in your code

Mat img = imread ("E:\\lena.jpg");       // input image
Mat gray_img;                           // image where result will save
cvtColor(img , gray_img ,CV_BGR2GRAY) ; // this line convert your image from BGR2GRAY

imwrite("E:\\gray_img.jpg" , gray_img);   // is used to save the image in your directory

imshow("img" , gray_img) ;   // this line show you the gray image as output

@berak already guide you about headers

edit flag offensive delete link more

Question Tools


Asked: 2014-05-24 16:53:22 -0600

Seen: 1,482 times

Last updated: May 26 '14