png to grayscale conversion offset by 1 in pixel intensity
hi
I am running opencv in ubuntu 14.04
Here I am reading a png file and displaying it , printing value of b,g,r channel at pixel and converting to gray scale (file not changed on disk only in memory).
My code in C
/* usage: prog image assuming filename is image-1.png*/
#include"cv.h"
#include"highgui.h"
#include"imgproc.hpp"
using namespace cv;
int main( int argc, char** argv )
{ char strimgfile[100];
int i,j,cn,filenumber=1;
Mat image;
Mat gray_image;
Vec3b intensity;
unsigned int b,g,r;
sprintf(strimgfile,"%s%d.png",argv[1],filenumber);//adding number and png extension to program argument which is filename prefix
printf("\nthe file that will be read _%s_\n",strimgfile);
image = imread( strimgfile, 1 );
if( argc != 2 || !image.data )
{
printf( "\n No image data \n " );
return -1;
}
cn=image.channels();
intensity = image.at<Vec3b>(100, 100);
printf("\npixel:%d_%d_%d\n",intensity.val[0],intensity.val[1],intensity.val[2]);
printf("\nThis image has %d rows %d columns and channels=%d\n",image.rows,image.cols,cn);
cvtColor(image, gray_image, CV_BGR2GRAY );
printf("\nChannels in gray image=%d\n",gray_image.channels());
printf("\nValue at that pixel in gray image is %u\n",(int)gray_image.at<uchar>(100,100));
namedWindow( strimgfile, CV_WINDOW_AUTOSIZE );
imshow( strimgfile, image );
waitKey(0);
return 0;
}
I compile it using
$ g++ code.c -o bincode -I /usr/include/opencv -I /usr/include/opencv2/imgproc/ -lm `pkg-config --libs --cflags opencv` -ldl
My output is
the file that will be read _image-1.png_
pixel:45_41_31
This image has 240 rows 352 columns and channels=3
Channels in gray image=1
Value at that pixel in gray image is 38
QUE) 45+41+31=117 so its one third is 39 . why do I get 38 ?
QUE)Also please suggest me compilation options or code changes for better performance .I am sure , i am using too many resources for this simple task .
thanks.