📄 grimsonbg.cpp
字号:
}
else
{
recorded=FALSE;
if (collx !=0)
{
for (ce=0; ce<collx; ce++) if ((coll[ce][0] ==TempBuffer[i-1][j]) && (coll[ce][1]==TempBuffer[i][j-1])) recorded=TRUE;//Dis_buff[x-3+y+2]) recorded=TRUE;
}
if (recorded==FALSE)
{
coll[collx][0]=TempBuffer[i-1][j];
coll[collx][1]=TempBuffer[i][j-1];
collx++;
}
}
}
else if ( aa ==bb) TempBuffer[i][j]= TempBuffer[i][j-1];//InsVfw.Dis_buff[x+y+2]=InsVfw.Dis_buff[x+y-w+2];
}
}
}
// verify whether there are a repeated label.
if (collx >0)
{
for (j=0; j<collx; j++)
{
for (i=0; i<collx; i++)
{
if (coll[i][1] == coll[j][0]) coll[i][1]=coll[j][1];
}
}
}
for (j=0; j<collx; j++)
{
for (i=0; i<collx; i++)
{
if ( (coll[i][0] == coll[j][0]) && (i != j))
{
if ( coll[i][1] < coll[j][1])
{
ttt=coll[j][1];
coll[j][1]=coll[i][1];
coll[i][0]=ttt;
for (jj=0; jj<i; jj++) if (coll[jj][1] == coll[i][0]) coll[jj][1]=coll[i][1];
}
else
{
ttt=coll[i][1];
coll[i][1]=coll[j][1];
coll[j][0]=ttt;
for (jj=0; jj<i; jj++) if (coll[jj][1] == coll[j][0]) coll[jj][1]=coll[j][1];
}
}
}
}
// for (j=0; j<collx; j++)
// {
// for (i=0; i<collx; i++)
// {
// if ( (coll[i][0] == coll[j][0]) && (i != j))
// {
// if ( coll[i][1] < coll[j][1])
// {
// ttt=coll[j][1];
// coll[j][1]=coll[i][1];
// coll[i][0]=ttt;
// for (jj=0; jj<i; jj++) if (coll[jj][1] == coll[i][0]) coll[jj][1]=coll[i][1];
// }
// else
// {
// ttt=coll[i][1];
// coll[i][1]=coll[j][1];
// coll[j][0]=ttt;
// for (jj=0; jj<i; jj++) if (coll[jj][1] == coll[j][0]) coll[jj][1]=coll[j][1];
// }
// }
// }
// }
// check the same [][0],
// merge to a segment that has the same [][0]
for (j=1; j<h; j++)
for (i=0; i<w; i++)
if (TempBuffer[i][j] !=0)
for (ce=collx; ce>=0; ce--)
if (TempBuffer[i][j] == coll[ce][0]) TempBuffer[i][j]=coll[ce][1];
// for (j=1; j<h; j++)
// for (i=0; i<w; i++)
// if (TempBuffer[i][j] !=0)
// for (ce=collx; ce>=0; ce--)
// if (TempBuffer[i][j] == coll[ce][0]) TempBuffer[i][j]=coll[ce][1];
// first, we calculate the size of each segment. Then, we assign the seg's value
// This can assure correct values.
m_iNumofSeg=0;
SHORT Seg_Info[400*2];
memset(Seg_Info, 0, sizeof(Seg_Info));
for (j=0; j<=m_biHeight-1; j++)
{
for (int i=0; i<=m_biWidth-1; i++)
{
if (TempBuffer[i][j] !=0)
{
recorded=FALSE;
if (m_iNumofSeg !=0)
{
for (int jj=0; jj<m_iNumofSeg; jj++)
if (TempBuffer[i][j] == unsigned(Seg_Info[jj*2+0])) // +0 : region value
{
recorded=TRUE;
Seg_Info[jj*2+1]++; // +1 : region size
jj=m_iNumofSeg-1;
}
if (recorded==FALSE)
{
Seg_Info[m_iNumofSeg*2+0]=TempBuffer[i][j];
Seg_Info[m_iNumofSeg*2+1]=1;
m_iNumofSeg++;
}
}
else
{
Seg_Info[m_iNumofSeg*2+0]=TempBuffer[i][j];
Seg_Info[m_iNumofSeg*2+1]=1;
m_iNumofSeg++;
}
}
}
}
// align Seg_Info high to low
int max_value=0, max_position=0;
int left=m_biWidth, right=0, top=m_biHeight, bottom=0;
// maximum number of segments is 255. the segment is aligned from large one to small one.
memset(Result, 0, 352*288);
int maxpositionvalue=0;
for (int loop=0; loop<256; loop++)
{
max_value=0;
max_position=0;
for (int II=0; II<m_iNumofSeg; II++)
{
if (Seg_Info[II*2+1] >=max_value)
{
max_value=Seg_Info[II*2+1];
max_position=II;
}
}
if ((max_value !=0))
{
if (m_iNumofSeg < 128)
{
for (int j=0; j<= m_biHeight-1; j++)
for (int i=0; i<=m_biWidth-1; i++)
{
if (TempBuffer[i][j] == unsigned(Seg_Info[max_position*2+0]))
{
maxpositionvalue=255/(loop+1);
Result[i+j*m_biWidth]=maxpositionvalue;//255/(loop+1);
if (left >= i) left=i;
if (right <= i) right =i;
if (top >=j) top=j;
if (bottom <= j) bottom=j;
}
}
}
else
{
for (int j=0; j<= m_biHeight-1; j++)
for (int i=0; i<=m_biWidth-1; i++)
{
if (TempBuffer[i][j] == unsigned(Seg_Info[max_position*2+0]))
{
maxpositionvalue=255-loop;
Result[i+j*m_biWidth]=maxpositionvalue;//255-loop;
if (left >= i) left=i;
if (right <= i) right =i;
if (top >=j) top=j;
if (bottom <= j) bottom=j;
}
}
}
position[loop].left=left;
position[loop].right=right;
position[loop].top=top;
position[loop].bottom=bottom;
// backup 256
// Bm_crResultPosition[loop+256*CurrentOffset].left=left;
// Bm_crResultPosition[loop+256*CurrentOffset].right=right;
// Bm_crResultPosition[loop+256*CurrentOffset].top=top;
// Bm_crResultPosition[loop+256*CurrentOffset].bottom=bottom;
region_info[loop*2+0]=maxpositionvalue; //Seg_Info[max_position*2+0]; // region value
region_info[loop*2+1]=max_value; // size of region
Seg_Info[max_position*2+1]=0;
}
else
{
region_info[loop*2+0]=0;
region_info[loop*2+1]=0; // size of region
loop=256;
}
}
// Be careful! The maximum value is 255.
}
void GrimsonBG::Display8bitPseudoColor(CDC *pDC, unsigned char *source, int xpos, int ypos)
{
int j,i;
if ((m_biWidth >0) && (m_biHeight >0))
{
// covert 8bits to 32bits
memset(m_ucTbuffer, 255, 352*288*4);
for ( j=0; j<m_biHeight; j++)
for (i=0; i<m_biWidth; i++)
{
if (source[i+j*m_biWidth]!=0)
{
m_ucTbuffer[j*m_biWidth*4+i*4+0]=200;
m_ucTbuffer[j*m_biWidth*4+i*4+1]=255-source[i+j*m_biWidth];
m_ucTbuffer[j*m_biWidth*4+i*4+2]=source[i+j*m_biWidth];
}
// memset(m_ucTbuffer+j*m_biWidth*4+i*4, source[i+j*m_biWidth], 3);
}
CBitmap bitmap;
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
bitmap.CreateCompatibleBitmap(pDC,m_biWidth,m_biHeight);
bitmap.SetBitmapBits(m_biWidth*m_biHeight*4,m_ucTbuffer);
dcMemory.SelectObject(&bitmap);
// dcMemory.SelectObject(m_lpBitmapInforHead);
pDC->BitBlt(xpos,ypos,m_biWidth,m_biHeight,&dcMemory,0,0,SRCCOPY);
}
}
void GrimsonBG::Display32bit(CDC *pDC, unsigned char* source, int xpos, int ypos)
{
int i,j;
if ((m_biWidth >0 )&&(m_biHeight >0))
{
CBitmap bitmap;
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
bitmap.CreateCompatibleBitmap(pDC,m_biWidth,m_biHeight);
bitmap.SetBitmapBits(m_biWidth*m_biHeight*4,source);
dcMemory.SelectObject(&bitmap);
// dcMemory.SelectObject(m_lpBitmapInforHead);
pDC->BitBlt(xpos,ypos,m_biWidth,m_biHeight,&dcMemory,0,0,SRCCOPY);
}
}
BOOL GrimsonBG::GetRGBXImage(unsigned char *rgbx)
{
if ((m_biWidth !=0) && (m_biHeight !=0) && (m_wBitCount > 16))
{
m_lCurrentFrame++;
m_bGetFrame=TRUE;
m_lpBitmapInforHead = (LPBITMAPINFOHEADER)AVIStreamGetFrame(m_pGf,m_lCurrentFrame-1);
if (m_lpBitmapInforHead != NULL)
{
memcpy(m_ucTbuffer, m_lpBitmapInforHead+1, m_biWidth*m_biHeight*(m_wBitCount/8));
memcpy(m_ucRgbTemp, m_ucTbuffer, m_biWidth*m_biHeight*(m_wBitCount/8));
}
else return 0;
// if the avi is 24bit, convert to 32bit
if (m_wBitCount==24)
{
for (int j=0; j<m_biHeight; j++)
for (int i=0; i< m_biWidth; i++)
memcpy (rgbx+j*m_biWidth*4+i*4, m_ucTbuffer+(m_biHeight-j-1)*m_biWidth*3+i*3, 3);
}
else if (m_wBitCount==32)
{
memcpy(rgbx, m_ucTbuffer, m_biWidth*m_biHeight*4);
}
return 1;
}
else return 0;
}
BOOL GrimsonBG::OpenAndInitialize()
{
char szFilter[] =
"avi file(*.avi)|*.avi|?All File(*.*)|*.*|";
CFileDialog OpenDlg(TRUE, "cif", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);
int val = OpenDlg.DoModal();
if(val ==IDOK)
{
m_csInfileName = OpenDlg.GetFileName();
if (AVIFileOpen(&m_pAviFile, m_csInfileName, OF_READ, NULL))
return 0;
else m_bAviOpened=TRUE;
if (!GetInfo()) return 0;
m_pGf = AVIStreamGetFrameOpen(m_pStream, NULL );
if (!m_pGf) return 0;
return 1;
}
else return 0;
}
BOOL GrimsonBG::GetInfo()
{
// Get the file Information
AVIFileInfo(m_pAviFile, &m_Info, sizeof(m_Info));
// findong audio and video streams
do
{
if(AVIFileGetStream(m_pAviFile, &m_pAudio[m_iNumAudioStreams], streamtypeAUDIO, m_iNumAudioStreams))
break;
}
while(++m_iNumAudioStreams < 5);
do
{
if(AVIFileGetStream(m_pAviFile, &m_pVideo[m_iNumVideoStreams], streamtypeVIDEO, m_iNumVideoStreams))
break;
}
while(++m_iNumVideoStreams < 5);
if (m_iNumVideoStreams >0) m_bStreamInit=TRUE;
// determin video format
for(int n = 0; n < m_iNumVideoStreams; n++)
{
m_pStream = m_pVideo[n];
LONG lSize;
if(AVIStreamReadFormat(m_pStream, AVIStreamStart(m_pStream), NULL, &lSize))
return 0;
LPBYTE pChunk = new BYTE[lSize];
if(!pChunk)
return 0;
if(AVIStreamReadFormat(m_pStream, AVIStreamStart(m_pStream), pChunk, &lSize))
return 0;
m_lpVideoFormats[n] = (LPBITMAPINFO)pChunk;
m_pBitmapInfo=(LPBITMAPINFO)pChunk;
m_biWidth=m_pBitmapInfo->bmiHeader.biWidth;
m_biHeight=m_pBitmapInfo->bmiHeader.biHeight;
m_wBitCount=m_pBitmapInfo->bmiHeader.biBitCount;
m_pStream = m_pVideo[0];
if ((m_biWidth > 352 ) || (m_biHeight > 288)) return 0;
}
return 1;
}
BOOL GrimsonBG::ConvertGrayImage(BYTE *rgb, BYTE *gray)
{
float total;
for (int j=0; j<m_biHeight; j++)
for (int i=0; i<m_biWidth; i++)
{
total=float(rgb[j*m_biWidth*4+i*4+0]+rgb[j*m_biWidth*4+i*4+1]+rgb[j*m_biWidth*4+i*4+2]);
gray[i+j*m_biWidth]= byte(total/3);
}
return 1;
}
void GrimsonBG::Display8bit(CDC *pDC, BYTE *source, int xpos, int ypos)
{
BYTE temp[320*240];
for(int j=0;j<m_biHeight;j++)
for(int i=0;i<m_biWidth;i++)
temp[i+j*m_biWidth] = source[i+(m_biHeight-j)*m_biWidth];
memcpy(source,temp,m_biWidth*m_biHeight);
if ((m_biWidth >0) && (m_biHeight >0))
{
// covert 8bits to 32bits
memset(m_ucTbuffer, 0, 352*288*4);
for (int j=0; j<m_biHeight; j++)
for (int i=0; i<m_biWidth; i++)
memset(m_ucTbuffer+j*m_biWidth*4+i*4, source[i+j*m_biWidth], 3);
CBitmap bitmap;
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
bitmap.CreateCompatibleBitmap(pDC,m_biWidth,m_biHeight);
bitmap.SetBitmapBits(m_biWidth*m_biHeight*4,m_ucTbuffer);
dcMemory.SelectObject(&bitmap);
// dcMemory.SelectObject(m_lpBitmapInforHead);
pDC->BitBlt(xpos,ypos,m_biWidth,m_biHeight,&dcMemory,0,0,SRCCOPY);
}
}
void GrimsonBG::DetectionMoveRegion(unsigned char *src, unsigned char *median, unsigned char *result)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -