初次接触图像处理,正摸着石头过河,在对24位的位图进行处理时直方图函数histog遇到问题,请指点一下:
调试时遇到的问题:
Run-Time Check Failure #2 - Stack around the variable 'sc' was corrupted.处理的图像:调用SetDibParam函数获得的参数(以供参考):
BYTE *m_pDib, *m_pDib1; //指向设备无关位图的指针BITMAPINFO  *m_pBI; //位图信息的指针
BYTE     *m_pBits; //像素数据的指针
int wid:   180                  //宽度
int hei:   131                  //高度
int bits:  24                   //像素总位数
int colors: 0                   //颜色数
int bpl:   540                  //每行字节数
int bpp:   3                    //每像素字节数
long size:  70780               //图像尺寸个人感觉可能出错的地方:1、histog的方法是参考清华大学出版社陆宗琪的《C/C++图像处理编程》中的函数而来,本人初次接触图像编程对具体不甚了解,可能出错;
2、在get_put方法中,原书中lp指针是通过Address方法根据矩形窗口左上角坐标x,y来获取图像起始指针的,本人将x,y参数设为0,
lp =  m_pBits + y * bpl + x * bpp作为图像像素数据起始指针,而m_pBits是指向像素数据的指针,这样做法不知是否有问题?参数注释:long pg[256]; //存放像素点数的数组,即图像的直方图曲线 
void CImageProcess::get_put(int x, int y, int Dx, int Dy, BYTE *buf, int flag)
{
BYTE *lp;
int i; lp =  m_pBits + y * bpl + x * bpp; // 指向像素数据的指针
if(lp == NULL) return;
Dx = bpl;
for(i = 0; i < Dy; i++)
{
if(flag == 0)
memcpy(lp, buf, Dx);
else
memcpy(buf, lp, Dx);
lp += bpl;
buf += Dx;
}
}void CImageProcess::get_image(int x, int y, int Dx, int Dy, BYTE *buf) // 图像数据读入一维数组
{
get_put(x, y, Dx, Dy, buf, 1);
}void CImageProcess::histog(long *pg, int Xs, int Ys, int Dx, int Dy) // 图像窗口灰度的直方图统计(其中pg为存放像素点数的数组单元,Xs、Ys、Dx、Dy分别为处理图像的的起始位置及它的宽度和高度)
{
BYTE sc[1024];

int i, j; for(i = 0; i < 256; i++)
{
pg[i] = 0;
}
for(i = 0; i < Dy; i++)
{
get_image(Xs, Ys + i, Dx, i, sc); // 读出一行数据
for(j = 0; j < Dx; j++)
{
pg[ sc[j] ]++; // 按像素值在相应单元计数
}
}
}
void CImageProcess::SetDibParam( BYTE *pDib ) // 设置设备无关位图参数
{
BITMAPINFOHEADER *pBIH; m_pBI = (BITMAPINFO *) pDib; // 得位图信息指针
pBIH = (BITMAPINFOHEADER *) pDib; // 得位图信息头指针

wid = pBIH->biWidth; // 从位图信息头中取出图像参数
hei = pBIH->biHeight;
bits = pBIH->biBitCount;
colors = 1 << pBIH->biBitCount; // 像素位数与调色板数的关系

if( pBIH->biBitCount > 8 ) colors = 0; // 真彩色图像无调色板
else if( pBIH->biClrUsed != 0) colors = pBIH->biClrUsed; // 如果使用的色彩数不等于0 m_pBits = &pDib[sizeof(BITMAPINFOHEADER) + colors * sizeof(RGBQUAD)]; // 得像素数据指针 bpl = ( wid * bits + 31 ) / 32 * 4; // 计算每行字节数
bpp = ( bits + 7 ) / 8; // 计算每像素字节数
size = 40L + colors * 4 + bpl * hei; // 计算图像尺寸
}

解决方案 »

  1.   

    你这个是256色还是24位色的?为什么i < 256?
      

  2.   

    24是指像素总位数,
    i<256是指i从0到255代表的颜色,初次接触图像编程,不知哪里有问题吗??
      

  3.   

    long pg[256]; 这个是用来算256色灰度的bmp的..24Bit有16兆色..用那个pg[256]能装得下所有的颜色么?能统计得了么?要全装就要DWORD pg[16*1024*1024]..
    ms是浪费空间.
    用个list,扫描原图,每读一个pixel,就扫描list,如果找不到,就add_to_list;如果找到,在该node的pixel记数器+1..最后就会得到一个原图所使用的颜色统计list..