图片的灰度图

练习一下灰度图

// HelloOpenCV.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <math.h>
#include <cv.h>
#include <highgui.h>
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) 

int main(int argc, _TCHAR* argv[])
{
	IplImage* src = 0; 
 
	const char * imagePath = "C:\\Users\\lenmovo\\Pictures\\Lena.jpg";
	// load an image  
	src=cvLoadImage(imagePath);
	IplImage* dst_gray = ::cvCreateImage(cvGetSize(src),src->depth,1);
	cvSetZero(dst_gray);
	//cvCvtColor(src,dst_gray,CV_BGR2GRAY);//得到灰度图

	// get the image data   
    int height    = src->height;  
    int width     = src->width;  
    int step      = src->widthStep;  
    int channels  = src->nChannels;  
    uchar* data      = (uchar *)src->imageData;
    printf("Processing a %dx%d image with %d channels\n",height,width,channels);  
   
    for(int i=0;i<height;i++) for(int j=0;j<width;j++) for(int k=0;k<channels;k++)  
	{
		int ddd = data[i*step+j*channels+k];
		int ttt = (uchar)src->imageData[i*step+j*channels+k];
		printf(" %d, %d \n",ddd,ttt); 
		//data[i*step+j*channels+k]=255-ddd;  
	}

	uchar* dstData = (uchar*)dst_gray->imageData;
	//使用Gray = (R*30 + G*59 + B*11 + 50) / 100来得到灰度
	for(int i=0; i<src->height; i++)
		for(int j=0; j<src->width; j++)
		{ 
			int b = (uchar)src->imageData[i*src->widthStep+j*src->nChannels];
			int g = (uchar)src->imageData[i*src->widthStep+j*src->nChannels+1];
			int r = (uchar)src->imageData[i*src->widthStep+j*src->nChannels+2];
			int mm = (r*30 + g*59+b*11+50)/100;
			dstData[i*dst_gray->widthStep+j] = mm;
			//((uchar *)(dst_gray->imageData + i*dst_gray->widthStep))[j]=mm;
		}
	cvShowImage("RGB", dst_gray); 
	cvWaitKey(0);
	::cvReleaseImage(&src);
	::cvReleaseImage(&dst_gray);
    return 0; 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值