private void setRoi(TUCAM_ROI_ATTR roiAttr) { WaitForFrameThread.Abort(); TUCamera.TUCAM_Buf_AbortWait(mainform.m_opCamList[mainform.m_nCamIndex].hIdxTUCam); // If you called TUCAM_Buf_WaitForFrames() TUCamera.TUCAM_Cap_Stop(mainform.m_opCamList[mainform.m_nCamIndex].hIdxTUCam); // Stop capture TUCamera.TUCAM_Buf_Release(mainform.m_opCamList[mainform.m_nCamIndex].hIdxTUCam); // Release alloc buffer after stop capture and quit drawing thread TUCamera.TUCAM_Cap_SetROI(mainform.m_opCamList[mainform.m_nCamIndex].hIdxTUCam, roiAttr); TUCamera.TUCAM_Cap_GetROI(/*m_opCam.hIdxTUCam*/ mainform.m_opCamList[mainform.m_nCamIndex].hIdxTUCam, ref roiAttr); mainform.m_drawframe.pBuffer = IntPtr.Zero; mainform.m_drawframe.ucFormatGet = (byte)TUFRM_FORMATS.TUFRM_FMT_RGB888; mainform.m_drawframe.uiRsdSize = 1U; TUCamera.TUCAM_Buf_Alloc(mainform.m_opCamList[mainform.m_nCamIndex].hIdxTUCam, ref mainform.m_drawframe); // Alloc buffer after set resolution or set ROI attribute TUCamera.TUCAM_Cap_Start(mainform.m_opCamList[mainform.m_nCamIndex].hIdxTUCam, 0U); WaitForFrameThread = new System.Threading.Thread(new ThreadStart(WaitForFrame)); WaitForFrameThread.Start(); ShowpictureBoxCamera.Refresh(); }
private void textBox_Expo_TextChanged(object sender, EventArgs e) { if (textBox_Expo.Text != "") { mainform.dbExp = Convert.ToDouble(textBox_Expo.Text); TUCamera.TUCAM_Prop_SetValue(mainform.m_opCam.hIdxTUCam, (int)TUCAM_IDPROP.TUIDP_EXPOSURETM, mainform.dbExp, 0); } }
private void CamConfigForm_FormClosed(object sender, FormClosedEventArgs e) { check_top_auto.Checked = false; TUCamera.TUCAM_Draw_Uninit(mainform.m_opCam.hIdxTUCam); TUCamera.TUCAM_Draw_Init(mainform.m_opCam.hIdxTUCam, mainform.m_itDraw); timer.Stop(); // WaitForFrameThread.Abort(); mainform.timer.Start(); mainform.Init_Layout(mainform.tableLayoutPanel, mainform.NumTube, mainform.NumSeries); mainform.InitDataCache(mainform.NumTube, mainform.NumSeries); }
private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { if (camConfigForm != null) { camConfigForm.WaitForFrameThread.Abort(); } for (int i = 0; i < m_opCamList.Count; i++) { TUCamera.TUCAM_Dev_Close(m_opCamList[i].hIdxTUCam); TUCamera.TUCAM_Draw_Uninit(m_opCamList[i].hIdxTUCam); } TUCamera.TUCAM_Api_Uninit(); // 释放SDK 资源环境 }
public void StartWaitForFrame(TUCAM_OPEN openCam) { //if (m_bWaitting) // return; m_bWaitting = true; if ((IntPtr)0 == m_hThdWaitForFrame) { m_drawframe.pBuffer = IntPtr.Zero; m_drawframe.ucFormatGet = (byte)TUFRM_FORMATS.TUFRM_FMT_RGB888; m_drawframe.uiRsdSize = 1; // how many frames do you want int nValue = 0; if ((int)TUCAM_CAPTURE_MODES.TUCCM_SEQUENCE != m_nTriMode) { nValue = (int)TUCamera.TUCAM_Cap_GetTrigger(openCam.hIdxTUCam, ref attrTgr); attrTgr.nTgrMode = m_nTriMode; attrTgr.nFrames = -1; // how many frames do you want to capture to RAM(the frames less than 0, use maximum frames ) nValue = (int)TUCamera.TUCAM_Cap_SetTrigger(openCam.hIdxTUCam, attrTgr); } else { nValue = (int)TUCamera.TUCAM_Cap_GetTrigger(openCam.hIdxTUCam, ref attrTgr); attrTgr.nTgrMode = (int)TUCAM_CAPTURE_MODES.TUCCM_SEQUENCE; attrTgr.nFrames = 1; // TUCCM_SEQUENCE must set 1 frame nValue = (int)TUCamera.TUCAM_Cap_SetTrigger(openCam.hIdxTUCam, attrTgr); } TUCamera.TUCAM_Buf_Alloc(openCam.hIdxTUCam, ref m_drawframe); // Alloc buffer after set resolution or set ROI attribute TUCamera.TUCAM_Cap_Start(openCam.hIdxTUCam, (uint)attrTgr.nTgrMode); // Start capture m_dwFrmCnt = 0; // reset frame count //m_dwSTm = TUCamera.GetTickCount(); // reset start tick count m_hThdWaitForFrame = TUCamera.CreateEvent(HANDLE.Zero, false, true, string.Empty); //WaitForFrameThread.Start(); } //EnableControl(m_bWaitting); //EnableTriggerControl(!m_bWaitting); }
//初始化绘制资源 private void InitDrawingResource() { int nSize = 0; unsafe { nSize = sizeof(BITMAPINFOHEADER); //memset(&m_bmpInfo, 0, nSize); } m_bmpInfo.biSize = nSize; m_bmpInfo.biPlanes = 1; m_bmpInfo.biBitCount = 24; m_bmpInfo.biCompression = 0; //hDraw = ShowpictureBox.Handle; hMainWin = TUCamera.FindWindow(null, "COVID19_Detection"); m_hDC = TUCamera.GetDC(hDraw); m_hDib = TUCamera.DrawDibOpen(); }
private void CamConfigForm_Load(object sender, EventArgs e) { TUCamera.TUCAM_Draw_Uninit(mainform.m_opCam.hIdxTUCam); m_itDraw.hWnd = this.ShowpictureBoxCamera.Handle; m_itDraw.ucChannels = (sbyte)mainform.valueinfo.nValue; m_itDraw.nWidth = 2048; m_itDraw.nHeight = 1148; m_itDraw.nMode = 0; TUCamera.TUCAM_Draw_Init(mainform.m_opCam.hIdxTUCam, m_itDraw); textBox_Expo.Text = mainform.dbExp.ToString(); H_offset.Text = mainform.roiAttr.nHOffset.ToString(); V_offset.Text = mainform.roiAttr.nVOffset.ToString(); ROI_Height.Text = mainform.roiAttr.nHeight.ToString(); ROI_Width.Text = mainform.roiAttr.nWidth.ToString(); checkBox_Roi.Checked = true; PicNum.Value = mainform.Pic_num; Tube_Num.Value = 0; H_num.Value = mainform.NumSeries; V_Pixel.Value = 10; check_tube.Checked = false; check_top_auto.Checked = true; timer = new System.Windows.Forms.Timer(); timer.Interval = 10; timer.Tick += timer1_Tick; timer.Start(); WaitForFrameThread = new System.Threading.Thread(new ThreadStart(WaitForFrame)); WaitForFrameThread.Start(); InitChart(); }
private void MainForm_Load(object sender, EventArgs e) { //IntPtr strPath = Marshal.StringToHGlobalAnsi(System.Environment.CurrentDirectory); m_opCamList = new List <TUCAM_OPEN>(); m_itApi.uiCamCount = 0; //m_itApi.pstrConfigPath = strPath; TUCamera.TUCAM_Api_Init(ref m_itApi); if (0 == m_itApi.uiCamCount) { MessageBox.Show("Init Camera Error!"); return; } for (int i = 0; i < m_itApi.uiCamCount; i++) { m_opCam.uiIdxOpen = (uint)i; TUCamera.TUCAM_Dev_Open(ref m_opCam); m_opCamList.Add(m_opCam); valueinfo.nValue = 1; valueinfo.nID = (int)TUCAM_IDINFO.TUIDI_CAMERA_CHANNELS; TUCamera.TUCAM_Dev_GetInfo(m_opCam.hIdxTUCam, ref valueinfo); if (1 < m_itApi.uiCamCount) { TUCamera.TUCAM_Capa_SetValue(m_opCamList[m_nCamIndex].hIdxTUCam, (int)TUCAM_IDCAPA.TUIDC_CAM_MULTIPLE, (int)m_itApi.uiCamCount); } //m_itDraw.hWnd = ShowpictureBox.Handle; m_itDraw.ucChannels = (sbyte)valueinfo.nValue; m_itDraw.nWidth = 2048; m_itDraw.nHeight = 2048; m_itDraw.nMode = 0; TUCamera.TUCAM_Draw_Init(m_opCam.hIdxTUCam, m_itDraw); } if (0 == (long)m_opCam.hIdxTUCam) { MessageBox.Show("Open Camera Faild!"); return; } m_nCamIndex = 0; m_opCam = m_opCamList[m_nCamIndex]; double dbGain = 0; // 获取增益值 TUCamera.TUCAM_Prop_GetValue(m_opCam.hIdxTUCam, (int)TUCAM_IDPROP.TUIDP_GLOBALGAIN, ref dbGain, 0); // 获取曝光时间 TUCamera.TUCAM_Prop_GetValue(m_opCam.hIdxTUCam, (int)TUCAM_IDPROP.TUIDP_EXPOSURETM, ref dbExp, 0); // 关闭自动曝光 TUCamera.TUCAM_Capa_SetValue(m_opCam.hIdxTUCam, (int)TUCAM_IDCAPA.TUIDC_ATEXPOSURE, 0); // 关闭自动白平衡 TUCamera.TUCAM_Capa_SetValue(m_opCam.hIdxTUCam, (int)TUCAM_IDCAPA.TUIDC_ATWBALANCE, 0); // 设置曝光时间 // TUCamera.TUCAM_Prop_SetValue(m_opCam.hIdxTUCam, (int)TUCAM_IDPROP.TUIDP_EXPOSURETM, dbExp, 0); // 设置增益值 //TUCamera.TUCAM_Prop_SetValue(m_opCam.hIdxTUCam, (int)TUCAM_IDPROP.TUIDP_GLOBALGAIN, dbGain, 0); valText.nTextSize = 64; string strp = "000000000000000000000000000"; valText.pText = Marshal.StringToHGlobalAnsi(strp); // 获取分辨率范围 attrCapa.idCapa = (int)TUCAM_IDCAPA.TUIDC_RESOLUTION; if (TUCAMRET.TUCAMRET_SUCCESS == TUCamera.TUCAM_Capa_GetAttr(/*m_opCam.hIdxTUCam*/ m_opCam.hIdxTUCam, ref attrCapa)) { int nCnt = attrCapa.nValMax - attrCapa.nValMin + 1; string[] szRes = new string[nCnt]; valText.nID = (int)TUCAM_IDCAPA.TUIDC_RESOLUTION; for (int i = 0; i < nCnt; ++i) { valText.dbValue = i; TUCamera.TUCAM_Capa_GetValueText(/*m_opCam.hIdxTUCam*/ m_opCam.hIdxTUCam, ref valText); szRes[i] = Marshal.PtrToStringAnsi(valText.pText); } } InitDrawingResource(); timer = new Timer(); timer.Interval = 50; timer.Tick += timer1_Tick; timer.Start(); //Init_Layout(tableLayoutPanel, NumTube, NumSeries); //InitDataCache(NumTube, NumSeries); StartWaitForFrame(m_opCamList[m_nCamIndex]); // InitChart(NumChartArea, 10); m_drawframe.ucFormatGet = (byte)TUFRM_FORMATS.TUFRM_FMT_RGB888; if (TUCAMRET.TUCAMRET_SUCCESS == TUCamera.TUCAM_Buf_WaitForFrame(m_opCamList[m_nCamIndex].hIdxTUCam, ref m_drawframe)) { m_nCurWidth = m_bmpInfo.biWidth = m_drawframe.usWidth; m_nCurHeight = m_bmpInfo.biHeight = m_drawframe.usHeight; } TUCamera.TUCAM_Cap_GetROI(/*m_opCam.hIdxTUCam*/ m_opCamList[m_nCamIndex].hIdxTUCam, ref roiAttr); // roiAttr.bEnable = false; roiAttr.nVOffset = 0; roiAttr.nHOffset = 0; roiAttr.nWidth = m_bmpInfo.biWidth; roiAttr.nHeight = m_bmpInfo.biHeight; TUCamera.TUCAM_Cap_SetROI(/*m_opCam.hIdxTUCam*/ m_opCamList[m_nCamIndex].hIdxTUCam, roiAttr); //camConfigForm = new CamConfigForm(this); }
public void WaitForFrame() { Control.CheckForIllegalCrossThreadCalls = false; int nCliWidth = 0; int nCliHeight = 0; m_nDrawOffX = 0; m_nDrawOffY = 0; m_nDrawWidth = 0; m_nDrawHeight = 0; m_nCurWidth = 0; m_nCurHeight = 0; int num = 0; while (mainform.m_bWaitting) { // 计算帧率 //m_dwFrmCnt++; // 帧数加1 //m_dwITm = TUCamera.GetTickCount() - m_dwSTm; // 计算间隔时间 ms //if (m_dwITm > 1000) //{ // m_fFps = m_dwFrmCnt * 1000000.0f / m_dwITm; // TUCamera.PostMessage(hMainWin, REFRESH_FRAMELABEL, 0, (int)m_fFps); // m_dwSTm = TUCamera.GetTickCount(); // m_dwFrmCnt = 0; //} //绘图 mainform.m_drawframe.ucFormatGet = (byte)TUFRM_FORMATS.TUFRM_FMT_RGB888; if (TUCAMRET.TUCAMRET_SUCCESS == TUCamera.TUCAM_Buf_WaitForFrame(mainform.m_opCamList[mainform.m_nCamIndex].hIdxTUCam, ref mainform.m_drawframe)) { // 改变显示区域 if (ShowpictureBoxCamera.Width != mainform.m_drawframe.usWidth || ShowpictureBoxCamera.Height != mainform.m_drawframe.usHeight || m_nCliWidth != nCliWidth || m_nCliHeight != nCliHeight) { m_nCurWidth = mainform.m_bmpInfo.biWidth = mainform.m_drawframe.usWidth; m_nCurHeight = mainform.m_bmpInfo.biHeight = mainform.m_drawframe.usHeight; nCliWidth = ShowpictureBoxCamera.Width; nCliHeight = ShowpictureBoxCamera.Height; float fScaleX = nCliWidth * 1.0f / m_nCurWidth; float fScaleY = nCliHeight * 1.0f / m_nCurHeight; m_fScale = fScaleX > fScaleY ? fScaleY : fScaleX; m_fScale = (float)((int)(m_fScale * 100) / 100.0f); if (m_fScale < 1) { m_nDrawWidth = (int)(m_fScale * m_nCurWidth); m_nDrawHeight = (int)(m_fScale * m_nCurHeight); } else { m_nDrawWidth = m_nCurWidth; m_nDrawHeight = m_nCurHeight; } m_nDrawWidth = (m_nDrawWidth >> 2) << 2; m_nDrawHeight = (m_nDrawHeight >> 2) << 2; m_nDrawOffX = (nCliWidth - m_nDrawWidth) / 2; m_nDrawOffY = (nCliHeight - m_nDrawHeight) / 2; } // 绘制图像 if (null != mainform.m_drawframe.pBuffer) { mainform.m_drawing.pFrame = Marshal.AllocHGlobal(Marshal.SizeOf(mainform.m_drawframe)); Marshal.StructureToPtr(mainform.m_drawframe, mainform.m_drawing.pFrame, true); int nSize = (int)(mainform.m_drawframe.uiImgSize + mainform.m_drawframe.usHeader); byte[] pBuf = new byte[nSize]; Marshal.Copy(mainform.m_drawframe.pBuffer, pBuf, 0, nSize); //Buffer.BlockCopy(pBuf, (int)mainform.m_drawframe.usHeader, pBuf, 0, (int)mainform.m_drawframe.uiImgSize); for (int i = 0; i < mainform.m_drawframe.usWidth; i++) { for (int j = 0; j < mainform.m_drawframe.usHeight; j++) { byte t = pBuf[mainform.m_drawframe.usHeader + i * 3 + (j) * (int)mainform.m_drawframe.uiWidthStep]; byte t1, t2, t3; if (t < 64) { t1 = 0; t2 = 4 * 4; t3 = 255; } else if (t >= 65 && t < 128) { t1 = 0; t2 = 255; t3 = (byte)(2 * 255 - 4 * t); } else if (t >= 128 && t < 192) { t1 = (byte)(4 * t - 2 * 255); t2 = 255; t3 = 0; } else { t1 = 255; t2 = (byte)(4 * t - 2 * 255); t3 = 0; } pBuf[mainform.m_drawframe.usHeader + i * 3 + (j) * (int)mainform.m_drawframe.uiWidthStep] = t3; pBuf[mainform.m_drawframe.usHeader + i * 3 + 1 + (j) * (int)mainform.m_drawframe.uiWidthStep] = t2; pBuf[mainform.m_drawframe.usHeader + i * 3 + 2 + (j) * (int)mainform.m_drawframe.uiWidthStep] = t1; } } IntPtr a = mainform.m_drawframe.pBuffer; Marshal.Copy(pBuf, 0, a, nSize); mainform.m_drawing.nDstX = m_nDrawOffX; mainform.m_drawing.nDstY = m_nDrawOffY; mainform.m_drawing.nDstWidth = m_nDrawWidth; mainform.m_drawing.nDstHeight = m_nDrawHeight; mainform.m_drawing.nSrcX = 0; mainform.m_drawing.nSrcY = 0; mainform.m_drawing.nSrcWidth = m_nCurWidth; mainform.m_drawing.nSrcHeight = m_nCurHeight; TUCamera.TUCAM_Draw_Frame(mainform.m_opCamList[mainform.m_nCamIndex].hIdxTUCam, ref mainform.m_drawing); if (draw_flag) { Rectangle _Rect = ShowpictureBoxCamera.RectangleToScreen(currRect); ControlPaint.DrawReversibleFrame(_Rect, Color.Green, FrameStyle.Dashed); } } mainform.m_drawframe.ucFormatGet = (byte)TUFRM_FORMATS.TUFRM_FMT_USUAl; if (TUCAMRET.TUCAMRET_SUCCESS == TUCamera.TUCAM_Buf_CopyFrame(/*m_opCam.hIdxTUCam*/ mainform.m_opCamList[mainform.m_nCamIndex].hIdxTUCam, ref mainform.m_drawframe)) { int nSize = (int)(mainform.m_drawframe.uiImgSize + mainform.m_drawframe.usHeader); byte[] pBuf = new byte[nSize]; if (num == 0) { average_temp = new double[mainform.m_drawframe.uiImgSize / mainform.m_drawframe.ucDepth]; } Marshal.Copy(mainform.m_drawframe.pBuffer, pBuf, 0, nSize); Buffer.BlockCopy(pBuf, (int)mainform.m_drawframe.usHeader, pBuf, 0, (int)mainform.m_drawframe.uiImgSize); double[] sum = new double[mainform.m_drawframe.usWidth]; for (int i = 0; i < mainform.m_drawframe.uiWidthStep; i += mainform.m_drawframe.ucDepth) { for (int j = 0; j < mainform.m_drawframe.usHeight; j++) { //byte a = pBuf[i + (j) * (int)mainform.m_drawframe.uiWidthStep]; //byte b = pBuf[i+1 + (j) * (int)mainform.m_drawframe.uiWidthStep]; // sum[i / mainform.m_drawframe.ucDepth] += (double)((ushort)pBuf[i + (j) * (int)mainform.m_drawframe.uiWidthStep] + pBuf[i + 1 + (j) * (int)mainform.m_drawframe.uiWidthStep] * 256); //sum[i / mainform.m_drawframe.ucDepth] += (double)((ushort)pBuf[i +1+ (j) * (int)mainform.m_drawframe.uiWidthStep] ); average_temp[i / mainform.m_drawframe.ucDepth + (j) * (int)mainform.m_drawframe.usWidth] += (double)((ushort)pBuf[i + (j) * (int)mainform.m_drawframe.uiWidthStep] + pBuf[i + 1 + (j) * (int)mainform.m_drawframe.uiWidthStep] * 256); } } num++; if (num >= mainform.Pic_num) { num = 0; for (int i = 0; i < mainform.m_drawframe.uiWidthStep; i += mainform.m_drawframe.ucDepth) { for (int j = 0; j < mainform.m_drawframe.usHeight; j++) { average[i / mainform.m_drawframe.ucDepth + (j) * (int)mainform.m_drawframe.usWidth] = average_temp[i / mainform.m_drawframe.ucDepth + (j) * (int)mainform.m_drawframe.usWidth] / mainform.Pic_num; } } for (int i = 0; i < mainform.m_drawframe.usWidth; i++) { for (int j = 0; j < mainform.m_drawframe.usHeight; j++) { sum[i] += average[i + j * mainform.m_drawframe.usWidth]; } } double a = 0; for (int i = 0; i < mainform.m_drawframe.usWidth; i++) { H_averg[i] = sum[i] / mainform.m_drawframe.usHeight; a += H_averg[i]; } a /= mainform.m_drawframe.usWidth; if (check_tube.Checked == true) { //double[] top = new double[mainform.m_drawframe.usWidth]; if (check_top_auto.Checked == true) { n = 0; for (int i = decimal.ToInt32(V_Pixel.Value); i < mainform.m_drawframe.usWidth - V_Pixel.Value; i++) { if (((H_averg[i] - H_averg[i - 1]) * (H_averg[i + 1] - H_averg[i]) < 0) && (H_averg[i] < a)) { top[n] = i; n++; } } bool flag = true; while (flag) { flag = false; int nn = n; for (int i = 1; i < nn; i++) { if ((top[i] - top[i - 1] < Convert.ToInt32(V_Pixel.Value) * 2) && (top[i] != top[i - 1])) { flag = true; n--; if (H_averg[Convert.ToInt32(top[i])] < H_averg[Convert.ToInt32(top[i - 1])]) { top[i - 1] = 0; } else { top[i] = 0; } } } for (int i = 0; i < nn; i++) { if (top[i] == 0) { for (int j = i; j < nn + 1; j++) { top[j] = top[j + 1]; } if ((top[i] == 0) && (i < n)) { i--; } } } } if (mainform.NumTube <= n) { tubenum = n; for (int nn = 0; nn < mainform.NumTube; nn++) { double min = H_averg[Convert.ToInt32(top[nn])]; for (int i = (top[nn] - Convert.ToInt32(V_Pixel.Value)) > 0 ? (top[nn] - Convert.ToInt32(V_Pixel.Value)) : 0; i < ((top[nn] + Convert.ToInt32(V_Pixel.Value)) < 2048 ? (top[nn] + Convert.ToInt32(V_Pixel.Value)) : 2048); i++) { if (H_averg[top[i]] < min) { min = H_averg[Convert.ToInt32(top[nn])]; top[nn] = i; } } } } } if (mainform.NumTube <= n) { for (int nn = 0; nn < mainform.NumTube; nn++) { int height_temp = mainform.m_drawframe.usHeight / mainform.NumSeries; for (int k = 0; k < mainform.NumSeries; k++) { double sum_temp = 0; for (int i = (top[nn] - Convert.ToInt32(V_Pixel.Value)) > 0 ? (top[nn] - Convert.ToInt32(V_Pixel.Value)) : 0; i < ((top[nn] + Convert.ToInt32(V_Pixel.Value)) < 2048 ? (top[nn] + Convert.ToInt32(V_Pixel.Value)) : 2048); i++) { for (int j = k * height_temp; j < (k + 1) * height_temp; j++) { sum_temp += average[i + j * mainform.m_drawframe.usWidth]; } } mainform.value_to_show[nn, k] = sum_temp / (height_temp * (Convert.ToInt32(V_Pixel.Value) * 2 + 1)); } } mainform.UpdateListValue(); } if (mainform.IsSave) { double[] temp = new double[mainform.NumSeries]; int height_temp = mainform.m_drawframe.usHeight / mainform.NumSeries; for (int nn = 0; nn < mainform.NumTube; nn++) { for (int i = -Convert.ToInt32(V_Pixel.Value); i < Convert.ToInt32(V_Pixel.Value); i++) { for (int k = 0; k < mainform.NumSeries; k++) { double sum_temp = 0; for (int j = k * height_temp; j < (k + 1) * height_temp; j++) { sum_temp += average[top[nn] + i + j * mainform.m_drawframe.usWidth]; } temp[k] = sum_temp / height_temp; } string path = mainform.file_path + "\\" + nn.ToString() + "_" + i.ToString() + ".txt"; mainform.WriteDataFile(path, temp, false); } } } } } } } } WaitForFrameThread.Abort(); }