vc6.0编的图像处理软件,检测空白图像时运行良好,图像中有物体时就崩溃

00猫殿下00 2015-12-22 09:18:13
以下黑色代码运行时可以检测空白的也可以检测图像中有物体的,加入红字部分后只能无物体时运行,如果图像中有物体了就会停止工作,出现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
...全文
89 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
向立天 2016-01-22
  • 打赏
  • 举报
回复
您好 我是本版版主 此帖已多日无人关注 请您及时结帖 如您认为问题没有解决可按无满意结帖处理 另外本版设置了疑难问题汇总帖 并已在版面置顶 相关规定其帖子中有说明 您可以根据规定提交您帖子的链接 如您目前不想结帖只需回帖说明 我们会删除此结帖通知 见此回复多日内无回应 我们将强制结帖 相关规定详见界面界面版关于版主结帖工作的具体办法
ArthurKingYs 2015-12-22
  • 打赏
  • 举报
回复
openCV 直接调用函数
Eleven 2015-12-22
  • 打赏
  • 举报
回复
你还是Debug单步调试一下,看看执行到哪里出错了?

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧