vc6.0编的图像处理软件,检测空白图像时运行良好,图像中有物体时就崩溃
以下黑色代码运行时可以检测空白的也可以检测图像中有物体的,加入红字部分后只能无物体时运行,如果图像中有物体了就会停止工作,出现appcrash
// 申请biHeight个长度为biWidthBytes的数组,用他们来保存位图数据
int i,j;
if (m_lpDataB==NULL)
{
m_lpDataB=new BYTE[m_pBMIH->biHeight*(m_pBMIH->biWidth)];
}
for (i=0;i<m_pBMIH->biHeight;i++)
{
for (j=0;j<m_pBMIH->biWidth;j++)
{
memcpy(m_lpDataB+(i*m_pBMIH->biWidth+j),(m_pImageBuffer+(i*m_pBMIH->biWidth*3+j*3)),sizeof(int));
}
}
mean=Mean();//计算b分量均值
BYTE gray;
int cum;
int nDiffGray;
int nThres = DetectThreshold(100, nDiffGray);//取得分割阈值,最大迭代次数为100
// 计算图像中最左边的第一行的B分量均值
int mean1=0,sum1=0 ,num1=0;
for (i=0;i<m_pBMIH->biHeight;i++)
{
for(j=0;j<250;j++)
{
if( GetGray(j, i)< nThres)
{
sum1+=m_lpDataB[i*m_pBMIH->biWidth+j];
num1++;
}
}
}
mean1=sum1/num1;
int mean2=0,sum2=0 ,num2=0;
for (i=0;i<m_pBMIH->biHeight-1;i++)
{
for(j=300;j<550;j++)
{
if( GetGray(j, i)< nThres)
{
sum2+=m_lpDataB[i*m_pBMIH->biWidth+j];
num2++;
}
}
}
mean2=sum2/num2;
// B分量均值
// 计算面积
double area1=0; //area1代表左边第一行区域面积
for(i = 0; i < (m_pBMIH->biHeight); i++)
{
for(j = 0; j <250; j++)
{
gray=GetGray(j, i);
if(gray<nThres )
gray = 0;
else
gray = 255;
if (gray==0)
{
area1++;
}
}
}
double area2=0; //area2代表左边第2行区域面积
for(i = 0; i <m_pBMIH->biHeight-1; i++)
{
for(j = 300; j <550 ; j++)
{
gray=GetGray(j, i);
if(gray<nThres )
gray = 0;
else
gray = 255;
if (gray==0)
{
area2++;
}
//m_lpDataB[i*m_pBMIH->biWidth+j]=gray;
}
}
///////////////////////////////////////判断是否合格
if(area1<m_Area&&mean1>m_Bmean)
{ m_Yes=m_Yes+1;
CByteArray Array;
Array.RemoveAll();
Array.SetSize(1);
Array.SetAt(0,0);//00000
myComm.SetOutput(COleVariant(Array));
m_Ld=mean1;
m_Rd=area1;
}
else //1不合格
{ if(area1>1000)
{m_No=m_No+1;
CByteArray Array;
Array.RemoveAll();
Array.SetSize(1);
Array.SetAt(0,1);//10000
myComm.SetOutput(COleVariant(Array));
m_Ld=mean1;
m_Rd=area1;
}
}
/////////////////////////////////
m_Sum=m_No+m_Yes;
UpdateData(FALSE);
//UpdateData();
return 1;
}
BYTE CTestDlg::GetGray(int x, int y)
{
COLORREF ref = GetPixel(x, y);
BYTE r, g, b, byte;
r = GetRValue(ref);
g = GetGValue(ref);
b = GetBValue(ref);
if(r == g && r == b)
return r;
double dGray = (0.30*r + 0.59*g + 0.11*b);
// 灰度化
byte = (int)dGray;
return byte;
}
int CTestDlg::DetectThreshold(int nMaxIter, int &nDiffRet)
{
int nThreshold;
nDiffRet = 0;
// 直方图数组
int nHistogram[256] = { 0 };
int i, j;
BYTE bt;
int nMin = 255;
int nMax = 0;
// 扫描图像,计算出最大、最小灰度和直方图
for(j = 0; j <m_pBMIH->biHeight; j ++)
{
for(i=0; i<m_pBMIH->biWidth; i++)
{
bt = GetGray(i, j);
if(bt < nMin)
nMin = bt;
if(bt > nMax)
nMax = bt;
nHistogram[bt] ++;
}
}
int nTotalGray = 0; //灰度值的和
int nTotalPixel = 0; //像素数的和
int nNewThreshold = (nMax + nMin)/2; //初始阈值
nDiffRet = nMax - nMin;
if (nMax == nMin)
nThreshold = nNewThreshold;
else
{
nThreshold = 0;
// 迭代开始,直到迭代次数达到100或新阈值与上一轮得到的阈值相等,迭代结束
for(int nIterationTimes = 0; nThreshold != nNewThreshold && nIterationTimes < nMaxIter; nIterationTimes ++)
{
nThreshold = nNewThreshold;
nTotalGray = 0;
nTotalPixel = 0;
//计算图像中小于当前阈值部分的平均灰度
for(i=nMin; i<nThreshold; i++)
{
nTotalGray += nHistogram[i]*i;
nTotalPixel += nHistogram[i];
}
int nMean1GrayValue = nTotalGray/nTotalPixel;
nTotalGray = 0;
nTotalPixel = 0;
//计算图像中大于当前阈值部分的平均灰度
for(i=nThreshold + 1; i<=nMax; i++)
{
nTotalGray += nHistogram[i]*i;
nTotalPixel += nHistogram[i];
}
int nMean2GrayValue = nTotalGray/nTotalPixel;
nNewThreshold = (nMean1GrayValue + nMean2GrayValue)/2; //计算出新的阈值
nDiffRet = abs(nMean1GrayValue - nMean2GrayValue);
}
}
return nThreshold;
}
更多 0