/// <summary> /// Invert the target /// </summary> /// <param name="bm"></param> /// <returns></returns> public override Bitmap InvertTarget(Bitmap bm) { CogImage24PlanarColor imgColor = new CogImage24PlanarColor(bm); imgColor = InvertTarget(imgColor) as CogImage24PlanarColor; return(imgColor.ToBitmap()); }
/// <summary> /// Assign the input image /// </summary> /// <param name="cogImage"></param> private void AssignInputImage(object oImage) { if (oImage != null) { try { ICogImage cogImage = oImage as ICogImage; if (cogImage == null) { if (oImage is Bitmap) { // Assume bitmap cogImage = new CogImage24PlanarColor(oImage as Bitmap); } else { return; } } CogToolBlock grabberBuffer = GetGrabberBuffer(); if (grabberBuffer != null) { grabberBuffer.Outputs["OutputBuffer"].Value = cogImage; } } catch (Exception ex) { U.LogPopup(ex, "Error AssignInputImage image to the Cog vpp file"); } } }
//***************************************************************************************** // Conversion //***************************************************************************************** public ICogImage Conv_BitmapToCogImage(Bitmap bitmap) { CogImage24PlanarColor image = new CogImage24PlanarColor(bitmap); ICogImage cog = image; return(cog); }
/// <summary> /// Copy the image produced by the camera /// </summary> /// <param name="oImage"></param> /// <returns></returns> public override object CopyImage(object oImage) { if (oImage is CogImage24PlanarColor) { CogImage24PlanarColor copyColorImage = new CogImage24PlanarColor(oImage as CogImage24PlanarColor); return(copyColorImage); } else if (oImage is ICogImage) { return((oImage as ICogImage).CopyBase(CogImageCopyModeConstants.CopyPixels)); } return(base.CopyImage(oImage)); }
/// <summary> /// 显示图片 /// </summary> /// <param name="nHeight">高</param> /// <param name="nWidth">宽</param> /// <param name="pImageBuf">图片数据</param> /// <param name="enPixelType">像素格式</param> public void VisionProDisplay(UInt32 nHeight, UInt32 nWidth, IntPtr pImageBuf, MyCamera.MvGvspPixelType enPixelType) { // ch: 显示 || display try { if (enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8) { CogImage8Root cogImage8Root = new CogImage8Root(); cogImage8Root.Initialize((Int32)nWidth, (Int32)nHeight, pImageBuf, (Int32)nWidth, null); CogImage8Grey cogImage8Grey = new CogImage8Grey(); cogImage8Grey.SetRoot(cogImage8Root); this.cogDisplayImage.Image = cogImage8Grey.ScaleImage((int)nWidth, (int)nHeight); System.GC.Collect(); } else { CogImage8Root image0 = new CogImage8Root(); IntPtr ptr0 = new IntPtr(pImageBuf.ToInt64()); image0.Initialize((int)nWidth, (int)nHeight, ptr0, (int)nWidth, null); CogImage8Root image1 = new CogImage8Root(); IntPtr ptr1 = new IntPtr(pImageBuf.ToInt64() + m_nRowStep); image1.Initialize((int)nWidth, (int)nHeight, ptr1, (int)nWidth, null); CogImage8Root image2 = new CogImage8Root(); IntPtr ptr2 = new IntPtr(pImageBuf.ToInt64() + m_nRowStep * 2); image2.Initialize((int)nWidth, (int)nHeight, ptr2, (int)nWidth, null); CogImage24PlanarColor colorImage = new CogImage24PlanarColor(); colorImage.SetRoots(image0, image1, image2); this.cogDisplayImage.Image = colorImage.ScaleImage((int)nWidth, (int)nHeight); System.GC.Collect(); } } catch (System.Exception ex) { MessageBox.Show(ex.ToString()); return; } return; }
/// <summary> /// Get the bits of the image for editing /// </summary> /// <param name="oImage"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="stride"></param> /// <param name="ptrRed"></param> /// <param name="ptrGreen"></param> /// <param name="ptrBlue"></param> /// <returns></returns> public override object GetImageBits(object oImage, ref int width, ref int height, ref int stride, ref IntPtr ptrRed, ref IntPtr ptrGreen, ref IntPtr ptrBlue) { if (oImage is CogImage24PlanarColor) { CogImage24PlanarColor imgColor = oImage as CogImage24PlanarColor; ICogImage8PixelMemory memRed, memGreen, memBlue; width = imgColor.Width; height = imgColor.Height; stride = width + 8; imgColor.Get24PlanarColorPixelMemory(CogImageDataModeConstants.ReadWrite, 0, 0, width, height, out memRed, out memGreen, out memBlue); ptrRed = memRed.Scan0; ptrGreen = memGreen.Scan0; ptrBlue = memBlue.Scan0; return(new ImagePixelLock() { MemRed = memRed, MemGreen = memGreen, MemBlue = memBlue }); } return(null); }
private ICogImage InvertTarget(ICogImage cogImage) { if (cogImage is CogImage24PlanarColor) { CogImage24PlanarColor imgColor = cogImage as CogImage24PlanarColor; if (HasInvertTargetEvent) { ICogImage8PixelMemory memRed, memGreen, memBlue; int width = imgColor.Width; int height = imgColor.Height; int memRowWidth = width + 8; imgColor.Get24PlanarColorPixelMemory(CogImageDataModeConstants.ReadWrite, 0, 0, width, height, out memRed, out memGreen, out memBlue); FireInvertTarget(width, height, memRowWidth, memRed.Scan0, memGreen.Scan0, memBlue.Scan0); memRed.Dispose(); memGreen.Dispose(); memBlue.Dispose(); } cogImage = imgColor; } return(cogImage); }
private void LogToDisk_ExceptionEaten() { try { using (var fileTool = new CogImageFileTool()) { var img = _display.CreateContentBitmap(_bitmapStyle); var bmp = new Bitmap(img); var cogImage = new CogImage24PlanarColor(bmp); fileTool.InputImage = cogImage; fileTool.Operator.Open(_recentImagePath, CogImageFileModeConstants.Write); fileTool.Run(); } } catch { } }
public override void AssignAcquireImageToWindows(Image acquireImage) { CognexCamera8 cogCam = CogCamera; if (cogCam != null && cogCam.CogDisplayWindows != null && cogCam.CogDisplayWindows.Count > 0) { ICogImage cogImage = null; if (acquireImage != null) { if (acquireImage is Bitmap) { cogImage = new CogImage24PlanarColor(acquireImage as Bitmap); } else { cogImage = acquireImage as ICogImage; } } Parallel.ForEach <CogDisplay>(cogCam.CogDisplayWindows, currentDisplay => { if (!currentDisplay.IsDisposed) { if (object.ReferenceEquals(currentDisplay.Image, acquireImage)) { System.Diagnostics.Debug.WriteLine("Saved time in AssignImageToWindows. Image already there"); } else { currentDisplay.BeginInvoke(new MethodInvoker(delegate { currentDisplay.Image = cogImage; })); } } }); } }
/// <summary> /// Wait for an expected image /// </summary> /// <param name="acqTicket">Use -1 if Auto-triggering</param> /// <returns></returns> private ICogImage WaitForImage(int acqTicket) { long startTime0 = U.DateTimeNow; ICogImage cogImage = null; int completeTicket, triggerNumber, numPending, numReady = 0; bool busy; do { lock (_lockAcqFifo) { try { _cogAcqFifo.GetFifoState(out numPending, out numReady, out busy); //System.Diagnostics.Debug.WriteLine(string.Format("{0} Acquire numPending={1} numReady={2} busy={3}", Name, numPending, numReady, busy)); if (numReady > 0) { long startTime1 = U.DateTimeNow; cogImage = _cogAcqFifo.CompleteAcquire(acqTicket, out completeTicket, out triggerNumber); long startTime2 = U.DateTimeNow; if (cogImage == null) { U.LogPopup("Unexpected CogImage type = {0}", cogImage.GetType().Name); return(null); } if (cogImage is CogImage8Grey) { CogImage8Grey cogImage8Grey = cogImage as CogImage8Grey; _mConGrey.InputImage = cogImage8Grey; _mConGrey.Run(); _mConGrey.InputImage = null; (cogImage as IDisposable).Dispose(); return(_mConGrey.OutputImage as CogImage8Grey); } else if (cogImage is CogImage24PlanarColor) { CogImage24PlanarColor copyColorImage = null; if (RotateImage == 0) { copyColorImage = new CogImage24PlanarColor((cogImage as CogImage24PlanarColor).ToBitmap()); } else { if (_mOneImageTool.Operators.Count == 0) { switch (RotateImage) { case 90: _ImageRotate.OperationInPixelSpace = CogIPOneImageFlipRotateOperationConstants.Rotate270Deg; break; case -90: case 270: _ImageRotate.OperationInPixelSpace = CogIPOneImageFlipRotateOperationConstants.Rotate270Deg; break; case 180: _ImageRotate.OperationInPixelSpace = CogIPOneImageFlipRotateOperationConstants.Rotate180Deg; break; default: U.LogPopup("Unexpected rotate value ({0}) for camera {1}", RotateImage, Nickname); break; } _mOneImageTool.Operators.Add(_ImageRotate); } _mOneImageTool.InputImage = cogImage; _mOneImageTool.Run(); _mOneImageTool.InputImage = null; copyColorImage = _mOneImageTool.OutputImage as CogImage24PlanarColor; } long startTime3 = U.DateTimeNow; double ms10 = U.TicksToMS(startTime1 - startTime0); double ms21 = U.TicksToMS(startTime2 - startTime1); double ms32 = U.TicksToMS(startTime3 - startTime2); Debug.WriteLine(string.Format("GotImage={0} Complete={1} Rotate={2}", ms10, ms21, ms32)); (cogImage as IDisposable).Dispose(); return(copyColorImage); } else { U.LogPopup("Unexpected CogImage type = {0}", cogImage.GetType().Name); } (cogImage as IDisposable).Dispose(); return(null); } U.SleepWithEvents(10); } catch (Exception ex) { Debug.WriteLine(string.Format("{0} WaitForImage Exception={1}", Name, ex.ToString())); _cogAcqFifo.Flush(); U.LogError(ex, "Error in CompleteAcquire"); System.Threading.Thread.Sleep(100); } } } while (numReady <= 0 && acqTicket >= 0); return(null); }
/// <summary> /// Run the vision job /// </summary> /// <param name="oImage"></param> /// <param name="invertTarget"></param> public override object RunJob(object oImage, bool invertTarget) { ICogImage cogImage = null; if (oImage != null) { if (oImage is Bitmap) { cogImage = new CogImage24PlanarColor(oImage as Bitmap); } else { cogImage = oImage as ICogImage; } if (invertTarget) { cogImage = InvertTarget(cogImage); } } else { cogImage = LastObjJobImage as ICogImage; } if (cogImage == null) { U.LogPopup("Expected to find a Cognex image to process"); return(null); } LastObjJobImage = cogImage; AssignInputImage(cogImage); _autoResetEventWaitToolRun.Reset(); // Run // // This lock help to prevent hang/crash in case a Run is called at the same time // when another job is assigning Image to CogDisplay lock (_sLockRunToolAndDisplayImage) { CogToolGroup.Run(); } // Then we will wait for time out before return. // We are sure that when we return, the result is available. _autoResetEventWaitToolRun.WaitOne(5000, false); // Re-Populate the results PopulateResults(); // Perform any post acquisition operations try { PostAcquisition(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(string.Format("PostAcquisition Exception : {0}", ex.Message)); } return(LastObjJobImage); }
public static void saveImage(CogRecordDisplay image, int index, string result) { var imageBaseDir = AppDomain.CurrentDomain.BaseDirectory + "Image"; if (!Directory.Exists(imageBaseDir)) { Directory.CreateDirectory(imageBaseDir); } var imageDir_today = imageBaseDir + "\\" + DateTime.Now.ToString("MM-dd"); if (!Directory.Exists(imageDir_today)) { Directory.CreateDirectory(imageDir_today); } var imagePath = imageDir_today + "\\" + index + "_" + DateTime.Now.ToString("HHmmss") + ".jpg"; if (result == resultNG) { lock (mu_recentNGImagePath) { recentNGIMagePath = imagePath; } } if (ShouldImageBeSavedBasedOnResult(result)) { try { using (var fileTool = new CogImageFileTool()) { Image img; lock (mu_SaveAsScreenShot) { img = image.CreateContentBitmap(saveAsScreenShot ? CogDisplayContentBitmapConstants.Display : CogDisplayContentBitmapConstants.Image); } var bmp = new Bitmap(img); var cogImage = new CogImage24PlanarColor(bmp); fileTool.InputImage = cogImage; lock (mu_recentNGImagePath) { fileTool.Operator.Open(imagePath, CogImageFileModeConstants.Write); } fileTool.Run(); } } catch { } } //删除过期文件夹 removeOutdatedDirs(imageBaseDir); }
public CogImage24PlanarColor Conv_BitmapTo24Planar(Bitmap bmp) { CogImage24PlanarColor image = new CogImage24PlanarColor(bmp); return(image); }
// 2014.10.12 public void SetInputImage(CogImage24PlanarColor InputImage) { m_aFixtureNPointToNPoint.InputImage = InputImage; }
public void Run(ref ICogImage cogImage, int nFlipRotation) { // 2013.05.03 try { m_nAcqCount++; if (nFlipRotation == 0) { //Acquire(cogDisplay); // 2015.04.08 m_bRan = false; m_cogAcqFifoTool.Run(); // 2015.04.08 WaitRanEvent(); if (m_cogAcqFifoTool.OutputImage != null) { cogImage = m_cogAcqFifoTool.OutputImage; #if !_USE_TRIGGER_ACQ // 2017.11.16 by kdi. m_cogImageTrigger = cogImage; AUtil.PostMessage(m_hMain, _WM_ACQFIFO_COMPLETE, m_nPoint, m_nSection); #endif } #if !_USE_TRIGGER_ACQ else { cogImage = null; //var property = m_cogAcqFifoTool.RunStatus.Exception.GetType().GetProperty("HResult", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); //var value = property.GetValue(m_cogAcqFifoTool.RunStatus.Exception, null); int nHResult = System.Runtime.InteropServices.Marshal.GetHRForException(m_cogAcqFifoTool.RunStatus.Exception); //if (m_cogAcqFifoTool.RunStatus.Message.Contains(_CONST_CAMERA_DISCONNECTION_MESSAGE) == true || // m_cogAcqFifoTool.RunStatus.Message.Contains(_CONST_CAMERA_DISCONNECTION_MESSAGE2) == true || // m_cogAcqFifoTool.RunStatus.Message.Contains(_CONST_CAMERA_DISCONNECTION_MESSAGE3) == true // ) //** to do: // 1. 네트워크 단절 에러와 일반 예외 값이 동일한지 확인 필요 // 동일한 값을 가진다면, 이 조건을 적용할 수 없음 if ((UInt32)nHResult == (UInt32)0x80131600) // Application Exception { // ip address System.Net.IPAddress ipaddress = System.Net.IPAddress.Parse(m_cogAcqFifoTool.Operator.FrameGrabber.OwnedGigEAccess.CurrentIPAddress); int intAddress = BitConverter.ToInt32(ipaddress.GetAddressBytes(), 0); //AUtil.PostMessage(m_hMain, ASDef._WM_CAMERA_IS_DISCONNECTED, m_nPoint, intAddress); AUtil.PostMessage(m_hMain, clsCamera._WM_CAMERA_IS_DISCONNECTED, m_nPoint, intAddress); } string strMsg = ""; strMsg = string.Format("AAcqFifo.Run: Error. {0}", m_cogAcqFifoTool.RunStatus.Message); m_strError = strMsg; AVisionProBuild.WriteLogFile(strMsg, ".AcqFifo.err.txt"); } #endif return; } // 2015.04.08 m_bRan = false; //DateTime Now = DateTime.Now; m_cogAcqFifoTool.Run(); // 2015.04.08 WaitRanEvent(); //TimeSpan span = DateTime.Now - Now; if (m_cogAcqFifoTool.OutputImage != null) { // 2011.07.29 CogIPOneImageFlipRotate cogIPOneImageFlipRotate = new CogIPOneImageFlipRotate(); cogIPOneImageFlipRotate.OperationInPixelSpace = (CogIPOneImageFlipRotateOperationConstants)nFlipRotation; // 2012.04.25 ICogImage cogImageFR = cogIPOneImageFlipRotate.Execute(m_cogAcqFifoTool.OutputImage, CogRegionModeConstants.PixelAlignedBoundingBox, null); Bitmap Bmp = cogImageFR.ToBitmap(); if ((Bmp.Flags & (16 | 32 | 64 | 128 | 256)) != 0) { cogImage = new CogImage24PlanarColor(Bmp); } else { cogImage = new CogImage8Grey(Bmp); } #if !_USE_TRIGGER_ACQ // 2017.11.16 by kdi. m_cogImageTrigger = cogImage; AUtil.PostMessage(m_hMain, _WM_ACQFIFO_COMPLETE, m_nPoint, m_nSection); #endif // 2012.06.06 if (Bmp != null) { Bmp.Dispose(); Bmp = null; } } #if !_USE_TRIGGER_ACQ else { cogImage = null; int nHResult = System.Runtime.InteropServices.Marshal.GetHRForException(m_cogAcqFifoTool.RunStatus.Exception); //if (m_cogAcqFifoTool.RunStatus.Message.Contains(_CONST_CAMERA_DISCONNECTION_MESSAGE) == true || // m_cogAcqFifoTool.RunStatus.Message.Contains(_CONST_CAMERA_DISCONNECTION_MESSAGE2) == true || // m_cogAcqFifoTool.RunStatus.Message.Contains(_CONST_CAMERA_DISCONNECTION_MESSAGE3) == true // ) //** to do: // 1. 네트워크 단절 에러와 일반 예외 값이 동일한지 확인 필요 // 동일한 값을 가진다면, 이 조건을 적용할 수 없음 if ((UInt32)nHResult == (UInt32)0x80131600) // Application Exception { // 2018.04.09 //AUtil.PostMessage(m_hMain, ASDef._WM_CAMERA_IS_DISCONNECTED, m_nPoint, 0); AUtil.PostMessage(m_hMain, clsCamera._WM_CAMERA_IS_DISCONNECTED, m_nPoint, 0); } string strMsg = ""; strMsg = string.Format("AAcqFifo.Run: Error. {0}", m_cogAcqFifoTool.RunStatus.Message); m_strError = strMsg; AVisionProBuild.WriteLogFile(strMsg, ".AcqFifo.err.txt"); } #endif /* * ICogImage cogImageFR = cogIPOneImageFlipRotate.Execute(m_cogAcqFifoTool.OutputImage, CogRegionModeConstants.PixelAlignedBoundingBox, null); * * lock (m_cogImageFile) * { * string strFName = "c:\\" + AVisionProBuild.MakeName("Flip", DateTime.Now) + ".bmp"; * m_cogImageFile.Open(strFName, CogImageFileModeConstants.Write); * m_cogImageFile.Append(cogImageFR); * m_cogImageFile.Close(); * m_cogImageFile.Open(strFName, CogImageFileModeConstants.Read); * cogImage = m_cogImageFile[0]; * m_cogImageFile.Close(); * File.Delete(strFName); * } */ } catch (CogException ex) { cogImage = null; m_cogImageTrigger = cogImage; #if !_USE_TRIGGER_ACQ if (ex.Message.Contains(_CONST_CAMERA_DISCONNECTION_MESSAGE) == true || ex.Message.Contains(_CONST_CAMERA_DISCONNECTION_MESSAGE2) == true) { // 2018.04.09 //AUtil.PostMessage(m_hMain, ASDef._WM_CAMERA_IS_DISCONNECTED, m_nPoint, 0); AUtil.PostMessage(m_hMain, clsCamera._WM_CAMERA_IS_DISCONNECTED, m_nPoint, 0); } string strMsg = ""; strMsg = string.Format("AAcqFifo.Run: CogException. {0}", ex.Message); m_strError = strMsg; AVisionProBuild.WriteLogFile(strMsg, ".AcqFifo.err.txt"); #endif } catch { cogImage = null; } }
private void 研究图像缝合ToolStripMenuItem_Click(object sender, EventArgs e) { CogImageFile imagefile_0 = new CogImageFile(); imagefile_0.Open(Application.StartupPath + "/../../../street/street_0.bmp", CogImageFileModeConstants.Read); CogImage24PlanarColor image_24_0 = new CogImage24PlanarColor(); image_24_0 = (CogImage24PlanarColor)imagefile_0[0]; CogImage8Grey imageGray8_0 = CogImageConvert.GetIntensityImage(image_24_0, 0, 0, image_24_0.Width, image_24_0.Height); CogImageFile imagefile_1 = new CogImageFile(); imagefile_1.Open(Application.StartupPath + "/../../../street/street_1.bmp", CogImageFileModeConstants.Read); CogImage24PlanarColor image_24_1 = new CogImage24PlanarColor(); image_24_1 = (CogImage24PlanarColor)imagefile_1[0]; CogImage8Grey imageGray8_1 = CogImageConvert.GetIntensityImage(image_24_1, 0, 0, image_24_1.Width, image_24_1.Height); CogImageFile imagefile_2 = new CogImageFile(); imagefile_2.Open(Application.StartupPath + "/../../../street/street_2.bmp", CogImageFileModeConstants.Read); CogImage24PlanarColor image_24_2 = new CogImage24PlanarColor(); image_24_2 = (CogImage24PlanarColor)imagefile_2[0]; CogImage8Grey imageGray8_2 = CogImageConvert.GetIntensityImage(image_24_2, 0, 0, image_24_2.Width, image_24_2.Height); // 灰度图像 //CogImage8Grey image8 = (CogImage8Grey)m_tb.GetScriptTerminalData("imageInput"); CogImage8Grey image8 = (CogImage8Grey)m_tb.GetScriptTerminalData("OutputImage"); image8 = imageGray8_0.Copy(); CogToolCollection tbTc = m_tb.Tools; //tbTc. if (false) { image8 = (CogImage8Grey)((CogImageFileTool)tbTc["CogImageFileTool1"]).OutputImage; } CogImage8Grey image_002 = new CogImage8Grey(); //image_002 = image8; image_002 = image8.Copy(); CogImage8Grey image_003 = image8.Copy(); // 将各个图像进行缝合组成一个完整的大图 // 灰度图像 //CogIPOneImageTool imageCtrl_02 = (CogIPOneImageTool)mToolBlock.Tools["CogIPOneImageTool2"]; CogRectangle rt = new CogRectangle(); rt.SetCenterWidthHeight(0, 0, image8.Width * 3, image8.Height * 3); CogImage8Grey m_gray = rt.CreateRLE(128, 0).CreateImage8Grey(); //imageCtrl_02.InputImage = m_gray; // CogImage8Grey inputImage = ( CogImage8Grey ) image8; // tb.SetScriptTerminalData("Input", inputImage); CogImageStitch stt = new CogImageStitch(); CogTransform2DLinear rootFromBlending = new CogTransform2DLinear(); //缩放,旋转,平移 rootFromBlending.SetScalingsRotationsTranslation(1, 1, 0, 0, 0, 0); // rootFromBlending.SetScalingsRotationTranslation(); stt.AllocateBlendingBuffer(image8.Width * 3, image8.Height * 3, rootFromBlending); //stt.BlendImageIntoBuffer(image8, m_gray); stt.BlendImageIntoBuffer(imageGray8_0, imageGray8_0, m_gray, 0, 0); stt.BlendImageIntoBuffer(imageGray8_1, imageGray8_1, m_gray, image8.Width, image8.Height); stt.BlendImageIntoBuffer(imageGray8_2, imageGray8_2, m_gray, image8.Width * 2, image8.Height * 2); stt.FillDestinationImageFromBuffer(m_gray); // BlendImageIntoBuffer( CogImage8Grey, CogImage8Grey, CogImage8Grey, Int32, Int32 ) // CogCopyRegionTool ccft = new CogCopyRegionTool(); //switch ( true ) //{ //case : // break; //} cogRecordDisplay1.Image = m_gray; cogRecordDisplay1.Fit(); }