private void ShowpictureBoxCamera_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && checkBox_Roi.Checked == true) { if (Convert.ToDouble(e.X - m_nDrawOffX) / m_fScale < mainform.roiAttr.nWidth && Convert.ToDouble(e.X - m_nDrawOffX) / m_fScale > 0 && Convert.ToInt32(Convert.ToDouble(e.X - m_nDrawOffX) / m_fScale) > Convert.ToInt32(H_offset.Text) && Convert.ToDouble(e.Y - m_nDrawOffY) / m_fScale < mainform.roiAttr.nHeight && Convert.ToDouble(e.Y - m_nDrawOffY) / m_fScale > 0 && Convert.ToInt32(Convert.ToDouble(e.Y - m_nDrawOffY) / m_fScale) > Convert.ToInt32(V_offset.Text)) { currRect.Width = e.X - currRect.X; currRect.Height = e.Y - currRect.Y; Rectangle _Rect = ShowpictureBoxCamera.RectangleToScreen(currRect); ControlPaint.DrawReversibleFrame(_Rect, Color.Green, FrameStyle.Thick); //ShowpictureBoxCamera.Refresh(); } } }
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(); }