//비동기 그랩 콜백 메서드 private MIL_INT OnGrab(MIL_INT eventProp, MIL_ID eventId, IntPtr userData) { try { //지금 콜백에 들어온 Image Buffer ID 가져오기 MIL_ID currentBuf = MIL.M_NULL; MIL.MdigGetHookInfo(eventId, eventProp + MIL.M_BUFFER_ID, ref currentBuf); //버퍼 byte[] 로 복사 byte[] rawImage = new byte[400 * 400]; MIL.MbufGet2d(currentBuf, 0, 0, 400, 400, rawImage); //BitmapSource로 변환 var img = BitmapSource.Create(400, 400, 96d, 96d, PixelFormats.Indexed8, BitmapPalettes.Gray256, rawImage, 400); if (img.CanFreeze) { img.Freeze(); } DisplayBitmapSource = img; return(0); } catch (Exception err) { return(-1); } }
// Optional decoding end function called every time a buffer is finished being decompressed. // ------------------------------------------------------------------------------------------ static MIL_INT FrameDecodingEndFunction(MIL_INT HookType, MIL_ID HookId, IntPtr HookDataPtr) { // this is how to check if the user data is null, the IntPtr class // contains a member, Zero, which exists solely for this purpose if (!IntPtr.Zero.Equals(HookDataPtr)) { // get the handle to the FrameEndHookDataStruct object back from the IntPtr GCHandle hUserData = GCHandle.FromIntPtr(HookDataPtr); // get a reference to the FrameEndHookDataStruct object DecodingFrameEndHookDataStruct UserHookDataPtr = hUserData.Target as DecodingFrameEndHookDataStruct; // Frame end hook post processing. if (HookType == MIL.M_FRAME_END) { MIL_ID DecompressedBufferId = MIL.M_NULL; // Increment a encoded frame counter. UserHookDataPtr.DecodedImageCount++; // Retrieve the MIL_ID of the encoded buffer. MIL.MseqGetHookInfo(HookId, MIL.M_MODIFIED_BUFFER + MIL.M_BUFFER_ID, ref DecompressedBufferId); // ----------------------------------------------------------------------------------------------- // Here you can do any action with the decoded buffer. // ----------------------------------------------------------------------------------------------- MIL.MbufCopy(DecompressedBufferId, UserHookDataPtr.MilImageDisp); } } return(0); }
private static MIL_INT HookHandler(MIL_INT HookType, MIL_ID EventId, IntPtr UserDataPtr) { // this is how to check if the user data is null, the IntPtr class // contains a member, Zero, which exists solely for this purpose if (!IntPtr.Zero.Equals(UserDataPtr)) { // get the handle to the DigHookUserData object back from the IntPtr GCHandle hUserData = GCHandle.FromIntPtr(UserDataPtr); // get a reference to the DigHookUserData object STestParameters DataStructure = hUserData.Target as STestParameters; // Check that the modified graphic is the rectangular region. MIL_INT ModifiedGraphicLabel = 0; MIL.MgraGetHookInfo(EventId, MIL.M_GRAPHIC_LABEL_VALUE, ref ModifiedGraphicLabel); if (ModifiedGraphicLabel == DataStructure.RegionLabel) { // Count objects and draw the corresponding annotations. CountObjects(DataStructure.MilDisplay, DataStructure.MilGraphicsList, DataStructure.MilGraphicsContext, DataStructure.MilBinImage, DataStructure.MilBlobFeatureList, DataStructure.MilBlobResult); } } return(MIL.M_NULL); }
static MIL_INT ProcessingFunction(MIL_INT HookType, MIL_ID HookId, IntPtr HookDataPtr) { MIL_ID ModifiedBufferId = MIL.M_NULL; // this is how to check if the user data is null, the IntPtr class // contains a member, Zero, which exists solely for this purpose if (!IntPtr.Zero.Equals(HookDataPtr)) { // get the handle to the DigHookUserData object back from the IntPtr GCHandle hUserData = GCHandle.FromIntPtr(HookDataPtr); // get a reference to the DigHookUserData object HookDataStruct UserData = hUserData.Target as HookDataStruct; // Retrieve the MIL_ID of the grabbed buffer. MIL.MdigGetHookInfo(HookId, MIL.M_MODIFIED_BUFFER + MIL.M_BUFFER_ID, ref ModifiedBufferId); // Increment the frame counter. UserData.ProcessedImageCount++; // Print and draw the frame count (remove to reduce CPU usage). Console.Write("Processing frame #{0}.\r", UserData.ProcessedImageCount); MIL.MgraText(MIL.M_DEFAULT, ModifiedBufferId, STRING_POS_X, STRING_POS_Y, String.Format("{0}", UserData.ProcessedImageCount)); // Execute the processing and update the display. MIL.MimArith(ModifiedBufferId, MIL.M_NULL, UserData.MilImageDisp, MIL.M_NOT); } return(0); }
public int Init(int camIndex = 0) { try { //1.初始化默认的appID和sysID MIL.MappAllocDefault(MIL.M_DEFAULT, ref appID, ref sysID, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL); //2.初始化digID,用于CameraLink控制相机 MIL_INT digNum = MIL.MsysInquire(sysID, MIL.M_DIGITIZER_NUM, MIL.M_NULL);//获取Cameralink数量 if (digNum < camIndex) { throw new Exception($"Camera {camIndex} not exits! Please check !"); } MIL.MdigAlloc(sysID, camIndex, dfc, MIL.M_DEFAULT, ref digID);//从DFC文件中加载相机配置,第三个参数就是DFC文件的路径 //3.相机配置保存在DFC文件中,现在可以从digID中获取到相机的配置 MIL.MdigInquire(digID, MIL.M_SIZE_X, ref width); //获取相机宽度 MIL.MdigInquire(digID, MIL.M_SIZE_Y, ref height); //获取相机高度 //MIL.MdigInquire(digID, MIL.M_MODEL_IMAGE_ATTRIBUTE, ref imgAttribute);//用于下一步分配相机帧buffer的参数 imgAttribute = MIL.M_GRAB | MIL.M_IMAGE | MIL.M_DISP | MIL.M_PROC; //4.分配buffer来存储相机帧数据 MIL.MbufAlloc2d(sysID, width, height, 8 + MIL.M_UNSIGNED, imgAttribute, ref imgID); //4.初始化时,开启控制串口 //comport.Open(); } catch (Exception e) { throw e; } return(0); }
/// <summary> /// SetEdgeFindParameter /// Edge검출 영역 설정 /// </summary> /// <param name="mPos"></param> /// <param name="dWidth"></param> /// <param name="dHeight"></param> /// <param name="dAng"></param> /// <returns></returns> public int SetEdgeFindParameter(Point mPos, double dWidth, double dHeight, double dAng) { MIL_INT MARKER_TYPE = MIL.M_EDGE; double FIRST_EDGE_POLARITY = (double)MIL.M_POSITIVE; double SECOND_EDGE_POLARITY = (double)MIL.M_DEFAULT; double BOX_CENTER_POS_X = (double)mPos.X; double BOX_CENTER_POS_Y = (double)mPos.Y; double BOX_SIZE_X = dWidth; double BOX_SIZE_Y = dHeight; double BOX_ANGLE = dAng; double SUB_REGIONS_NUMBER = (double)MIL.M_DEFAULT; double NB_MARKERS = (double)MIL.M_ALL; double EDGEVALUE_MIN = (double)MIL.M_DEFAULT; // Edge Find m_EdgeMaker = MIL.MmeasAllocMarker(m_MilSystem, MARKER_TYPE, MIL.M_DEFAULT, MIL.M_NULL); MIL.MmeasSetMarker(m_EdgeMaker, MIL.M_POLARITY, FIRST_EDGE_POLARITY, SECOND_EDGE_POLARITY); MIL.MmeasSetMarker(m_EdgeMaker, MIL.M_BOX_CENTER, BOX_CENTER_POS_X, BOX_CENTER_POS_Y); MIL.MmeasSetMarker(m_EdgeMaker, MIL.M_BOX_SIZE, BOX_SIZE_X, BOX_SIZE_Y); MIL.MmeasSetMarker(m_EdgeMaker, MIL.M_BOX_ANGLE, BOX_ANGLE, MIL.M_NULL); MIL.MmeasSetMarker(m_EdgeMaker, MIL.M_SUB_REGIONS_NUMBER, SUB_REGIONS_NUMBER, MIL.M_NULL); MIL.MmeasSetMarker(m_EdgeMaker, MIL.M_NUMBER, NB_MARKERS, MIL.M_NULL); MIL.MmeasSetMarker(m_EdgeMaker, MIL.M_EDGEVALUE_MIN, EDGEVALUE_MIN, MIL.M_NULL); return(SUCCESS); }
private void CalibrateFromDigitizer(object obj) { MIL.MbufClear(_buffer, MIL.M_COLOR_BLUE); if (IsGrabbing) { ToggleGrab(null); } MIL.MdigGrab(_digitizer, _buffer); try { MIL.McalGrid(_calibration, _buffer, 0, 0, 0, 16, 16, 2, 2, MIL.M_FULL_CALIBRATION, MIL.M_CIRCLE_GRID); } catch (MILException ex) { MessageBox.Show(ex.ToString()); return; } MIL_INT result = MIL.M_NULL; MIL.McalInquire(_calibration, MIL.M_CALIBRATION_STATUS + MIL.M_TYPE_MIL_INT, ref result); if (result == MIL.M_CALIBRATED) { // Associate buffer with new calibration // MIL.McalAssociate(_calibration, _calibration, MIL.M_DEFAULT); // Don't need to suppy graphics context to clear graphics MIL.MgraClear(MIL.M_DEFAULT, _calibrationGraphics); // Draw Pixel Coord Sys MIL.MgraControl(_calibrationGraphicsContext, MIL.M_COLOR, MIL.M_COLOR_RED); MIL.McalDraw(_calibrationGraphicsContext, MIL.M_NULL, _calibrationGraphics, MIL.M_DRAW_PIXEL_COORDINATE_SYSTEM + MIL.M_DRAW_AXES, MIL.M_DEFAULT, MIL.M_DEFAULT); // Draw World Coord Sys MIL.MgraControl(_calibrationGraphicsContext, MIL.M_COLOR, MIL.M_COLOR_GREEN); MIL.McalDraw(_calibrationGraphicsContext, _calibration, _calibrationGraphics, MIL.M_DRAW_ABSOLUTE_COORDINATE_SYSTEM + MIL.M_DRAW_AXES, MIL.M_DEFAULT, MIL.M_DEFAULT); MIL_INT angle = 42; // MIL.McalRelativeOrigin(_calibration, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, 45, MIL.M_DEFAULT); // Move and draw Relative Coord Sys //MIL.McalFixture(_calibration, MIL.M_NULL, MIL.M_MOVE_RELATIVE, MIL.M_POINT_AND_DIRECTION_POINT, MIL.M_DEFAULT, 15, 15, 20, 15); MIL.MgraControl(_calibrationGraphicsContext, MIL.M_COLOR, MIL.M_COLOR_BLUE); MIL.McalDraw(_calibrationGraphicsContext, _calibration, _calibrationGraphics, MIL.M_DRAW_RELATIVE_COORDINATE_SYSTEM + MIL.M_DRAW_AXES, MIL.M_DEFAULT, MIL.M_DEFAULT); // Associate calibration graphics to display MIL.MdispControl(_display, MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, _calibrationGraphics); // MIL_INT angle = MIL.M_NULL; //MIL.McalInquire(_calibration, MIL.M_RELATIVE_ORIGIN_ANGLE, ref angle); IsCalibrated = true; } // Associate display and buffer MIL.MdispSelect(_display, _buffer); }
// Main function. static void Main(string[] args) { MIL_ID MilApplication = MIL.M_NULL; // Application identifier. MIL_ID MilSystem = MIL.M_NULL; // System identifier. MIL_ID MilDisplay = MIL.M_NULL; // Display identifier. MIL_ID MilImage = MIL.M_NULL; // Image buffer identifier. MIL_ID MilOverlayImage = MIL.M_NULL; // Overlay buffer identifier. MIL_ID HistResult = MIL.M_NULL; // Histogram buffer identifier. MIL_INT[] HistValues = new MIL_INT[HIST_NUM_INTENSITIES]; // Histogram values. double[] XStart = new double[HIST_NUM_INTENSITIES]; double[] YStart = new double[HIST_NUM_INTENSITIES]; double[] XEnd = new double[HIST_NUM_INTENSITIES]; double[] YEnd = new double[HIST_NUM_INTENSITIES]; double AnnotationColor = MIL.M_COLOR_RED; // Allocate the default system and image buffer. MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL); // Restore source image into an automatically allocated image buffer. MIL.MbufRestore(IMAGE_FILE, MilSystem, ref MilImage); // Display the image buffer and prepare for overlay annotations. MIL.MdispSelect(MilDisplay, MilImage); MIL.MdispControl(MilDisplay, MIL.M_OVERLAY, MIL.M_ENABLE); MIL.MdispInquire(MilDisplay, MIL.M_OVERLAY_ID, ref MilOverlayImage); // Allocate a histogram result buffer. MIL.MimAllocResult(MilSystem, HIST_NUM_INTENSITIES, MIL.M_HIST_LIST, ref HistResult); // Calculate the histogram. MIL.MimHistogram(MilImage, HistResult); // Get the results. MIL.MimGetResult(HistResult, MIL.M_VALUE, HistValues); // Draw the histogram in the overlay. MIL.MgraColor(MIL.M_DEFAULT, AnnotationColor); for (int i = 0; i < HIST_NUM_INTENSITIES; i++) { XStart[i] = i + HIST_X_POSITION + 1; YStart[i] = HIST_Y_POSITION; XEnd[i] = i + HIST_X_POSITION + 1; YEnd[i] = HIST_Y_POSITION - (HistValues[i] / HIST_SCALE_FACTOR); } MIL.MgraLines(MIL.M_DEFAULT, MilOverlayImage, HIST_NUM_INTENSITIES, XStart, YStart, XEnd, YEnd, MIL.M_DEFAULT); // Print a message. Console.Write("\nHISTOGRAM:\n"); Console.Write("----------\n\n"); Console.Write("The histogram of the image was calculated and drawn.\n"); Console.Write("Press <Enter> to end.\n\n"); Console.ReadKey(); // Free all allocations. MIL.MimFree(HistResult); MIL.MbufFree(MilImage); MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL); }
private MIL_INT FrameEvent(MIL_INT HookType, MIL_ID EventId, IntPtr UserDataPtr) { if (grabResetEvent != null) { grabResetEvent.Set(); } return((MIL_INT)1); }
public RecordSettings() { Type = "Vídeo"; outputFormat = MIL.M_FILE_FORMAT_MP4; fps = 0; value_postTrigger = 0; UnitTimeStop = "Segundos"; Root = @"C:\Recording\Records"; }
// Master MIL Function definition. // ------------------------------- static MIL_INT AddConstant(MIL_ID SrcImageId, MIL_ID DstImageId, MIL_INT ConstantToAdd) { MIL_ID Func = MIL.M_NULL; MIL_INT SlaveReturnValue = 0; // Allocate a MIL function context that will be used to call a target // Slave function locally on the Host to do the processing. MFUNCFCTPTR SlaveAddConstantDelegate = new MFUNCFCTPTR(SlaveAddConstant); MIL.MfuncAlloc("AddConstant", FUNCTION_NB_PARAM, SlaveAddConstantDelegate, MIL.M_NULL, MIL.M_NULL, MIL.M_USER_MODULE_1 + FUNCTION_OPCODE_ADD_CONSTANT, MIL.M_LOCAL + MIL.M_SYNCHRONOUS_FUNCTION, ref Func); // Register the parameters. MIL.MfuncParamMilId(Func, 1, SrcImageId, MIL.M_IMAGE, MIL.M_IN); MIL.MfuncParamMilId(Func, 2, DstImageId, MIL.M_IMAGE, MIL.M_OUT); MIL.MfuncParamMilInt(Func, 3, ConstantToAdd); // To pass a pointer to MIL, we need to use a reference type such as an array // to be able to pin the object and prevent the garbage collector from moving the object. MIL_INT[] slaveReturnValueArray = new MIL_INT[1] { -1 }; GCHandle slaveReturnValueArrayHandle = GCHandle.Alloc(slaveReturnValueArray, GCHandleType.Pinned); try { // Get the address of the pinned object, for an array, the address is pointing to the first element. IntPtr slaveReturnValuePtr = slaveReturnValueArrayHandle.AddrOfPinnedObject(); MIL.MfuncParamDataPointer(Func, 4, slaveReturnValuePtr, MIL_INT.Size * 2, MIL.M_OUT); // Call the target Slave function. MIL.MfuncCall(Func); SlaveReturnValue = slaveReturnValueArray[0]; // Make sure that the delegate survives garbage collection until the slave function is executed. GC.KeepAlive(SlaveAddConstantDelegate); } finally { // Free the allocated GCHandle to allow the array object to be garbage collected. if (slaveReturnValueArrayHandle.IsAllocated) { slaveReturnValueArrayHandle.Free(); } } // Free the MIL function context. MIL.MfuncFree(Func); return(SlaveReturnValue); }
static void SimulateGrabFromCamera(MIL_ID SourceImage, MIL_ID FocusImage, MIL_INT Iteration, MIL_ID AnnotationDisplay) { int NbSmoothNeeded = 0; // Number of smooths needed. MIL_INT BufType = 0; // Buffer type. MIL_INT BufSizeX = 0; // Buffer size X. MIL_INT BufSizeY = 0; // Buffer size Y. int Smooth = 0; // Smooth index. MIL_ID TempBuffer = MIL.M_NULL; // Temporary buffer. MIL_ID SourceOwnerSystem = MIL.M_NULL; // Owner system of the source buffer. // Compute number of smooths needed to simulate focus. NbSmoothNeeded = (int)Math.Abs(Iteration - FOCUS_BEST_POSITION); // Buffer inquires. BufType = MIL.MbufInquire(FocusImage, MIL.M_TYPE, MIL.M_NULL); BufSizeX = MIL.MbufInquire(FocusImage, MIL.M_SIZE_X, MIL.M_NULL); BufSizeY = MIL.MbufInquire(FocusImage, MIL.M_SIZE_Y, MIL.M_NULL); if (NbSmoothNeeded == 0) { // Directly copy image source to destination. MIL.MbufCopy(SourceImage, FocusImage); } else if (NbSmoothNeeded == 1) { // Directly convolve image from source to destination. MIL.MimConvolve(SourceImage, FocusImage, MIL.M_SMOOTH); } else { SourceOwnerSystem = (MIL_ID)MIL.MbufInquire(SourceImage, MIL.M_OWNER_SYSTEM, MIL.M_NULL); // Allocate temporary buffer. MIL.MbufAlloc2d(SourceOwnerSystem, BufSizeX, BufSizeY, BufType, MIL.M_IMAGE + MIL.M_PROC, ref TempBuffer); // Perform first smooth. MIL.MimConvolve(SourceImage, TempBuffer, MIL.M_SMOOTH); // Perform smooths. for (Smooth = 1; Smooth < NbSmoothNeeded - 1; Smooth++) { MIL.MimConvolve(TempBuffer, TempBuffer, MIL.M_SMOOTH); } // Perform last smooth. MIL.MimConvolve(TempBuffer, FocusImage, MIL.M_SMOOTH); // Free temporary buffer. MIL.MbufFree(TempBuffer); } // Draw position cursor. DrawCursor(AnnotationDisplay, Iteration); }
private void mil(IntPtr UserWindowHandle) { MIL_ID MilDigitizer = MIL.M_NULL; MIL_INT BufSizeX = DEFAULT_IMAGE_SIZE_X; MIL_INT BufSizeY = DEFAULT_IMAGE_SIZE_Y; MIL_INT bufsx = DEFAULT_IMAGE_SIZE_X; MIL_INT bufsy = DEFAULT_IMAGE_SIZE_Y; MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, ref MilDigitizer, ref MilImage); MIL.MdispSelectWindow(MilDisplay, MilImage, UserWindowHandle); //MIL.MdispSelect(MilDisplay, MilImage); ////MIL.MdigAlloc(MilSystem, MIL.M_DEFAULT, "M_DEFAULT", MIL.M_DEFAULT, ref MilDigitizer); MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_X, ref BufSizeX); MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_Y, ref BufSizeY); //MIL.MdispControl(MilDisplay, MIL.M_SIZE_BIT, 8); // Resize the display window //if ((BufSizeX > DEFAULT_IMAGE_SIZE_X) || (BufSizeY > DEFAULT_IMAGE_SIZE_Y)) //{ // FromHandle(UserWindowHandle).Size = new Size((int)BufSizeX, (int)BufSizeY); //} // MIL.MdispZoom(MilDisplay, -2.0, -2.0); //MIL.MdispControl(MilDisplay, MIL.M_VIEW_BIT_SHIFT, 8); MIL.MdispControl(MilDisplay, MIL.M_SCALE_DISPLAY, MIL.M_ENABLE); MIL.MdispControl(MilDisplay, MIL.M_SCALE_DISPLAY, MIL.M_ENABLE); ////MIL.MdispInquire(MilDisplay, MIL.M_SIZE_X, ref DEFAULT_IMAGE_SIZE_X); //MIL_INT imageWidth, imageHeight; //imageWidth = MIL.MbufInquire(MilImage, MIL.M_SIZE_X, MIL.M_NULL); //imageHeight = MIL.MbufInquire(MilImage, MIL.M_SIZE_Y, MIL.M_NULL); //MIL.MbufInquire(MilImage, MIL.M_SIZE_X, bufsx); //MIL.MbufInquire(MilImage, MIL.M_SIZE_X, bufsy); if (MilDigitizer != MIL.M_NULL) { MIL.MdigGrabContinuous(MilDigitizer, MilImage); //MessageBox.Show("Image Acquisition", "Acquisition"); //MIL.MdigHalt(MilDigitizer); } //MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, MilImage); }
/// <summary> /// Edge를 찾음 /// dPosX,dPosY에 검출된 값을 보내줌 /// </summary> /// <param name="iCam"></param> /// <param name="dPosX"></param> /// <param name="dPosY"></param> /// <returns></returns> public int FindEdge(int iCam, ref CEdgeData pEdgeData) { // Edge 검출 설정이 되어 있는지를 확인함. if (m_EdgeMaker == MIL.M_NULL) { return(ERR_VISION_ERROR); } // 검출할 영상 Image를 가져온다. MIL_ID m_MilImage = m_pDisplay[iCam].GetImage(); // 결과를 Display할 Overlay를 가져온다. MIL_ID m_DisplayGraph = m_pDisplay[iCam].GetViewGraph(); // Find the marker and compute all applicable measurements. // Edge검출 명령 실행 MIL.MmeasFindMarker(MIL.M_DEFAULT, m_MilImage, m_EdgeMaker, MIL.M_DEFAULT); // Overlay Clear MIL.MgraClear(MIL.M_DEFAULT, m_DisplayGraph); // Edge 검출 영역을 Overlay에 표시함 MIL.MgraColor(MIL.M_DEFAULT, MIL.M_COLOR_GREEN); MIL.MmeasDraw(MIL.M_DEFAULT, m_EdgeMaker, m_DisplayGraph, MIL.M_DRAW_SEARCH_REGION, MIL.M_DEFAULT, MIL.M_RESULT); // Edge 개수 확인 MIL_INT FindEdgeNum = 0; MIL.MmeasGetResult(m_EdgeMaker, MIL.M_NUMBER + MIL.M_TYPE_MIL_INT, ref FindEdgeNum, MIL.M_NULL); // Edge 검출 되었을 경우 if (FindEdgeNum >= 1) { pEdgeData.m_bSuccess = true; pEdgeData.m_iEdgeNum = FindEdgeNum; pEdgeData.m_dPosX = new double[FindEdgeNum]; pEdgeData.m_dPosY = new double[FindEdgeNum]; // 검출된 Edge를 Overlay에 표시함. MIL.MgraColor(MIL.M_DEFAULT, MIL.M_COLOR_RED); MIL.MmeasDraw(MIL.M_DEFAULT, m_EdgeMaker, m_DisplayGraph, MIL.M_DRAW_POSITION, MIL.M_DEFAULT, MIL.M_RESULT); MIL.MmeasGetResult(m_EdgeMaker, MIL.M_POSITION + MIL.M_EDGE_FIRST, ref pEdgeData.m_dPosX[0], ref pEdgeData.m_dPosY[0]); return(SUCCESS); } else { pEdgeData.m_bSuccess = false; //pEdgeData.m_dPosX = 0.0; //pEdgeData.m_dPosY = 0.0; pEdgeData.m_iEdgeNum = 0; return(GenerateErrorCode(ERR_VISION_EDGE_SEARCH_FAIL)); } }
//***************************************************************************** // Benchmark function. //***************************************************************************** private static void Benchmark(ref PROC_PARAM ProcParamPtr, ref double Time, ref double FramesPerSecond) { MIL_INT EstimatedNbLoop = DEFAULT_NB_LOOP; double StartTime = 0.0; double EndTime = 0.0; double MinTime = 0; MIL_INT n; // Wait for the completion of all functions in this thread. MIL.MthrWait(MIL.M_DEFAULT, MIL.M_THREAD_WAIT, MIL.M_NULL); // Call the processing once before benchmarking for a more accurate time. // This compensates for Dll load time, etc. MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, ref StartTime); ProcessingExecute(ref ProcParamPtr); MIL.MthrWait(MIL.M_DEFAULT, MIL.M_THREAD_WAIT, MIL.M_NULL); MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, ref EndTime); MinTime = EndTime - StartTime; // Estimate the number of loops required to benchmark the processing for // the specified minimum time. for (n = 0; n < ESTIMATION_NB_LOOP; n++) { MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, ref StartTime); ProcessingExecute(ref ProcParamPtr); MIL.MthrWait(MIL.M_DEFAULT, MIL.M_THREAD_WAIT, MIL.M_NULL); MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, ref EndTime); Time = EndTime - StartTime; MinTime = (Time < MinTime) ? Time : MinTime; } if (MinTime > 0) { EstimatedNbLoop = (MIL_INT)(MINIMUM_BENCHMARK_TIME / MinTime) + 1; } // Benchmark the processing according to the estimated number of loops. MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, ref StartTime); for (n = 0; n < EstimatedNbLoop; n++) { ProcessingExecute(ref ProcParamPtr); } MIL.MthrWait(MIL.M_DEFAULT, MIL.M_THREAD_WAIT, MIL.M_NULL); MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, ref EndTime); Time = EndTime - StartTime; FramesPerSecond = EstimatedNbLoop / Time; Time = Time * 1000 / EstimatedNbLoop; }
private static void CountObjects(MIL_ID MilDisplay, MIL_ID MilGraphicsList, MIL_ID MilGraphicsContext, MIL_ID MilBinImage, MIL_ID MilBlobFeatureList, MIL_ID MilBlobResult) { MIL_INT NumberOfBlobs = 0; MIL_INT NumberOfPrimitives = 0; MIL_INT Index; string TextLabel; // Disable the display update for better performance. MIL.MdispControl(MilDisplay, MIL.M_UPDATE, MIL.M_DISABLE); // Remove all elements from the graphics list, except the rectangle // region primitive at index 0. MIL.MgraInquireList(MilGraphicsList, MIL.M_LIST, MIL.M_DEFAULT, MIL.M_NUMBER_OF_GRAPHICS, ref NumberOfPrimitives); for (Index = NumberOfPrimitives - 1; Index > 0; Index--) { MIL.MgraControlList(MilGraphicsList, MIL.M_GRAPHIC_INDEX(Index), MIL.M_DEFAULT, MIL.M_DELETE, MIL.M_DEFAULT); } // Set the input region. The blob analysis will be done // from the (filled) interactive rectangle. MIL.MbufSetRegion(MilBinImage, MilGraphicsList, MIL.M_DEFAULT, MIL.M_RASTERIZE + MIL.M_FILL_REGION, MIL.M_DEFAULT); // Calculate the blobs and their features. MIL.MblobCalculate(MilBinImage, MIL.M_NULL, MilBlobFeatureList, MilBlobResult); // Get the total number of blobs. MIL.MblobGetNumber(MilBlobResult, ref NumberOfBlobs); // Set the input units to display unit for the count annotations. MIL.MgraControl(MilGraphicsContext, MIL.M_INPUT_UNITS, MIL.M_DISPLAY); TextLabel = string.Format(" Number of blobs found: {0:00} ", NumberOfBlobs); MIL.MgraColor(MilGraphicsContext, MIL.M_COLOR_WHITE); MIL.MgraText(MilGraphicsContext, MilGraphicsList, 10, 10, TextLabel); // Restore the input units to pixel units for result annotations. MIL.MgraControl(MilGraphicsContext, MIL.M_INPUT_UNITS, MIL.M_PIXEL); // Draw blob center of gravity annotation. MIL.MgraColor(MilGraphicsContext, MIL.M_COLOR_RED); MIL.MblobDraw(MilGraphicsContext, MilBlobResult, MilGraphicsList, MIL.M_DRAW_CENTER_OF_GRAVITY, MIL.M_INCLUDED_BLOBS, MIL.M_DEFAULT); // Draw blob bounding box annotations. MIL.MgraColor(MilGraphicsContext, MIL.M_COLOR_GREEN); MIL.MblobDraw(MilGraphicsContext, MilBlobResult, MilGraphicsList, MIL.M_DRAW_BOX, MIL.M_INCLUDED_BLOBS, MIL.M_DEFAULT); // Enable the display to update the drawings. MIL.MdispControl(MilDisplay, MIL.M_UPDATE, MIL.M_ENABLE); }
//} // User's archive function called each time a new buffer is grabbed. // -------------------------------------------------------------------*/ // Local defines for the annotations. static MIL_INT ArchiveFunction(MIL_INT HookType, MIL_ID HookId, IntPtr HookDataPtr) { GCHandle HookDataHandle = GCHandle.FromIntPtr(HookDataPtr); HookDataObject UserHookDataPtr = HookDataHandle.Target as HookDataObject; MIL_ID ModifiedImage = 0; // Retrieve the MIL_ID of the grabbed buffer. MIL.MdigGetHookInfo(HookId, MIL.M_MODIFIED_BUFFER + MIL.M_BUFFER_ID, ref ModifiedImage); // Increment the frame count. UserHookDataPtr.NbGrabbedFrames++; // Draw the frame count in the image if enabled. if (FRAME_NUMBER_ANNOTATION == MIL.M_YES) { MIL.MgraText(MIL.M_DEFAULT, ModifiedImage, STRING_POS_X, STRING_POS_Y, UserHookDataPtr.NbGrabbedFrames.ToString()); } // Compress the new image. if (UserHookDataPtr.MilCompressedImage != MIL.M_NULL) { MIL.MbufCopy(ModifiedImage, UserHookDataPtr.MilCompressedImage); } // Archive the new image. if (UserHookDataPtr.SaveSequenceToDisk) { MIL_ID ImageToExport; if (UserHookDataPtr.MilCompressedImage != MIL.M_NULL) { ImageToExport = UserHookDataPtr.MilCompressedImage; } else { ImageToExport = ModifiedImage; } MIL.MbufExportSequence(SEQUENCE_FILE, MIL.M_DEFAULT, ref ImageToExport, 1, MIL.M_DEFAULT, MIL.M_WRITE); UserHookDataPtr.NbArchivedFrames++; Console.Write("Frame #{0} \r", UserHookDataPtr.NbArchivedFrames); } // Copy the new grabbed image to the display. MIL.MbufCopy(ModifiedImage, UserHookDataPtr.MilImageDisp); return(0); }
/////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Fills the image buffer using MIL drawing. /// </summary> /// <param name="bufferSizeX">The width of the image buffer.</param> /// <param name="bufferSizeY">The height of the image buffer.</param> private void FillImageBuffer(MIL_INT bufferSizeX, MIL_INT bufferSizeY) { // Start by clearing the buffer MIL.MbufClear(_bufId, MIL.M_RGB888(0, 0, 0)); // Fill the buffer with default content. MIL_INT defaultGraFont = MIL.MgraInquire(MIL.M_DEFAULT, MIL.M_FONT, MIL.M_NULL); MIL.MgraFont(MIL.M_DEFAULT, MIL.M_FONT_DEFAULT_LARGE); MIL.MgraText(MIL.M_DEFAULT, _bufId, ((bufferSizeX / 8) * 2), bufferSizeY / 2, " Welcome to MIL !!! "); MIL.MgraRect(MIL.M_DEFAULT, _bufId, ((bufferSizeX / 8) * 2) - 60, (bufferSizeY / 2) - 80, ((bufferSizeX / 8) * 2) + 370, (bufferSizeY / 2) + 100); MIL.MgraRect(MIL.M_DEFAULT, _bufId, ((bufferSizeX / 8) * 2) - 40, (bufferSizeY / 2) - 60, ((bufferSizeX / 8) * 2) + 350, (bufferSizeY / 2) + 80); MIL.MgraRect(MIL.M_DEFAULT, _bufId, ((bufferSizeX / 8) * 2) - 20, (bufferSizeY / 2) - 40, ((bufferSizeX / 8) * 2) + 330, (bufferSizeY / 2) + 60); MIL.MgraFont(MIL.M_DEFAULT, defaultGraFont); }
// Calculate the rotated center of the model to compare the accuracy with // the center of the occurrence found during pattern matching. static void RotateModelCenter(MIL_ID Buffer, ref double X, ref double Y, double Angle) { MIL_INT BufSizeX = MIL.MbufInquire(Buffer, MIL.M_SIZE_X, MIL.M_NULL); MIL_INT BufSizeY = MIL.MbufInquire(Buffer, MIL.M_SIZE_Y, MIL.M_NULL); double RadAngle = Angle * ROTATED_FIND_RAD_PER_DEG; double CosAngle = Math.Cos(RadAngle); double SinAngle = Math.Sin(RadAngle); double OffSetX = (BufSizeX - 1) / 2.0; double OffSetY = (BufSizeY - 1) / 2.0; X = (ROTATED_FIND_MODEL_X_CENTER - OffSetX) * CosAngle + (ROTATED_FIND_MODEL_Y_CENTER - OffSetY) * SinAngle + OffSetX; Y = (ROTATED_FIND_MODEL_Y_CENTER - OffSetY) * CosAngle - (ROTATED_FIND_MODEL_X_CENTER - OffSetX) * SinAngle + OffSetY; }
/////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Allocates a MIL application, system, display, image buffer and digitizer (if available). /// </summary> public void Allocate() { // Allocate a MIL application. MIL.MappAlloc(MIL.M_NULL, MIL.M_DEFAULT, ref _appId); // Allocate a MIL system. MIL.MsysAlloc(MIL.M_DEFAULT, "M_DEFAULT", MIL.M_DEFAULT, MIL.M_DEFAULT, ref _sysId); // Allocate a MIL display. MIL.MdispAlloc(_sysId, MIL.M_DEFAULT, "M_DEFAULT", MIL.M_WINDOWED, ref _dispId); // Set default values for the image buffer in case no digitizer can be allocated. MIL_INT bufferSizeX = DEFAULT_IMAGE_SIZE_X; MIL_INT bufferSizeY = DEFAULT_IMAGE_SIZE_Y; MIL_INT bufferSizeBand = DEFAULT_IMAGE_SIZE_BAND; long imageAttributes = MIL.M_IMAGE | MIL.M_DISP | MIL.M_PROC; // Inquire the number of digitizers for the system. MIL_INT numberOfDigitizers = MIL.MsysInquire(_sysId, MIL.M_DIGITIZER_NUM, MIL.M_NULL); if (numberOfDigitizers > 0) { // Allocate a digitizer. MIL.MdigAlloc(_sysId, MIL.M_DEFAULT, "M_DEFAULT", MIL.M_DEFAULT, ref _digId); // Inquire the digitizer to determine the image buffer size. bufferSizeBand = MIL.MdigInquire(_digId, MIL.M_SIZE_BAND, MIL.M_NULL); bufferSizeX = MIL.MdigInquire(_digId, MIL.M_SIZE_X, MIL.M_NULL); bufferSizeY = MIL.MdigInquire(_digId, MIL.M_SIZE_Y, MIL.M_NULL); // Add the M_GRAB attibute to the image buffer. imageAttributes |= MIL.M_GRAB; } // Notify the UI that grabbing options have changed. RaisePropertyChangedEvent("CanStartGrab"); RaisePropertyChangedEvent("CanStopGrab"); // Allocate the image buffer. MIL.MbufAllocColor(_sysId, bufferSizeBand, bufferSizeX, bufferSizeY, 8 + MIL.M_UNSIGNED, imageAttributes, ref _bufId); // Notify the UI that the buffer size changed. RaisePropertyChangedEvent("BufferSizeX"); RaisePropertyChangedEvent("BufferSizeY"); // Fill the buffer with a default message. FillImageBuffer(bufferSizeX, bufferSizeY); }
// Local defines. private static MIL_INT ProcessingFunction(MIL_INT HookType, MIL_ID HookId, IntPtr HookDataPtr) { // get the handle to the DigHookUserData object back from the IntPtr GCHandle hUserData = GCHandle.FromIntPtr(HookDataPtr); // get a reference to the DigHookUserData object DigitizerImp pThis = hUserData.Target as DigitizerImp; pThis.HookId = HookId; pThis.HookType = HookType; pThis.OnImageGrabbed(); return(0); }
// Grab Start hook function: // This function is called at the start of each frame captured. // private static MIL_INT GrabStart(MIL_INT HookType, MIL_ID EventId, IntPtr UserObjectPtr) { if (UserObjectPtr != IntPtr.Zero) { GCHandle userObjectHandle = GCHandle.FromIntPtr(UserObjectPtr); UserDataObject userData = userObjectHandle.Target as UserDataObject; if (userData != null) { // Increment grab start count and print it. userData.NbGrabStart++; Console.Write("#{0}\r", userData.NbGrabStart); } } return(0); }
/// <summary> /// 현재 Grab하는 Image를 저장함. /// </summary> /// <param name="strPath"></param> /// <returns></returns> public bool SaveImage(MIL_ID pImage, string strPath) { MIL_ID pSaveImage = MIL.M_NULL; // Inquire overlay size. MIL_INT iWidth = MIL.MbufInquire(pImage, MIL.M_SIZE_X, MIL.M_NULL); MIL_INT iHeight = MIL.MbufInquire(pImage, MIL.M_SIZE_Y, MIL.M_NULL); MIL.MbufAlloc2d(m_pMilSystemID, iWidth, iHeight, MIL.M_UNSIGNED + 8, MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP, ref pSaveImage); MIL.MbufCopy(pImage, pSaveImage); MIL.MbufExport(strPath, MIL.M_BMP, pSaveImage); return(true); }
// Function to draw the current LUT's shape in the image. // // Note: This simple annotation method requires significant update // and CPU time since it repaints the entire image every time. // static void DrawLutShape(MIL_ID MilDisplay, MIL_ID MilOriginalImage, MIL_ID MilImage, MIL_INT Start, MIL_INT End, MIL_INT InflexionIntensity, MIL_INT ImageMaxValue, MIL_INT DisplayMaxValue) { double Xstart = 0.0; double Xend = 0.0; double Xstep = 0.0; double Ymin = 0.0; double Yinf = 0.0; double Ymax = 0.0; double Ystep = 0.0; MIL_INT ImageSizeX = 0; MIL_INT ImageSizeY = 0; // Inquire image dimensions. MIL.MbufInquire(MilImage, MIL.M_SIZE_X, ref ImageSizeX); MIL.MbufInquire(MilImage, MIL.M_SIZE_Y, ref ImageSizeY); // Calculate the drawing parameters. Xstep = (double)ImageSizeX / (double)ImageMaxValue; Xstart = Start * Xstep; Xend = End * Xstep; Ystep = ((double)ImageSizeY / 4.0) / (double)DisplayMaxValue; Ymin = ((double)ImageSizeY - 2); Yinf = Ymin - (InflexionIntensity * Ystep); Ymax = Ymin - (DisplayMaxValue * Ystep); // To increase speed, disable display update until all annotations are done. MIL.MdispControl(MilDisplay, MIL.M_UPDATE, MIL.M_DISABLE); // Restore the original image. MIL.MbufCopy(MilOriginalImage, MilImage); // Draw axis max and min values. MIL.MgraColor(MIL.M_DEFAULT, (double)ImageMaxValue); MIL.MgraText(MIL.M_DEFAULT, MilImage, 4, (int)Ymin - 22, "0"); MIL.MgraText(MIL.M_DEFAULT, MilImage, 4, (int)Ymax - 16, String.Format("{0}", DisplayMaxValue)); MIL.MgraText(MIL.M_DEFAULT, MilImage, ImageSizeX - 38, (int)Ymin - 22, String.Format("{0}", ImageMaxValue)); // Draw LUT Shape (X axis is display values and Y is image values). MIL.MgraLine(MIL.M_DEFAULT, MilImage, 0, (int)Ymin, (int)Xstart, (int)Ymin); MIL.MgraLine(MIL.M_DEFAULT, MilImage, (int)Xstart, (int)Ymin, (int)Xend, (int)Yinf); MIL.MgraLine(MIL.M_DEFAULT, MilImage, (int)Xend, (int)Yinf, ImageSizeX - 1, (int)Ymax); // Enable display update to show the result. MIL.MdispControl(MilDisplay, MIL.M_UPDATE, MIL.M_ENABLE); }
public int SetLocalOverlay(bool milSystem) { // Prepare overlay buffer. //*************************** // Enable the display of overlay annotations. MIL.MdispControl(m_MilDisplay, MIL.M_OVERLAY, MIL.M_ENABLE); // Inquire the overlay buffer associated with the display. MIL.MdispInquire(m_MilDisplay, MIL.M_OVERLAY_ID, ref m_MilOverlay); // Clear the overlay to transparent. MIL.MdispControl(m_MilDisplay, MIL.M_OVERLAY_CLEAR, MIL.M_DEFAULT); // Disable the overlay display update to accelerate annotations. MIL.MdispControl(m_MilDisplay, MIL.M_OVERLAY_SHOW, MIL.M_DISABLE); // Inquire overlay size. m_ImageWidth = MIL.MbufInquire(m_MilOverlay, MIL.M_SIZE_X, MIL.M_NULL); m_ImageHeight = MIL.MbufInquire(m_MilOverlay, MIL.M_SIZE_Y, MIL.M_NULL); // Draw MIL overlay annotations. //********************************* // Set the graphic text background to transparent. MIL.MgraControl(m_ImgText, MIL.M_BACKGROUND_MODE, MIL.M_TRANSPARENT); // Re-enable the overlay display after all annotations are done. MIL.MdispControl(m_MilDisplay, MIL.M_OVERLAY_SHOW, MIL.M_ENABLE); m_MilOverLayID = MIL.MgraAlloc(m_pMilSystemID, MIL.M_NULL); // Draw GDI color overlay annotation. //*********************************** // The inquire might not be supported MIL.MappControl(MIL.M_DEFAULT, MIL.M_ERROR, MIL.M_PRINT_DISABLE); // Allocate a graphic list to hold the subpixel annotations to draw. MIL.MgraAllocList(m_pMilSystemID, MIL.M_DEFAULT, ref GraphicList); // Associate the graphic list to the display for annotations. MIL.MdispControl(m_MilDisplay, MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList); MIL.MgraColor(MIL.M_DEFAULT, MIL.M_COLOR_GREEN); return(0); }
const int HALF_LUMINANCE = 128; // All colors have half luminance. // Creates a color display LUT to show defects in red. static void SetupColorDisplay(MIL_ID MilSystem, MIL_ID MilDisplay, MIL_INT SizeBit) { MIL_ID MilRampLut1Band = MIL.M_NULL; // LUT containing hue values. MIL_ID MilRampLut3Band = MIL.M_NULL; // RGB LUT used by display. MIL_ID MilColorImage = MIL.M_NULL; // Image used for HSL to RGB conversion. MIL_INT DefectGrayLevel = 0; // Gray level under which all is red. MIL_INT ExpectedGrayLevel = 0; // Gray level over which all is blue. MIL_INT NbGrayLevels; // Number of possible gray levels in corrected depth map. NbGrayLevels = (MIL_INT)(1 << (int)SizeBit); // Allocate 1-band LUT that will contain hue values. MIL.MbufAlloc1d(MilSystem, NbGrayLevels, 8 + MIL.M_UNSIGNED, MIL.M_LUT, ref MilRampLut1Band); // Compute limit gray values. DefectGrayLevel = (MIL_INT)((EXPECTED_HEIGHT - SATURATED_DEFECT) * SCALE_FACTOR); ExpectedGrayLevel = (MIL_INT)(EXPECTED_HEIGHT * SCALE_FACTOR); // Create hue values for each possible gray level. MIL.MgenLutRamp(MilRampLut1Band, 0, RED_HUE, DefectGrayLevel, RED_HUE); MIL.MgenLutRamp(MilRampLut1Band, DefectGrayLevel, RED_HUE, ExpectedGrayLevel, BLUE_HUE); MIL.MgenLutRamp(MilRampLut1Band, ExpectedGrayLevel, BLUE_HUE, NbGrayLevels - 1, BLUE_HUE); // Create a HSL image buffer. MIL.MbufAllocColor(MilSystem, 3, NbGrayLevels, 1, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE, ref MilColorImage); MIL.MbufClear(MilColorImage, MIL.M_RGB888(0, FULL_SATURATION, HALF_LUMINANCE)); // Set its H band (hue) to the LUT contents and convert the image to RGB. MIL.MbufCopyColor2d(MilRampLut1Band, MilColorImage, 0, 0, 0, 0, 0, 0, NbGrayLevels, 1); MIL.MimConvert(MilColorImage, MilColorImage, MIL.M_HSL_TO_RGB); // Create RGB LUT to give to display and copy image contents. MIL.MbufAllocColor(MilSystem, 3, NbGrayLevels, 1, 8 + MIL.M_UNSIGNED, MIL.M_LUT, ref MilRampLut3Band); MIL.MbufCopy(MilColorImage, MilRampLut3Band); // Associates LUT to display. MIL.MdispLut(MilDisplay, MilRampLut3Band); // Free all allocations. MIL.MbufFree(MilRampLut1Band); MIL.MbufFree(MilRampLut3Band); MIL.MbufFree(MilColorImage); }
static MIL_INT ProcessingFunction(MIL_INT HookType, MIL_ID HookId, IntPtr HookDataPtr) { // this is how to check if the user data is null, the IntPtr class // contains a member, Zero, which exists solely for this purpose if (!IntPtr.Zero.Equals(HookDataPtr)) { // get the handle to the ProcessingHookDataStruct object back from the IntPtr GCHandle hUserData = GCHandle.FromIntPtr(HookDataPtr); // get a reference to the ProcessingHookDataStruct object ProcessingHookDataStruct UserHookDataPtr = hUserData.Target as ProcessingHookDataStruct; MIL_ID ModifiedBufferId = MIL.M_NULL; string Text; // Retrieve the MIL_ID of the grabbed buffer. MIL.MdigGetHookInfo(HookId, MIL.M_MODIFIED_BUFFER + MIL.M_BUFFER_ID, ref ModifiedBufferId); switch (UserHookDataPtr.ProcessingOperation) { case ProcessingHookOperation.DISPLAY: // Update the display with the last captured image. MIL.MbufCopy(ModifiedBufferId, UserHookDataPtr.MilImageDisp); break; case ProcessingHookOperation.ENCODE: // Increase the compressed images count. UserHookDataPtr.ProcessedImageCount++; // Print and draw the frame count (comment to reduce CPU usage). Console.Write("Processing frame #{0}.\r", UserHookDataPtr.ProcessedImageCount); Text = string.Format("{0}", UserHookDataPtr.ProcessedImageCount); MIL.MgraText(MIL.M_DEFAULT, ModifiedBufferId, STRING_POS_X, STRING_POS_Y, Text); // Enqueue the grabbed buffer for parallel encoding. MIL.MseqFeed(UserHookDataPtr.MilSeqContext, ModifiedBufferId, MIL.M_DEFAULT); // Update the display with the last captured image. MIL.MbufCopy(ModifiedBufferId, UserHookDataPtr.MilImageDisp); break; } } return(0); }
//**************************************************************************** // Draw the samples as color patches. static void DrawSampleColors(MIL_ID DestImage, MIL_INT[,] pSamplesColors, string[] pSampleNames, MIL_INT NumSamples, MIL_INT XSpacing, MIL_INT YOffset) { MIL_INT DestSizeX = MIL.MbufInquire(DestImage, MIL.M_SIZE_X, MIL.M_NULL); MIL_INT DestSizeY = MIL.MbufInquire(DestImage, MIL.M_SIZE_Y, MIL.M_NULL); double OffsetX = (DestSizeX - (NumSamples * COLOR_PATCH_SIZEX) - ((NumSamples - 1) * XSpacing)) / 2.0; double OffsetY = YOffset > 0 ? YOffset : (DestSizeY - COLOR_PATCH_SIZEY) / 2.0; double TextOffsetX; MIL.MgraFont(MIL.M_DEFAULT, MIL.M_FONT_DEFAULT_SMALL); for (MIL_INT SampleIndex = 0; SampleIndex < NumSamples; SampleIndex++) { MIL.MgraColor(MIL.M_DEFAULT, MIL.M_RGB888((int)pSamplesColors[SampleIndex, 0], (int)pSamplesColors[SampleIndex, 1], (int)pSamplesColors[SampleIndex, 2])); MIL.MgraRectFill(MIL.M_DEFAULT, DestImage, OffsetX, OffsetY, OffsetX + COLOR_PATCH_SIZEX, OffsetY + COLOR_PATCH_SIZEY); MIL.MgraColor(MIL.M_DEFAULT, MIL.M_COLOR_YELLOW); TextOffsetX = OffsetX + COLOR_PATCH_SIZEX / 2.0 - 4.0 * pSampleNames[SampleIndex].Length + 0.5; MIL.MgraText(MIL.M_DEFAULT, DestImage, TextOffsetX, OffsetY - 20, pSampleNames[SampleIndex]); OffsetX += (COLOR_PATCH_SIZEX + XSpacing); } }
// Optional encoding end function called every time a buffer is finished being compressed. // ---------------------------------------------------------------------------------------- static MIL_INT FrameEncodingEndFunction(MIL_INT HookType, MIL_ID HookId, IntPtr HookDataPtr) { // this is how to check if the user data is null, the IntPtr class // contains a member, Zero, which exists solely for this purpose if (!IntPtr.Zero.Equals(HookDataPtr)) { // get the handle to the FrameEndHookDataStruct object back from the IntPtr GCHandle hUserData = GCHandle.FromIntPtr(HookDataPtr); // get a reference to the FrameEndHookDataStruct object EncodingFrameEndHookDataStruct UserHookDataPtr = hUserData.Target as EncodingFrameEndHookDataStruct; // Frame end hook post processing. if (HookType == MIL.M_FRAME_END) { MIL_ID CompressedBufferId = MIL.M_NULL; MIL_INT CompressedDataPtr = MIL.M_NULL; MIL_INT CompressedDataSize = 0; // Increment a encoded frame counter UserHookDataPtr.EncodedImageCount++; // Retrieve the MIL_ID of the encoded buffer. MIL.MseqGetHookInfo(HookId, MIL.M_MODIFIED_BUFFER + MIL.M_BUFFER_ID, ref CompressedBufferId); // Retrieves the address of the encoded data. MIL.MbufInquire(CompressedBufferId, MIL.M_HOST_ADDRESS, ref CompressedDataPtr); // Retrieves the size in bytes of the encoded data. MIL.MbufInquire(CompressedBufferId, MIL.M_SIZE_BYTE, ref CompressedDataSize); // ----------------------------------------------------------------------------------------------- // Here you can do any action with the encoded data such as send buffer through a network stream. // If the processing done on the compressed data is long, it is recommended to copy the // buffer and to process it in a separate thread to avoid blocking the compressions flow. // ----------------------------------------------------------------------------------------------- } } return(0); }
static void DrawCursor(MIL_ID AnnotationDisplay, MIL_INT Position) { MIL_ID AnnotationImage = MIL.M_NULL; MIL_INT BufSizeX = 0; MIL_INT BufSizeY = 0; MIL_INT n = 0; // Prepare for overlay annotations. MIL.MdispControl(AnnotationDisplay, MIL.M_OVERLAY, MIL.M_ENABLE); MIL.MdispControl(AnnotationDisplay, MIL.M_OVERLAY_CLEAR, MIL.M_DEFAULT); MIL.MdispInquire(AnnotationDisplay, MIL.M_OVERLAY_ID, ref AnnotationImage); MIL.MbufInquire(AnnotationImage, MIL.M_SIZE_X, ref BufSizeX); MIL.MbufInquire(AnnotationImage, MIL.M_SIZE_Y, ref BufSizeY); MIL.MgraColor(MIL.M_DEFAULT, CURSOR_COLOR); // Write annotations. n = (BufSizeX / FOCUS_MAX_NB_POSITIONS); MIL.MgraLine(MIL.M_DEFAULT, AnnotationImage, 0, CURSOR_POSITION(BufSizeY) + CURSOR_SIZE, BufSizeX - 1, CURSOR_POSITION(BufSizeY) + CURSOR_SIZE); MIL.MgraLine(MIL.M_DEFAULT, AnnotationImage, Position * n, CURSOR_POSITION(BufSizeY) + CURSOR_SIZE, Position * n - CURSOR_SIZE, CURSOR_POSITION(BufSizeY)); MIL.MgraLine(MIL.M_DEFAULT, AnnotationImage, Position * n, CURSOR_POSITION(BufSizeY) + CURSOR_SIZE, Position * n + CURSOR_SIZE, CURSOR_POSITION(BufSizeY)); MIL.MgraLine(MIL.M_DEFAULT, AnnotationImage, Position * n - CURSOR_SIZE, CURSOR_POSITION(BufSizeY), Position * n + CURSOR_SIZE, CURSOR_POSITION(BufSizeY)); }