最大值法灰度化

来源:互联网 发布:软件开发课程设计 编辑:程序博客网 时间:2024/05/16 01:12
//最大值法灰度化
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
IplImage * RGBtoGRAYTmax(IplImage *in,IplImage *out)
{   
for (int y=0;y<in->height;y++)
{
uchar *ptr=(uchar*)(in->imageData+y*in->widthStep);
for (int x=0;x<in->width;x++)
{
if(ptr[3*x]>ptr[3*x+1])
out->imageData[y * in->width+ x]=ptr[3*x];
else out->imageData[y * in->width + x]=ptr[3*x+1];
if(ptr[3*x+2]>in->imageData[x])
out->imageData[y * in->width + x]=ptr[3*x+2];
}
}
return(out);
}
int main()
{
IplImage * p=cvLoadImage("C:\\Users\\Administrator\\Desktop\\I.JPG",1); //图像地址,采用双斜杠
IplImage *c=cvCreateImage(cvGetSize(p),IPL_DEPTH_8U,1);
    RGBtoGRAYTmax(p,c);//调用最大值法灰度化函数
double t = (double)cvGetTickCount();//算运算时间
t = (double)cvGetTickCount() - t;
printf( "exec time = %gms\n", t/(cvGetTickFrequency()*1000.));//算运算时间(ms)
cvNamedWindow("max",0);
cvShowImage("max",c);
cvSaveImage("C:\\Users\\Administrator\\Desktop\\max.jpg",c);  //将图像保存下来
cvWaitKey(0);
cvDestroyWindow("OUT");
cvReleaseImage(&p);
cvReleaseImage(&c);


}
0 0