I know that you want to use "imread" and "CV_LOAD_IMAGE_GRAYSCALE" and convert automatically. But this is another way to upload one image and convert to grayscale:
define CV_NO_BACKWARD_COMPATIBILITY #include <cv.h> #include <highgui.h> #include <math.h> int main(){ /* load the image */ IplImage* img = cvLoadImage("yourPicture.bmp"); //jpg - bmp /* retrieve properties */ int width = img->width; int height = img->height; int nchannels = img->nChannels; int step = img->widthStep; IplImage* img2 = cvCreateImage(cvSize(img->height, img->width),IPL_DEPTH_8U,1); /* setup the pointer to access image data */ uchar *data = ( uchar* )img->imageData; uchar *data2= ( uchar* )img2->imageData; /* convert to grayscale manually */ int i, j, r, g, b, byte; for( i = 0 ; i < height ; i++ ) { for( j = 0 ; j < width ; j++ ) { r = data[i*step + j*nchannels + 0]; g = data[i*step + j*nchannels + 1]; b = data[i*step + j*nchannels + 2]; byte = ( r + g + b ) / 3; int v0=0, v1=0, v2=0; data2[i*(img2->widthStep)+j*(img2->nChannels)+0] = byte; data2[i*(img2->widthStep)+j*(img2->nChannels)+1] = byte; data2[i*(img2->widthStep)+j*(img2->nChannels)+2] = byte; } } cvNamedWindow("ImagenColor", 1); cvShowImage("ImagenColor", img); cvNamedWindow("Gray", 1); cvShowImage("Gray", img2); }
Orlando Herrera 05 Oct 2018-11-11T00: 00Z
source share