MoveTrace.cpp
资源名称:VideoMove.rar [点击查看]
上传用户:czshopping
上传日期:2022-05-22
资源大小:5430k
文件大小:22k
源码类别:
视频捕捉/采集
开发平台:
Visual C++
- // MoveTrace.cpp: implementation of the CMoveTrace class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "VideoMove.h"
- #include "MoveTrace.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CMoveTrace::CMoveTrace()
- {
- m_prect=NULL;
- m_pImageData=m_pGrey=m_pGrey1=m_pGrey2=m_pMosaic=NULL;
- }
- CMoveTrace::~CMoveTrace()
- {
- }
- bool CMoveTrace::Initialize(const int nImageWidth, const int nImageHeight,
- const int nWidthCount, const int nHeightCount,
- const int nBitsCount)
- {
- m_nExtractWidth = m_nImageWidth = nImageWidth;
- m_nExtractHeight = m_nImageHeight = nImageHeight;
- m_nX=m_nY=1;
- m_Count = 0;
- m_rect.SetRectEmpty();
- m_nWidthCount = nWidthCount; //设置将屏幕分割的横向份数
- m_nHeightCount = nHeightCount; //设置将屏幕分割的纵向份数
- m_nDivisionWidth = m_nImageWidth/m_nWidthCount; //分割后的每一个小区域的宽度(象素)
- m_nDivisionHeight = m_nImageHeight/m_nHeightCount; //分割后的每一个小区域的高度(象素)
- m_prect = new CRect*[m_nHeightCount];
- for (int i=0;i<m_nHeightCount;++i)
- m_prect[i]=new CRect[nWidthCount];
- for(int y=0; y<m_nHeightCount; y++)
- {
- for(int x=0; x<m_nWidthCount; x++)
- {
- m_prect[y][x] = CRect(x*m_nDivisionWidth, y*m_nDivisionHeight,
- x*m_nDivisionWidth+m_nDivisionWidth,
- y*m_nDivisionHeight+m_nDivisionHeight);
- }
- }
- if (!m_pGrey)
- delete m_pGrey;
- m_pGrey = new BYTE[m_nImageHeight*m_nImageWidth];
- memset(m_pGrey,0,m_nImageHeight*m_nImageWidth);
- ////////////////////////////////////////////////
- if (!m_pGrey1)
- delete m_pGrey1;
- m_pGrey1 = new BYTE[m_nImageHeight*m_nImageWidth];
- memset(m_pGrey1,0,m_nImageHeight*m_nImageWidth);
- if (!m_pGrey2)
- delete m_pGrey2;
- m_pGrey2 = new BYTE[m_nImageHeight*m_nImageWidth];
- memset(m_pGrey2,0,m_nImageHeight*m_nImageWidth);
- if (!m_pMosaic)
- delete m_pMosaic;
- m_pMosaic = new BYTE[m_nImageHeight*m_nImageWidth];
- memset(m_pMosaic,0,m_nImageHeight*m_nImageWidth);
- return m_pGrey && m_pGrey1 && m_pMosaic && m_pGrey2 && m_pImageData;
- }
- void CMoveTrace::Uninitialize()
- {
- if (m_pGrey)
- delete m_pGrey;
- if (m_pGrey1)
- delete m_pGrey1;
- if (m_pGrey2)
- delete m_pGrey2;
- if(m_pMosaic)
- delete m_pMosaic;
- // m_prect = new CRect*[m_nHeightCount];
- for (int i=0;i<m_nHeightCount;++i)
- delete[] m_prect[i];
- delete[] m_prect;
- m_pImageData=m_pGrey=m_pGrey1=m_pGrey2=m_pMosaic=NULL;
- m_prect=NULL;
- }
- bool CMoveTrace::GetDIBBit(unsigned char * pImageData)
- {
- if(!pImageData)
- return false;
- m_pImageData = pImageData;
- return true;
- }
- bool CMoveTrace::Grey()
- {
- int nIndex=0,x1,y1;
- for(int y=0;y<m_nImageHeight;y+=m_nY)
- {
- y1=m_nImageHeight-y;
- for(int x=0;x<m_nImageWidth;x+=m_nX)
- {
- x1=m_nImageWidth-x;
- int nPos = (y1*m_nImageWidth+x1)*3;
- // int nTemp1 = m_pImageData[nPos];
- // int nTemp2 = m_pImageData[nPos+1];
- // int nTemp3 = m_pImageData[nPos+2];
- // m_pGrey1[x/m_nX+y*m_nExtractWidth/m_nY] =
- m_pGrey1[nIndex++] =
- static_cast<unsigned char>(m_pImageData[nPos+2]*0.3+m_pImageData[nPos+1]*0.59+m_pImageData[nPos]*0.11);
- }
- }
- return true;
- }
- void CMoveTrace::Dither(int nThreshold)
- {
- double dbTotalValue=0;
- int nTotal=m_nExtractHeight*m_nExtractWidth;
- int nIndex=0;
- for (int i=0;i<nTotal;++i)
- {
- (*(m_pMosaic+nIndex))=
- (*(m_pMosaic+nIndex))>nThreshold ? 255:0;
- ++nIndex;
- }
- }
- int CMoveTrace::Sobel()
- {
- int ImageValue,ImageValueX,ImageValueY;
- unsigned char *pImageDataTemp;
- pImageDataTemp=new unsigned char[m_nExtractWidth*m_nExtractHeight];
- if (pImageDataTemp==NULL)
- {
- return 0;//内存分配错误
- }
- memcpy(pImageDataTemp, m_pGrey1, m_nExtractWidth*m_nExtractHeight);
- int nPos=m_nExtractWidth;
- int i_end=m_nExtractHeight-1;
- int j_end=m_nExtractWidth-1;
- for (int i=1;i<i_end;++i)
- {
- for (int j=1;j<j_end;++j)
- {
- ImageValueX=-(*(pImageDataTemp+nPos-m_nExtractWidth+j-1))
- -2*(*(pImageDataTemp+nPos-m_nExtractWidth+j))
- -(*(pImageDataTemp+nPos-m_nExtractWidth+j+1))
- +(*(pImageDataTemp+nPos+m_nExtractWidth+j-1))
- +2*(*(pImageDataTemp+nPos+m_nExtractWidth+j))
- +(*(pImageDataTemp+nPos+m_nExtractWidth+j+1));
- ImageValueY=-(*(pImageDataTemp+nPos-m_nExtractWidth+j-1))
- +(*(pImageDataTemp+nPos-m_nExtractWidth+j))
- -2*(*(pImageDataTemp+nPos+j-1))
- +2*(*(pImageDataTemp+nPos+j+1))
- -(*(pImageDataTemp+nPos+m_nExtractWidth+j-1))
- +(*(pImageDataTemp+nPos+m_nExtractWidth+j+1));
- ImageValue=abs(max(ImageValueX,ImageValueY));
- if (ImageValue>255)
- {
- *(m_pGrey1+nPos+j)=255;
- }
- else
- {
- *(m_pGrey1+nPos+j)=static_cast<unsigned char>(ImageValue);
- }
- }
- nPos+=m_nExtractWidth;
- }
- delete [] pImageDataTemp;
- return 1;
- }
- BOOL CMoveTrace::FrameMinus()
- {
- for(int x=0; x<m_nExtractWidth*m_nExtractHeight; ++x)
- {
- m_pGrey[x]=abs(m_pGrey1[x] - m_pGrey2[x]);
- }
- return TRUE;
- }
- void CMoveTrace::Mosaic(int nMosaicWidth,const int nMosaicHeight)
- {
- m_nMosaicWidth = nMosaicWidth;
- m_nMosaicHeight = nMosaicHeight;
- int nW,nH;
- nW=m_nExtractWidth/nMosaicWidth;
- nH=m_nExtractHeight/nMosaicHeight;
- // nW=m_nImageWidth%nMosaicWidth==0 ? m_nImageWidth/nMosaicWidth : m_nImageWidth/nMosaicWidth;
- // nH=m_nImageHeight%nMosaicHeight==0?m_nImageHeight/nMosaicHeight : m_nImageHeight/nMosaicHeight;
- double dbTemp;
- int nTemp;
- int nArea=nMosaicWidth*nMosaicHeight;
- // unsigned char nImageValue;
- int i,j,ii,jj;
- int n_i_pos=0;
- int n_j_pos=0;
- int n_ii_pos=0;
- int n_jj_pos=0;
- for (i=0;i<nH;++i)
- {
- n_j_pos=0;
- for (j=0;j<nW;++j)
- {
- dbTemp=0;
- for (ii=0;ii<nMosaicHeight;++ii)
- {
- n_ii_pos=(n_i_pos+ii)*m_nExtractWidth+n_j_pos;
- for (jj=0;jj<nMosaicWidth;++jj)
- {
- dbTemp+=m_pGrey[n_ii_pos+jj];
- }
- }
- nTemp=static_cast<BYTE>(dbTemp/nArea);
- for (ii=0;ii<nMosaicHeight;++ii)
- {
- n_ii_pos=(n_i_pos+ii)*m_nExtractWidth+n_j_pos;
- for (jj=0;jj<nMosaicWidth;++jj)
- {
- m_pMosaic[n_ii_pos+jj]=nTemp;
- }
- }
- n_j_pos+=nMosaicWidth;
- }
- n_i_pos+=nMosaicHeight;
- }
- }
- void CMoveTrace::SaveFrame()
- {
- int nTotal=m_nExtractWidth*m_nExtractHeight;
- for(int x=0; x<nTotal; ++x)
- {
- m_pGrey2[x] = m_pGrey1[x];
- }
- }
- /*
- int CMoveTrace::Mean()
- {
- double dbTotalValue=0;
- for (int i=0;i<m_nImageHeight;++i)
- {
- for (int j=0;j<m_nImageWidth;++j)
- {
- dbTotalValue+=*(m_pImageData+i*m_nImageWidth+j);
- }
- }
- return static_cast<int>(dbTotalValue/(m_nImageHeight*m_nImageWidth));
- }
- */
- bool CMoveTrace::Rectangle(CRect &rect,const int nMaxWidth, const int nMinChange)
- {
- CRect tempRect;
- POINT centerPoint, tmpPoint;
- centerPoint.x = m_nExtractWidth/2;
- centerPoint.y = m_nExtractHeight/2;
- int i,j;
- bool bTopFind=false;
- bool bBottomFind=false;
- int nIndex=0;
- for (i=0;i<m_nExtractHeight;++i)
- {
- for (j=0;j<m_nExtractWidth;++j)
- {
- if (*(m_pMosaic+nIndex)!=0 && !bTopFind)
- tempRect.top=m_nExtractHeight-1-i, tempRect.left=j, bTopFind=true;
- else if (*(m_pMosaic+nIndex)!=0 && bTopFind && tempRect.left>j)
- tempRect.left=j;
- ++nIndex;
- }
- }
- nIndex=m_nExtractWidth*m_nExtractHeight-1;
- for (i=m_nExtractHeight-1;i>-1 && bTopFind ;--i)
- {
- for (j=m_nExtractWidth-1;j>-1;--j)
- {
- if (*(m_pMosaic+nIndex)!=0 && !bBottomFind)
- tempRect.bottom=m_nExtractHeight-1-i, tempRect.right=j, bBottomFind=true;
- else if (*(m_pMosaic+nIndex)!=0 && bBottomFind && tempRect.right<j)
- tempRect.right=j;
- --nIndex;
- }
- }
- //交换top和bottom的值
- int temp = tempRect.top;
- tempRect.top = tempRect.bottom;
- tempRect.bottom = temp;
- //如果发现的活动区域过大(大于分割的矩形区域)则忽略
- if(tempRect.Width()>=m_nDivisionWidth ||
- tempRect.Height()>=m_nDivisionHeight)
- return false;
- if(bTopFind && bBottomFind && m_Count==0)//发现有新的运动物体
- {
- m_rect = tempRect;
- rect.left = m_rect.left*m_nX;
- rect.right = m_rect.right*m_nX;
- rect.top = m_rect.top*m_nY;
- rect.bottom = m_rect.bottom*m_nY;
- m_Count = 1; //设置有运动物体的标志
- Forecast(rect); //求得运动物体在屏幕中的位置
- return true;
- }
- else if (!(bTopFind && bBottomFind) && m_Count == 1)//如果没有找到运动物体但是有运动物体
- {
- tmpPoint = m_rect.CenterPoint();
- BOOL bFlag = abs(tmpPoint.x-centerPoint.x)<nMaxWidth;
- //如果运动物体的中心到监视区域中心的距离时候小于预设值,
- //则认为物体没有消失,否则物体消失
- if(bFlag)
- {
- //将上次的框保留下来
- rect.left = m_rect.left*m_nX;
- rect.right = m_rect.right*m_nX;
- rect.top = m_rect.top*m_nY;
- rect.bottom = m_rect.bottom*m_nY;
- Forecast(rect);
- return true;
- }
- else
- {
- //认为没有运动物体,将变量复位
- m_Count = 0;
- m_rect.SetRectEmpty();
- return false;
- }
- }
- else if(m_Count == 1 && bTopFind && bBottomFind)//有物体并且检测到有运动(有白点产生)
- {
- int nChangePix = m_nMosaicWidth*nMinChange;
- int nH=tempRect.Height();
- int nW=tempRect.Width();
- //如果检测到物体的运动范围小于最小运动范围(nMinChange*m_nMosaicWidth)
- //将范围适当变大
- // if (min(abs(nH),abs(nW) )<nMinChange*m_nMosaicWidth )
- if (nH<nMinChange*m_nMosaicHeight || nW<nMinChange*m_nMosaicWidth )
- {
- tempRect.left-=m_nMosaicWidth*2;
- tempRect.left = (tempRect.left<0) ? 0: tempRect.left;
- tempRect.right+=m_nMosaicWidth*2;
- tempRect.right = (tempRect.right>m_nImageWidth)? m_nImageWidth : tempRect.right;
- tempRect.bottom+=m_nMosaicHeight*2;
- tempRect.bottom = (tempRect.bottom>m_nImageHeight) ? m_nImageHeight : tempRect.bottom;
- tempRect.top-=m_nMosaicHeight*2;
- tempRect.top = (tempRect.top<0) ? 0 : tempRect.top;
- }
- int ntempTop = abs(tempRect.top - m_rect.top); //得到这次的纵向的运动范围和上次的纵向运动范围的
- int ntempBottom = abs(tempRect.bottom - m_rect.bottom); //和上次的纵向运动范围的绝对值
- //如果变化过大,则调整变化速度
- if( ntempTop>nChangePix || ntempBottom>nChangePix)
- {
- m_rect.left = tempRect.left;
- m_rect.right = tempRect.right;
- if(abs(tempRect.bottom-m_rect.bottom) > nChangePix)
- tempRect.bottom-m_rect.bottom > 0 ? m_rect.bottom+=nChangePix : m_rect.bottom-=nChangePix;
- if(abs(tempRect.top-m_rect.top) > nChangePix)
- tempRect.top-m_rect.top > 0 ? m_rect.top+=nChangePix : m_rect.top-=nChangePix;
- }
- m_rect=tempRect;
- rect.left = m_rect.left*m_nX;
- rect.right = m_rect.right*m_nX;
- rect.top = m_rect.top*m_nY;
- rect.bottom = m_rect.bottom*m_nY;
- Forecast(rect);
- return true;
- }
- return false;
- }
- bool CMoveTrace::Rectangle2(CRect &rect, const int nMaxWidth, const int nMinChange)
- {
- CRect tempRect;
- POINT centerPoint, tmpPoint;
- centerPoint.x = m_nExtractWidth/2;
- centerPoint.y = m_nExtractHeight/2;
- int nMosaicArea = m_nMosaicWidth*m_nMosaicHeight;
- long nWhiteCount = 1;
- long nTempX = 0;
- long nTempY = 0;
- for(int y=1; y<m_nExtractHeight-1; ++y)
- {
- for(int x=1; x<m_nExtractWidth-1; ++x)
- {
- if((255==m_pMosaic[x+y*m_nExtractWidth])
- && (255==m_pMosaic[x+m_nExtractWidth+y*m_nExtractWidth])
- //&& (255==lpVideo->m_pGrey1[x+1+y*lpVideo->m_iWidth])
- )
- {
- ++nWhiteCount;
- nTempX += x;
- nTempY += y;
- }
- }
- }
- nTempX = nTempX/nWhiteCount;
- nTempY = m_nExtractHeight-1-nTempY/nWhiteCount;
- // nTempY = nTempY/nWhiteCount;
- long nharfTempW = nWhiteCount/nMosaicArea/m_nX; //矩形框的半宽度
- long nharfTempH = nharfTempW*3; //矩形框的半高度
- // tempRect.left = abs(nTempX-nharfTempW);
- // tempRect.top = abs(nTempY-nharfTempH);
- (nTempX-nharfTempW)<0 ? tempRect.left = 0 : tempRect.left = nTempX-nharfTempW;
- (nTempY-nharfTempH)<0 ? tempRect.top = 0 : tempRect.top = nTempY-nharfTempH;
- (nTempX+nharfTempW)>m_nExtractWidth ? tempRect.right = m_nExtractWidth : tempRect.right = nTempX+nharfTempW;
- (nTempY+nharfTempH)>m_nExtractHeight ? tempRect.bottom = m_nExtractHeight : tempRect.bottom = nTempY+nharfTempH;
- if(tempRect.left==0 && tempRect.right==m_nExtractWidth && tempRect.top==0 && tempRect.bottom==m_nExtractHeight)
- return false;
- if(nWhiteCount>=nMosaicArea && m_rect.IsRectNull())//发现有新的运动物体
- {
- m_rect = tempRect;
- rect.left = m_rect.left*m_nX;
- rect.right = m_rect.right*m_nX;
- rect.top = m_rect.top*m_nY;
- rect.bottom = m_rect.bottom*m_nY;
- m_Count = 1;
- return true;
- }
- else if (m_Count == 1 && nWhiteCount<nMosaicArea)
- {
- tmpPoint = m_rect.CenterPoint();
- BOOL bFlag;
- bFlag = abs(tmpPoint.x-centerPoint.x) < nMaxWidth;
- if(bFlag)
- {
- rect.left = m_rect.left*m_nX;
- rect.right = m_rect.right*m_nX;
- rect.top = m_rect.top*m_nY;
- rect.bottom = m_rect.bottom*m_nY;
- return true;
- }
- else
- {
- m_Count = 0;
- m_rect.SetRectEmpty();
- rect = m_rect;
- return false;
- }
- }
- else if(m_Count == 1 && nWhiteCount>=nMosaicArea)
- {
- // int nH=tempRect.Height();
- int nW=tempRect.Width();
- int nChangePix = m_nMosaicWidth*nMinChange;
- if (nW<nMinChange*m_nMosaicWidth)// (min(nH,nW)<nMinChange*m_nMosaicWidth )
- {
- tempRect.left-=m_nMosaicWidth*2;
- if (tempRect.left < 0)
- tempRect.left=0;
- tempRect.right+=m_nMosaicWidth*2;
- if (tempRect.right > m_nExtractWidth)
- tempRect.right = m_nExtractWidth;
- tempRect.top-=m_nMosaicHeight*2;
- if (tempRect.top < 0)
- tempRect.top=0;
- tempRect.bottom+=m_nMosaicHeight*2;
- if (tempRect.bottom>m_nExtractHeight)
- tempRect.bottom = m_nExtractHeight;
- }
- int ntempTop = abs(tempRect.top - m_rect.top);
- int ntempBottom = abs(tempRect.bottom - m_rect.bottom);
- if( ntempTop>nChangePix || ntempBottom>nChangePix)
- {
- // if(abs(tempRect.left-m_rect.left) > nChangePix)
- // tempRect.left-m_rect.left > 0 ? m_rect.left+=nChangePix : m_rect.left-=nChangePix;
- //
- // if(abs(tempRect.right-m_rect.right) > nChangePix)
- // tempRect.right-m_rect.right > 0 ? m_rect.right+=nChangePix : m_rect.right-=nChangePix;
- m_rect.left = tempRect.left;
- m_rect.right = tempRect.right;
- if(abs(tempRect.bottom-m_rect.bottom) > nChangePix)
- tempRect.bottom-m_rect.bottom > 0 ? m_rect.bottom+=nChangePix : m_rect.bottom-=nChangePix;
- if(abs(tempRect.top-m_rect.top) > nChangePix)
- tempRect.top-m_rect.top > 0 ? m_rect.top+=nChangePix : m_rect.top-=nChangePix;
- rect.left = m_rect.left*m_nX;
- rect.right = m_rect.right*m_nX;
- rect.top = m_rect.top*m_nY;
- rect.bottom = m_rect.bottom*m_nY;
- return true;
- }
- m_rect=tempRect;
- rect.left = m_rect.left*m_nX;
- rect.right = m_rect.right*m_nX;
- rect.top = m_rect.top*m_nY;
- rect.bottom = m_rect.bottom*m_nY;
- return true;
- }
- return false;
- }
- bool CMoveTrace::Extract(int nX, int nY)
- {
- if (nX && nY)
- {
- m_nExtractWidth=m_nImageWidth/nX;
- m_nExtractHeight=m_nImageHeight/nY;
- m_nX=nX,m_nY=nY;
- return true;
- }
- else
- return false;
- }
- void CMoveTrace::Forecast(CRect rect)
- {
- if(NULL==m_prect) AfxMessageBox("没有初始化!");
- int x,y;
- for(y=0;y<m_nHeightCount;y++)
- {
- for(x=0;x<m_nWidthCount;x++)
- {
- if( m_prect[y][x].PtInRect(CPoint(rect.left, rect.top))
- && m_prect[y][x].PtInRect(CPoint(rect.right, rect.top))
- && m_prect[y][x].PtInRect(rect.CenterPoint()) )
- {
- m_NextRect = m_CurrentRect;
- m_CurrentRect = m_prect[y][x];
- break;
- }
- else
- {
- //--------------
- }
- }
- }
- /* if(NULL==m_prect) AfxMessageBox("没有初始化!");
- int x,y;
- for(y=0;y<m_nHeightCount;y++)
- {
- for(x=0;x<m_nWidthCount;x++)
- {
- if(m_prect[y][x].PtInRect(rect.CenterPoint()))
- {
- m_NextRect = m_CurrentRect = m_prect[y][x];
- //判断下一个是否在左上的单元
- if(nIndex/m_nWidthCount>0 && nIndex%m_nWidthCount>0)
- {
- if(rect.left <= m_prect[nIndex-m_nWidthCount-1].right
- && rect.top <= m_prect[nIndex-m_nWidthCount-1].bottom)
- {
- m_NextRect = m_prect[nIndex-m_nWidthCount-1];
- break;
- }
- }
- //判断下一个是否在左边的单元
- //if(nIndex-1 >= 0)
- if(nIndex%m_nWidthCount>0)
- {
- if(rect.left <= m_prect[nIndex-1].right
- && rect.bottom < m_prect[nIndex-1].bottom
- && rect.top > m_prect[nIndex-1].top)
- {
- m_NextRect = m_prect[nIndex-1];
- break;
- }
- }
- //判断下一个是否在左下边的单元
- //if(nIndex+m_nWidthCount-1 < m_nWidthCount*m_nHeightCount)
- if( (nIndex/m_nWidthCount<m_nHeightCount-1) &&
- (nIndex%m_nWidthCount>0) )
- {
- if(rect.left <= m_prect[nIndex+m_nWidthCount-1].right
- && rect.bottom >= m_prect[nIndex+m_nWidthCount-1].top)
- {
- m_NextRect = m_prect[nIndex+m_nWidthCount-1];
- break;
- }
- }
- //判断下一个是否在右上边的单元
- if( nIndex/m_nWidthCount>0
- && (nIndex%m_nWidthCount < m_nWidthCount-1) ) //该单元必须有上一排单元
- {
- if(rect.right >= m_prect[nIndex-m_nWidthCount+1].left
- && rect.top <= m_prect[nIndex-m_nWidthCount+1].bottom)
- {
- m_NextRect = m_prect[nIndex-m_nWidthCount+1];
- break;
- }
- }
- //判断下一个是否在右边的单元
- if(nIndex%m_nWidthCount < m_nWidthCount-1)
- {
- if(rect.right >= m_prect[nIndex+1].left
- && rect.top > m_prect[nIndex+1].top
- && rect.bottom < m_prect[nIndex+1].bottom)
- {
- m_NextRect = m_prect[nIndex+1];
- break;
- }
- }
- //判断下一个是否在右下边的单元
- //if(nIndex+m_nWidthCount+1 < m_nWidthCount*m_nHeightCount)
- if( (nIndex%m_nWidthCount < m_nWidthCount-1)
- && (nIndex/m_nWidthCount<m_nHeightCount-1) )
- {
- if(rect.right >= m_prect[nIndex+m_nWidthCount+1].left
- &&rect.bottom >= m_prect[nIndex+m_nWidthCount+1].top)
- {
- m_NextRect = m_prect[nIndex+m_nWidthCount+1];
- break;
- }
- }
- //判断下一个是否在上边的单元
- if( nIndex/m_nWidthCount>0 )
- {
- if(rect.top <= m_prect[nIndex-m_nWidthCount].bottom
- && rect.left > m_prect[nIndex-m_nWidthCount].left
- && rect.right < m_prect[nIndex-m_nWidthCount].right)
- {
- m_NextRect = m_prect[nIndex-m_nWidthCount];
- break;
- }
- }
- //判断下一个是否在下边的单元
- //if(nIndex+m_nWidthCount < m_nWidthCount*m_nHeightCount)
- if( nIndex/m_nWidthCount<m_nHeightCount-1 )
- {
- if(rect.bottom >= m_prect[nIndex+m_nWidthCount].top
- && rect.left > m_prect[nIndex+m_nWidthCount].left
- && rect.right < m_prect[nIndex+m_nWidthCount].right)
- {
- m_NextRect = m_prect[nIndex+m_nWidthCount];
- break;
- }
- }
- }//if(m_prect[nIndex].PtInRect(rect.CenterPoint()))
- nIndex++;
- }//for(x=0;x<m_nWidthCount;x++)
- }
- */
- }
- void CMoveTrace::JudgeAnalysis(int off)
- {
- /*int isize = m_nExtractWidth*m_nExtractHeight;//抽取后高度
- unsigned char* result = m_pGrey1+off*isize;
- int width=m_nExtractWidth;
- int height=m_nExtractHeight;
- double variance[256];
- int i, j, threshold;
- int level, Th;
- double bmax=0.0;
- double mean, mean1, mean2;
- double counter, counter1, counter2;
- double sum, sum1, sum2;
- //get the maximum grey level of original image
- level=-9999;
- int nIndex=0;
- for(i=0; i<height; i++)
- {
- for(j=0; j<width; j++)
- {
- level=(level<m_pGrey1[nIndex]) ? m_pGrey1[nIndex] : level;
- ++nIndex;
- }
- }
- if(level<=1) return ;
- counter=(double)isize;
- //get the globel mean value
- sum=0.0;
- nIndex=0;
- for(i=0; i<height; i++)
- {
- for(j=0; j<width; j++)
- {
- sum=sum+m_pGrey1[nIndex];
- mean=sum/counter;
- ++nIndex;
- }
- }*/
- /********* the beginning of Th loop ********
- grey less than threshold Th belong to Class 1;
- grey more than threshold Th belong to Class 2;
- where Th=0, 1, 2, 3, ..., level ************/
- /* for(Th=0; Th<=level; Th++)
- {
- //get the mean value of every class
- sum1=0.0;
- sum2=0.0;
- counter1=0;
- counter2=0;
- for(i=0; i<height; i++)
- {
- int line=i*width;
- for(j=0; j<width; j++)
- {
- if(m_pGrey1[line+j]<Th)
- {
- counter1=counter1+1;
- sum1=sum1+m_pGrey1[line+j];
- }
- else
- {
- counter2=counter2+1;
- sum2=sum2+m_pGrey1[line+j];
- }
- }
- }
- if(counter1>0) mean1=sum1/counter1;
- else mean1=0;
- if(counter2>0) mean2=sum2/counter2;
- else mean2=0;
- //calcuate the variance between classes
- variance[Th]=counter1*counter2*(mean1-mean2)*(mean1-mean2);
- }
- // get maximum variance to determine the best threshold
- for(Th=1; Th<=level; Th++)
- {
- if(bmax<variance[Th])
- {
- bmax=variance[Th];
- threshold=Th;
- }
- }
- ///////////////////////////////
- // do thresholding
- for(i=0; i<height-22; i++)
- {
- int line=i*width;
- for(j=0; j<width; j++)
- {
- result[line+j] = (m_pGrey1[line+j] >= threshold) ? 255: 0;
- }
- }*/
- ///////////////////////////
- }
- int CMoveTrace::GetThreshold(const int nMinThreshold)
- {
- //循环变量
- long i;
- //直方图数组
- long lHistogram[256];
- //阈值,最大灰度值与最小灰度值,两个区域的平均灰度值
- unsigned char iThreshold,iNewThreshold,iMaxGrayValue,iMinGrayValue,iMean1GrayValue,iMean2GrayValue;
- //用于计算区域灰度平均值的中间变量
- long lP1,lP2,lS1,lS2;
- //迭代次数
- int iIterationTimes;
- memset(lHistogram,0,sizeof(lHistogram));
- //获得直方图
- iMaxGrayValue = 0;
- iMinGrayValue = 255;
- int nIndex=0;
- int nTotal=m_nExtractWidth*m_nExtractHeight;
- for (i = 0;i < nTotal ;i++)
- {
- lHistogram[m_pGrey1[nIndex]]++;
- //修改最大,最小灰度值
- if(iMinGrayValue > m_pGrey1[nIndex])
- {
- iMinGrayValue = m_pGrey1[nIndex];
- }
- if(iMaxGrayValue < m_pGrey1[nIndex])
- {
- iMaxGrayValue = m_pGrey1[nIndex];
- }
- ++nIndex;
- }
- //迭代求最佳阈值
- iNewThreshold = (iMinGrayValue + iMaxGrayValue)/2;
- iThreshold = 0;
- for(iIterationTimes = 0; iThreshold != iNewThreshold && iIterationTimes < 100;iIterationTimes ++)
- {
- iThreshold = iNewThreshold;
- lP1 =0;
- lP2 =0;
- lS1 = 1;
- lS2 = 1;
- //求两个区域的灰度平均值
- for (i = iMinGrayValue;i < iThreshold;i++)
- {
- lP1 += lHistogram[i]*i;
- lS1 += lHistogram[i];
- }
- iMean1GrayValue = (unsigned char)(lP1 / lS1);
- for (i = iThreshold+1;i < iMaxGrayValue;i++)
- {
- lP2 += lHistogram[i]*i;
- lS2 += lHistogram[i];
- }
- iMean2GrayValue = (unsigned char)(lP2 / lS2);
- iNewThreshold = (iMean1GrayValue + iMean2GrayValue)/2;
- }
- TRACE(";;;;;;;;;;;;;;;;;;;;;;;;;;;%d",iThreshold);
- return max(nMinThreshold, iThreshold);
- }