一段C代码运行时_出现内存不能为read的现象_求大神分析
IplImage *getGray(const IplImage *img)
//img的宽高参数为800*600
{
IplImage *gray=(IplImage *)malloc(sizeof(IplImage));
IplImage *gray32=img;
int i=0,j=0;
int c=0;
int width=gray32->width;
int heigh=gray32->height;
int step=gray32->widthStep;
int nchan=gray32->nChannels;
float *data=(float*)malloc(sizeof(float)*width*height*step);
gray->imageData = (char*)data;
char* data1 = img->imageData;
for (i=0;i<heigh;i++)
{
for (j=0;j<width;j++)
{
if(nchan==3)
data[i*step+j]=((float)data1[i*step+j*nchan]+data1 [i*step+j*nchan+1]+data1[i*step+j*nchan+2])/765.0;
else
{
data[i*step+j]=(float)data1[i*step+j]/255.0f;
printf("i=%d,j=%d,c=%d\n",i,j,c++);
}
}
}
return gray; //返回后的imageData中是float格式的数据,使用时要注意。
}