//将一张彩色图片转成灰度图;#include#include #include #include #include using namespace std;using namespace cv;int main(){ IplImage *src=0; src=cvLoadImage("G:/1.jpg",1); IplImage *img1=cvCreateImage(cvGetSize(src),8,1); IplImage *img2=cvCreateImage(cvGetSize(src),8,1); cvCvtColor(src,img1,CV_BGR2GRAY); for(int i=0;i height;i++) { for(int j=0;j width;j++) { ((uchar*)(img2->imageData+i*img2->widthStep))[j]=((uchar *)(src->imageData+i*src->widthStep))[j*src->nChannels+0]*0.114+ ((uchar *)(src->imageData+i*src->widthStep))[j*src->nChannels+0]*0.587+ ((uchar *)(src->imageData+i*src->widthStep))[j*src->nChannels+0]*0.299; } } cvNamedWindow("src",1); cvShowImage("src",src); cvNamedWindow("img1",1); cvShowImage("img1",img1); cvNamedWindow("img2",1); cvShowImage("img2",img2); cvWaitKey(0); cvDestroyWindow("src"); cvDestroyWindow("img1"); cvDestroyWindow("img2"); return 0;}
主要采用两种方式将彩色图片转换成灰度图,一种是直接利用Opencv自带的cvCvtColor函数进行转换,另一种是直接对像素进行处理。