RGB to Gray Image

Hi, can anyone tell me how to convert RGB image into Gray Scale Image using C++? Thanks...

I've tried it using C++ in OpenCv but don't know how to show it instead of save the gray image? TQ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <opencv/highgui.h>
#include <opencv/cv.h>
int main( int argc, char** argv ) {
IplImage* img = cvLoadImage("C:/Users/123/Desktop/PROJECT/1.jpg");

IplImage *destination = cvCreateImage( 
cvSize( img->width, img->height ), IPL_DEPTH_8U, 1 );
cvCvtColor( img, destination, CV_RGB2GRAY );
cvSaveImage( "C:/Users/123/Desktop/1ed.jpg", destination );

cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
}
Last edited on
cv::cvtColor(original, gray, CV_BGR2GRAY);
Check out cv::imshow()

¿does it help?
Hi ne555, I'm actually new in OpenCV, can I know why u use cv::cvtColor(original, gray, CV_BGR2GRAY); instead of cvCvtColor( img, destination, CV_RGB2GRAY );? I mean why u use this cv::cvtColor instead of cvCvtColor?

And how to use imshow in OpenCV? isn't it a Matlab command? Thanks...^^
1
2
3
/*! \namespace cv
    Namespace where all the C++ OpenCV functionality resides
*/
A namespace is used to avoid name collision. That way I can tell what function I want to use (which vendor, or category by instance)

I'm using opencv2, didn't realize that you were using the ¿previous? headers (I think they are C)
cv::imshow() seems to be equivalent to cvShowImage(). You already used it, ¿what is the problem?

http://opencv.willowgarage.com/documentation/cpp/highgui_user_interface.html?highlight=imshow#imshow
http://opencv.willowgarage.com/documentation/user_interface.html#showimage
Thanks...

opencv and opencv2 are different right?
my opencv have many library ("highgui", "cv" and so on) but opencv2 only have one library call "opencv"...so ur opencv2 have many library is it?

-----------------------------------------------------------------------------------

If I use using namespace cv;

so at the program, I only need to type CvtColor instead of cvCvtColor is it?
[Just wanted to know more as many online sources use this way, but I don't understand it, hope u can help]

-----------------------------------------------------------------------------------

actually for both of the link above, i got read it, but don't understand how to use them...

i solve my problem with the code below...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <opencv/highgui.h>
#include <opencv/cv.h>

int main( int argc, char** argv ) {
IplImage* img = cvLoadImage("C:/Users/123/Desktop/PROJECT/1.jpg");

IplImage *destination = cvCreateImage( 
cvSize( img->width, img->height ), IPL_DEPTH_8U, 1 );
cvCvtColor( img, destination, CV_RGB2GRAY );

cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );

cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
cvShowImage("Example2", destination);

cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
}


Now, I'm able to display both RGB and Gray image on the screen.

Just curious how to use imshow() inside my program above [just another knowledge if u willing to help me]

Hope to get answer from u for all my 3 questions above...

Thanks a lot...^^
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// file opencv2/opencv.hpp
#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp" 
You could also use those includes directly, if you want.

If I use using namespace cv;
so at the program, I only need to type CvtColor instead of cvCvtColor is it?
Not quite. You don't have any namespace.
In case that you would, then you could replace cv::CvtColor() with just CvtColor()

IIRC cv::imshow("Example2", destination); (exactly the same)
Thanks ne555,
I've change the line to the one below using imshow, but then it got error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <opencv/highgui.h>
#include <opencv/cv.h>

int main( int argc, char** argv ) {
IplImage* img = cvLoadImage("C:/Users/128A5S5/Desktop/PROJECT/1.jpg");

IplImage *destination = cvCreateImage( 
cvSize( img->width, img->height ), IPL_DEPTH_8U, 1 );
cvCvtColor( img, destination, CV_RGB2GRAY );
//cvSaveImage( "C:/Users/128A5S5/Desktop/1ed.jpg", destination );

cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );

cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
cv::imshow("Example2", destination);

cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
}


Here is the error I got...

error C2664: 'cv::imshow' : cannot convert parameter 2 from 'IplImage *' to 'cv::InputArray'
1>          Reason: cannot convert from 'IplImage *' to 'const cv::_InputArray'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous


Why I cannot use imshow in openCV? Thanks...
Topic archived. No new replies allowed.