首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一段C代码运行时_出现内存不能为read的现象_求大神分析,该如何处理

2013-12-19 
一段C代码运行时_出现内存不能为read的现象_求大神分析[解决办法]建议这样改一下试试:IplImage *getGray(c

一段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格式的数据,使用时要注意。
}

热点排行