imageobjectdlg.cpp

来自「实时视频捕获」· C++ 代码 · 共 1,641 行 · 第 1/4 页

CPP
1,641
字号
		wy=m_SysSetPara.dtr[i].region.Height();

		//get data from image
		l=0;
		for(int j=top;j<bottom;j++)
		{
			
			for(int k=left;k<right;k++)
			{
				index=3*(imageWidth*j+k);
				red=m_bufferVideo[index];
				green=m_bufferVideo[index+1];
				blue=m_bufferVideo[index+2];
				getRegionBuffer[l++]=(BYTE)(0.299*red+0.587*green+0.144*blue);
			}
		}
		//calculate vertical direction brightness average
		
		for(int k=0;k<wx;k++)
		{
			accamulit=0;
			for(int j=0;j<wy;j++)
			{
				accamulit+=getRegionBuffer[j*wx+k];	
			}
			accamulit=accamulit/wy;
			if(accamulit>255)accamulit=255;
			m_VAverageLight[i][k]=(BYTE)accamulit;
		}
		//calculate block brightness average
		accamulit=0;
		for(int k=0;k<wx;k++)
		{
			accamulit+=m_VAverageLight[i][k];
		}
		accamulit=accamulit/wx;
		if(accamulit>255)accamulit=255;
		m_BrightnessAverage[i]=(BYTE)accamulit;
		m_RegionLightHist[i][m_indexofLightHist]=accamulit;

	}
	m_indexofLightHist++;
	delete []getRegionBuffer;
}
void CImageObjectDlg::DisplayResult()
{
	CDC memDC;
	CClientDC dc(FromHandle(m_ShowImage.m_hWnd ));//this
	memDC.CreateCompatibleDC( &dc);

	COLORREF red=RGB(255,0,0);
	COLORREF green=RGB(0,255,0);
	COLORREF blue=RGB(0,0,255);
	COLORREF yellow=RGB(255,255,0);
	COLORREF pink=RGB(255,0,255);
	COLORREF white=RGB(255,255,255);
	CPen pen(PS_SOLID,1,white);
	CBrush *lpbrush=CBrush::FromHandle((HBRUSH)::GetStockObject(NULL_BRUSH));
	CPen *oldpen;

	memDC.SelectObject( &m_bmpMain);//CBitmap::FromHandle((HBITMAP)m_bmpMain.m_hObject) 
	CBrush*oldbrush;
	oldbrush=memDC.SelectObject(lpbrush);
	oldpen=memDC.SelectObject(&pen);
	CBitmap* pbmpOld = memDC.SelectObject( &m_bmpMain );
//	oldpen=dc.SelectObject(&pen);
//	oldbrush=dc.SelectObject(lpbrush);

	for(int i=0;i<m_SysSetPara.detect_num;i++)
	{
		if(i==m_iCurrentDetectRegionID)
		{
			if(m_iRegionDetectStatu[i]==NO_OBJECT)
			{
				pen.DeleteObject();
				pen.CreatePen(PS_SOLID,1,pink);
			}else
			{
				pen.DeleteObject();
				pen.CreatePen(PS_SOLID,1,yellow);
			}
		}else
		{
			if(m_iRegionDetectStatu[i]==NO_OBJECT)
			{
				pen.DeleteObject();
				pen.CreatePen(PS_SOLID,1,red);
			}else
			{
				pen.DeleteObject();
				pen.CreatePen(PS_SOLID,1,green);
			}
		}
		memDC.SelectObject(&pen);
		memDC.Rectangle(DetectRegion[i]);
	}
	dc.StretchBlt(0,0,IMAGE_WIDTH,IMAGE_HEIGHT,&memDC,0,0,IMAGE_WIDTH,IMAGE_HEIGHT,SRCCOPY);
	memDC.SelectObject(oldpen);
	memDC.SelectObject(oldbrush);
	memDC.SelectObject(pbmpOld);
	
/////CRichEdit Controler show data updata///////////////////////////////////////////
	if(!m_SetEnable)
	{
		CString str;
		//set detect parameter controller;
		m_ImageLightSlider.SetPos(m_SysSetPara.image_light);
		m_ImageContrastSlider.SetPos(m_SysSetPara.image_contrast);
		m_SetDetectNum.Clear();
		str.Format(_T("%d"),m_SysSetPara.detect_num);
		m_SetDetectNum.SetWindowText(str);
		m_SelectDetectRegion.SetCurSel(m_iCurrentDetectRegionID);
		str.Format(_T("%d"),m_SysSetPara.dtr[m_iCurrentDetectRegionID].region.Height());
		m_SetDetectRegionHeight.SetWindowText(str);
		str.Format(_T("%d"),m_SysSetPara.dtr[m_iCurrentDetectRegionID].region.Width());
		m_SetDetectRegionWidth.SetWindowText(str);
		str.Format(_T("%d"),m_SysSetPara.dtr[m_iCurrentDetectRegionID].threshold);
		m_SetDetectThreshold.SetWindowText(str);
		//detect info show controller;
		str.Format(_T("%d"),m_BrightnessAverage[0]);
		m_DetectRegion1Light.SetWindowText(str);
		str.Format(_T("%d"),m_BrightnessAverage[1]);
		m_DetectRegion2Light.SetWindowText(str);
		str.Format(_T("%d"),m_BrightnessAverage[2]);
		m_DetectRegion3Light.SetWindowText(str);
		str.Format(_T("%d"),m_BrightnessAverage[3]);
		m_DetectRegion4Light.SetWindowText(str);
		str.Format(_T("%d"),m_BrightnessAverage[4]);
		m_DetectRegion5Light.SetWindowText(str);
		str.Format(_T("%d"),m_BrightnessAverage[5]);
		m_DetectRegion6Light.SetWindowText(str);
		str.Format(_T("%d"),m_SysSetPara.dtr[0].threshold);
		m_DetectRegion1Threshold.SetWindowText(str);
		str.Format(_T("%d"),m_SysSetPara.dtr[1].threshold);
		m_DetectRegion2Threshold.SetWindowText(str);
		str.Format(_T("%d"),m_SysSetPara.dtr[2].threshold);
		m_DetectRegion3Threshold.SetWindowText(str);
		str.Format(_T("%d"),m_SysSetPara.dtr[3].threshold);
		m_DetectRegion4Threshold.SetWindowText(str);
		str.Format(_T("%d"),m_SysSetPara.dtr[4].threshold);
		m_DetectRegion5Threshold.SetWindowText(str);
		str.Format(_T("%d"),m_SysSetPara.dtr[5].threshold);
		m_DetectRegion6Threshold.SetWindowText(str);
		str.Format(_T("%d"),m_iDetectCountTotal);
		m_Counter.SetWindowText(str);
	}else
	{
		CString str;
		str.Format(_T("%d"),m_BrightnessAverage[0]);
		m_DetectRegion1Light.SetWindowText(str);
		str.Format(_T("%d"),m_BrightnessAverage[1]);
		m_DetectRegion2Light.SetWindowText(str);
		str.Format(_T("%d"),m_BrightnessAverage[2]);
		m_DetectRegion3Light.SetWindowText(str);
		str.Format(_T("%d"),m_BrightnessAverage[3]);
		m_DetectRegion4Light.SetWindowText(str);
		str.Format(_T("%d"),m_BrightnessAverage[4]);
		m_DetectRegion5Light.SetWindowText(str);
		str.Format(_T("%d"),m_BrightnessAverage[5]);
		m_DetectRegion6Light.SetWindowText(str);
		str.Format(_T("%d"),m_SysSetPara.dtr[0].threshold);
		m_DetectRegion1Threshold.SetWindowText(str);
		str.Format(_T("%d"),m_SysSetPara.dtr[1].threshold);
		m_DetectRegion2Threshold.SetWindowText(str);
		str.Format(_T("%d"),m_SysSetPara.dtr[2].threshold);
		m_DetectRegion3Threshold.SetWindowText(str);
		str.Format(_T("%d"),m_SysSetPara.dtr[3].threshold);
		m_DetectRegion4Threshold.SetWindowText(str);
		str.Format(_T("%d"),m_SysSetPara.dtr[4].threshold);
		m_DetectRegion5Threshold.SetWindowText(str);
		str.Format(_T("%d"),m_SysSetPara.dtr[5].threshold);
		m_DetectRegion6Threshold.SetWindowText(str);
		str.Format(_T("%d"),m_iDetectCountTotal);
		m_Counter.SetWindowText(str);
	}
/////Cuver plot Window show//////////////////////////////////////////////////////////
	if(m_SetEnable)
	{////set mode

		double scale;
		BYTE value;

		int threshold=m_SysSetPara.dtr[m_iCurrentDetectRegionID].threshold;
		scale=m_SysSetPara.dtr[m_iCurrentDetectRegionID].region.Width()/100;
		memset(m_bufferCurveBlock,240,100*256);
		for(int i=0;i<100;i++)
		{
			value=m_VAverageLight[m_iCurrentDetectRegionID][RoundDouble(i*scale)];
			m_bufferCurveBlock[(255-value)*100+i]=0;
//			m_bufferCurveBlock[(255-threshold)*100+i]=100;
		}
		m_bmpCurve.SetBitmapBits(100*256,m_bufferCurveBlock);
	}else
	{/// detect mode
		int value;
		memset(m_bufferCurveHist,240,100*256);
		int threshold=m_SysSetPara.dtr[m_iCurrentDetectRegionID].threshold;///
		threshold=(255-threshold)*100;
		int addr;
		for(int i=0;i<100;i++)
		{
			addr=(i+m_indexofLightHist)%100;
			value=(255-m_RegionLightHist[m_iCurrentDetectRegionID][addr])*100;
			m_bufferCurveHist[value+i]=0;
//			m_bufferCurveHist[threshold+i]=100;
		}
		m_bmpCurve.SetBitmapBits(100*256,m_bufferCurveHist);
	}

	CClientDC dcCurve(FromHandle(m_CurveWindow.m_hWnd));
	CDC tempDC;
	tempDC.CreateCompatibleDC(&dcCurve);
	CPalette * oldpalette;
	oldpalette=dcCurve.SelectPalette(&m_Palette,TRUE);
	pbmpOld=tempDC.SelectObject(&m_bmpCurve);
	CRect showRect ;
	m_CurveWindow.GetWindowRect(showRect);
	dcCurve.StretchBlt(0,0,showRect.Width (),showRect.Height(),&tempDC,0,0,100,256,SRCCOPY);
	
//	ReleaseDC(&tempDC);


}
/*
bool CImageObjectDlg::SetVideoPara()
{
	VIDEOSTREAMINFO vsi;
	int m_CardID=1;
	//设置视频制式
	DSStream_SetVideoStandard(m_CardID, SaveData.standard);
	//设置视频源
	DSStream_RouteInPinToOutPin(m_CardID, SaveData.source, 0);
	//设置视频属性:亮度、对比度、色度、饱和度
	DSStream_SetVideoPropertyValue(m_CardID, VideoProperty_Brightness, SaveData.brightness);
	DSStream_SetVideoPropertyValue(m_CardID, VideoProperty_Contrast, SaveData.contrast);
	//设置视频格式:视频格式、宽、高
	DSStream_GetVideoInfo(m_CardID, &vsi, PREVIEW);
	vsi.subtype = VideoSubType_YUY2;
	vsi.bmiHeader.biWidth = IMAGE_WIDTH;
	vsi.bmiHeader.biHeight= IMAGE_HEIGHT;
	DSStream_DisconnectPin(m_CardID, PREVIEW);
	DSStream_SetVideoInfo(m_CardID, vsi, PREVIEW);
	DSStream_ConnectPin(m_CardID, PREVIEW);

}
*/
//保存卡的当前属性,以备下次连接时重新设回

void CImageObjectDlg::OnBnClickedBtnSet()
{
	// TODO: Add your control notification handler code here
/*
	CFile fileDeliver;
				int k;
				CString str;
				if(fileDeliver.Open(_T("C:\\sendcount.txt"),CFile::modeReadWrite |CFile::osWriteThrough ,NULL))
				{
					char buffer[20];
					int readnum=fileDeliver.Read(buffer,20);
					if(readnum)
					{
						k=atoi(buffer);
						k=1000+k;
					}else
					{
						k=100;
					}
					fileDeliver.SeekToBegin();
					_itoa(k,buffer,10);
					str=buffer;
					fileDeliver.Write(buffer,20);
					fileDeliver.Close();
				}
*/
	if(m_SetEnable)
	{
		m_SetEnable=FALSE;
		this->m_bntSetDetect.SetWindowText(_T("检测设定"));
		this->m_SelectDetectRegion.EnableWindow(FALSE);
		this->m_SetDetectNum.EnableWindow(FALSE);
		this->m_SetDetectRegionHeight.EnableWindow(FALSE);
		this->m_SetDetectRegionWidth.EnableWindow(FALSE);
		this->m_SetDetectThreshold.EnableWindow(FALSE);
		this->m_ImageContrastSlider.EnableWindow(FALSE);
		this->m_ImageLightSlider.EnableWindow(FALSE);
		this->m_SelectDetectRegion.ShowWindow(SW_HIDE );
		this->m_SetDetectNum.ShowWindow(SW_HIDE );
		this->m_SetDetectRegionHeight.ShowWindow(SW_HIDE );
		this->m_SetDetectRegionWidth.ShowWindow(SW_HIDE );
		this->m_SetDetectThreshold.ShowWindow(SW_HIDE );
		this->m_ImageContrastSlider.ShowWindow(SW_HIDE );
		this->m_ImageLightSlider.ShowWindow(SW_HIDE );
		this->SaveParameter();
	}else
	{
		m_SetEnable=TRUE;
		this->m_bntSetDetect.SetWindowText(_T("设定完成"));
		this->m_SelectDetectRegion.ShowWindow(SW_SHOW    );
		this->m_SetDetectNum.ShowWindow(SW_SHOW    );
		this->m_SetDetectRegionHeight.ShowWindow(SW_SHOW    );
		this->m_SetDetectRegionWidth.ShowWindow(SW_SHOW    );
		this->m_SetDetectThreshold.ShowWindow(SW_SHOW    );
		this->m_ImageContrastSlider.ShowWindow(SW_SHOW    );
		this->m_ImageLightSlider.ShowWindow(SW_SHOW    );
		this->m_SelectDetectRegion.EnableWindow(TRUE);
		this->m_SetDetectNum.EnableWindow(TRUE);
		this->m_SetDetectRegionHeight.EnableWindow(TRUE);
		this->m_SetDetectRegionWidth.EnableWindow(TRUE);
		this->m_SetDetectThreshold.EnableWindow(TRUE);
		this->m_ImageContrastSlider.EnableWindow(TRUE);
		this->m_ImageLightSlider.EnableWindow(TRUE);
		m_SelectDetectRegion.SetCurSel(m_iCurrentDetectRegionID);
	}
}

void CImageObjectDlg::OnEnChangeRe2RegionCountSet()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
int end=m_SetDetectNum.GetTextLength();
	CString str;
	m_SetDetectNum.GetTextRange(0,end,str);
	int k=_ttoi(str);
	if(k<=0)
		return;
	if(k>MAX_DETECT_REGION_NUM)
	{
		str.Format(_T("%d"),MAX_DETECT_REGION_NUM);
		m_SetDetectNum.SetWindowText(str);
		m_SysSetPara.detect_num=MAX_DETECT_REGION_NUM;
	}else 
	{
		m_SelectDetectRegion.ResetContent();
		for(int i=0;i<k;i++)
		{
			str.Format(_T("%d%s"),i,_T("号检测区域"));
			this->m_SelectDetectRegion.InsertString(i,(LPCTSTR)str);
			m_SelectDetectRegion.SetCurSel(m_iCurrentDetectRegionID);
			
		}
		m_SysSetPara.detect_num=k;
	}
	Invalidate();
}

void CImageObjectDlg::OnEnKillfocusRe2RegionCountSet()
{
	// TODO: Add your control notification handler code here
	int end=m_SetDetectNum.GetTextLength();
	CString str;
	m_SetDetectNum.GetTextRange(0,end,str);
	int k=_ttoi(str);
	if(k<=0)
		return;
	if(k>MAX_DETECT_REGION_NUM)
	{
		str.Format(_T("%d"),MAX_DETECT_REGION_NUM);
		m_SetDetectNum.SetWindowText(str);
		m_SysSetPara.detect_num=MAX_DETECT_REGION_NUM;
	}else 
	{
		m_SelectDetectRegion.ResetContent();
		for(int i=0;i<k;i++)
		{
			str.Format(_T("%d%s"),(i+1),_T("号检测区域"));
			this->m_SelectDetectRegion.InsertString(i,(LPCTSTR)str);
			m_SelectDetectRegion.SetCurSel(m_iCurrentDetectRegionID);
			
		}
		m_SysSetPara.detect_num=k;
		m_DetectRegion6Light.ShowWindow(SW_HIDE);
		m_DetectRegion6Threshold.ShowWindow(SW_HIDE);
		m_DetectRegion5Light.ShowWindow(SW_HIDE);
		m_DetectRegion5Threshold.ShowWindow(SW_HIDE);
		m_DetectRegion4Light.ShowWindow(SW_HIDE);
		m_DetectRegion4Threshold.ShowWindow(SW_HIDE);
		m_DetectRegion3Light.ShowWindow(SW_HIDE);
		m_DetectRegion3Threshold.ShowWindow(SW_HIDE);
		m_DetectRegion2Light.ShowWindow(SW_HIDE);
		m_DetectRegion2Threshold.ShowWindow(SW_HIDE);
		m_DetectRegion1Light.ShowWindow(SW_HIDE);
		m_DetectRegion1Threshold.ShowWindow(SW_HIDE);
		switch(k)
		{
			case 6:
				m_DetectRegion6Light.ShowWindow(SW_SHOW);
				m_DetectRegion6Threshold.ShowWindow(SW_SHOW);
			case 5:
				m_DetectRegion5Light.ShowWindow(SW_SHOW);
				m_DetectRegion5Threshold.ShowWindow(SW_SHOW);
			case 4:
				m_DetectRegion4Light.ShowWindow(SW_SHOW);
				m_DetectRegion4Threshold.ShowWindow(SW_SHOW);
			case 3:
				m_DetectRegion3Light.ShowWindow(SW_SHOW);
				m_DetectRegion3Threshold.ShowWindow(SW_SHOW);
			case 2:
				m_DetectRegion2Light.ShowWindow(SW_SHOW);
				m_DetectRegion2Threshold.ShowWindow(SW_SHOW);
			case 1:
				m_DetectRegion1Light.ShowWindow(SW_SHOW);
				m_DetectRegion1Threshold.ShowWindow(SW_SHOW);
				break;

			default:
				break;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?