void SourceImagesLoader(string folderLocation) { fileNames = Directory.GetFiles(folderLocation, "*.png"); if (fileNames.Length == 0) { isSampleImageFolderEmpty = true; return; } // Dispose old images and load new images if PTAMSourceImages is not null if (PTAMSourceImages != null) { for (int imageNumber = 0; imageNumber < fileNames.Length; imageNumber++) { PTAMSourceImages[imageNumber].Dispose(); } } PTAMSourceImages = new Bitmap[fileNames.Length]; for (int imageNumber = 0; imageNumber < fileNames.Length; imageNumber++) { PTAMSourceImages[imageNumber] = new Bitmap(fileNames[imageNumber]); } ThreadHelperClass.SetImage(this, imgImagePreview, PTAMSourceImages[0]); }
void ConvertImages(ConvertMode convertMode, ThresholdImageType type, string SavePath) { for (int imagePosition = 0; imagePosition < fileName.Length; imagePosition++) { ThreadHelperClass.SetImage(this, imgPreviewImage, this.image[imagePosition].Bitmap); ThreadHelperClass.SetText(this, lblPreviewImagePosition, "Image " + (imagePosition + 1) + " of " + this.image.Length); Mat image = new Mat(this.image[imagePosition].Rows, this.image[imagePosition].Cols, DepthType.Cv8U, 1); CvInvoke.CvtColor(this.image[imagePosition], image, ColorConversion.Bgr2Gray); Mat resultImage = new Mat(image.Rows, image.Cols, Emgu.CV.CvEnum.DepthType.Cv8U, 1); if (convertMode == ConvertMode.Automatic) { if (type == ThresholdImageType.Normal) { CvInvoke.AdaptiveThreshold(image, resultImage, 255, Emgu.CV.CvEnum.AdaptiveThresholdType.GaussianC, Emgu.CV.CvEnum.ThresholdType.Binary, 75, 25); } else { CvInvoke.AdaptiveThreshold(image, resultImage, 255, Emgu.CV.CvEnum.AdaptiveThresholdType.GaussianC, Emgu.CV.CvEnum.ThresholdType.BinaryInv, 75, 25); } } else { if (type == ThresholdImageType.Normal) { resultImage = new Image <Bgra, byte>(ImageProcessor.ImageProcessor.RecolorMonochrome(image.Bitmap, threshold)).Mat; } else { resultImage = new Image <Bgra, byte>(ImageProcessor.ImageProcessor.RecolorMonochromeInverted(image.Bitmap, threshold)).Mat; } } ThreadHelperClass.SetImage(this, imgResultImage, resultImage.Bitmap); resultImage.Bitmap.Save(SavePath + "\\" + Path.GetFileName(fileName[imagePosition])); image.Dispose(); resultImage.Dispose(); } }
// Programatically change icon and creating a simple animation void IconAnimation() { // Get bitmap files string[] iconAnimationFile = Directory.GetFiles(Application.StartupPath + "\\Sources", "*.png"); // Manual filling for (int i = 0; i < iconAnimationFile.Length; i++) { iconAnimationFile[i] = Application.StartupPath + "\\Sources\\" + (i + 1).ToString() + ".png"; } // Load bitmap to memory Bitmap[] sourceIcon = new Bitmap[iconAnimationFile.Length]; for (int iconNumber = 0; iconNumber < iconAnimationFile.Length; iconNumber++) { sourceIcon[iconNumber] = new Bitmap(iconAnimationFile[iconNumber]); } Icon[] icon = new Icon[sourceIcon.Length]; for (int iconNumber = 0; iconNumber < sourceIcon.Length; iconNumber++) { IntPtr Hicon = sourceIcon[iconNumber].GetHicon(); icon[iconNumber] = Icon.FromHandle(Hicon); } // Begin animating int position = 0; do { ThreadHelperClass.SetIcon(this, this, icon[position]); position++; if (position == icon.Length) { position = 0; } Thread.Sleep(1000 / 30); } while (true); }
void Kursor3DTestThread() { // TODO: Implement actAsModelerModule ThreadHelperClass.SetText(this, lblStatusValue, "Thread started. Loading images from selected folder to memory..."); ThreadHelperClass.SetText(this, lblStatusValue, "Thread started. Loading images from selected folder to memory..."); #region Test image loader Bitmap[] sourceImage; string[] Files = Directory.GetFiles(cursor3DSourceImage, "*.png"); sourceImage = new Bitmap[Files.Length]; for (int fileNumber = 0; fileNumber < Files.Length; fileNumber++) { sourceImage[fileNumber] = new Bitmap(Files[fileNumber]); } #endregion Test image loader ThreadHelperClass.SetText(this, lblStatusValue, "Images loaded. " + Files.Length + " files were added to memory"); bool isExitRequested = false; MMF mappedFile = new MMF(); mappedFile.CreateNewFile("Kursor3DSourceImage", 10000000); int position = 0; do { Stopwatch overalPerformance = new Stopwatch(); overalPerformance.Start(); // Create preview of loaded image ThreadHelperClass.SetImage(this, picImagePreview, sourceImage[position]); ThreadHelperClass.SetText(this, lblStatusValue, "Now previewing image: " + Files[position]); position++; if (position == Files.Length) { position = 0; } #region Receiver threat receiverThread = new Thread(ResultReceiver); receiverThread.Start(); #endregion Receiver threat ThreadHelperClass.SetText(this, lblStatusValue, "Writing image to memory-mapped file"); // Convert image to array and save to tempImage Stopwatch imageConversionWatcher = new Stopwatch(); imageConversionWatcher.Start(); #region Image converter byte[] tempImage; using (var ms = new MemoryStream()) { sourceImage[position].Save(ms, sourceImage[position].RawFormat); tempImage = ms.ToArray(); } #endregion Image converter imageConversionWatcher.Stop(); ThreadHelperClass.SetText(this, lblImageConversionPerformaceValue, imageConversionWatcher.ElapsedMilliseconds.ToString() + "ms"); Stopwatch MMFWatcher = new Stopwatch(); MMFWatcher.Start(); // Write the image to memory-mapped file mappedFile.AddInformation(Convert.ToBase64String(tempImage)); MMFWatcher.Stop(); ThreadHelperClass.SetText(this, lblMMFPerformanceValue, MMFWatcher.ElapsedMilliseconds.ToString() + "ms"); ThreadHelperClass.SetText(this, lblStatusValue, "File writen. Sending notification to Cursor 3D"); // Notify Kursor3D Module about the image SendReport("Kursor3DImageNotifier"); ThreadHelperClass.SetText(this, lblStatusValue, "Module connected. Now waiting for the module to complete the process"); // Wait for receiver thread to complete if (receiverThread.IsAlive) { receiverThread.Join(); } receiverThread = null; #region UI setter ThreadHelperClass.SetText(this, lblStatusValue, "Module has finished the work. Writing the received values"); ThreadHelperClass.SetText(this, lblXPositionValue, x.ToString()); ThreadHelperClass.SetText(this, lblYPositionValue, y.ToString()); ThreadHelperClass.SetText(this, lblZPositionValue, z.ToString()); ThreadHelperClass.SetText(this, lblGestureTypeValue, gestureType); overalPerformance.Stop(); ThreadHelperClass.SetText(this, lblOveralPerformanceValue, overalPerformance.ElapsedMilliseconds.ToString() + "ms"); ThreadHelperClass.SetText(this, lblKursor3DOverallPerformanceValue, kursor3DOverallPerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblKursor3DFindHandThreadPerformanceValue, kursor3DFindHandPerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblKursor3DFindDepthThreadPerformanceValue, kursor3DFindDepthPerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblKursor3DGestureRecognitionThreadPerformanceValue, kursor3DgestureRecognitionPerformance.ToString() + "ms"); #endregion UI setter } while (!isExitRequested); }
void MainFunction() { ThreadHelperClass.SetText(this, lblStatusValue, "Loading sample images to memory..."); SourceImagesLoader(txtSampleImagesFolderLocation.Text); if (isSampleImageFolderEmpty) { ThreadHelperClass.SetText(this, lblStatus, "The selected folder has no images or the images is not supported"); } else { ThreadHelperClass.SetText(this, lblStatusValue, "Loading finished. " + PTAMSourceImages.Length + " images were loaded into memory."); } Thread.Sleep(100); ThreadHelperClass.SetText(this, lblStatusValue, "Preparing for sending..."); MMF mappedFile = new MMF(); mappedFile.CreateNewFile(PTAMModuleSourceImageFileName, 10000000); int position = 0; do { Stopwatch overallPerformance = new Stopwatch(); overallPerformance.Start(); // Create preview of loaded image ThreadHelperClass.SetImage(this, imgImagePreview, PTAMSourceImages[position]); ThreadHelperClass.SetText(this, lblStatusValue, "Now previewing image: " + fileNames[position]); position++; if (position == fileNames.Length) { position = 0; } #region Receiver threat receiverThread = new Thread(ResultReceiver); receiverThread.Start(); #endregion Receiver threat ThreadHelperClass.SetText(this, lblStatusValue, "Writing image to memory-mapped file"); #region Image converter // Convert image to array and save to tempImage Stopwatch imageConversionWatcher = new Stopwatch(); imageConversionWatcher.Start(); byte[] tempImage; using (var ms = new MemoryStream()) { PTAMSourceImages[position].Save(ms, PTAMSourceImages[position].RawFormat); tempImage = ms.ToArray(); } imageConversionWatcher.Stop(); testerImageConversionPerformance = imageConversionWatcher.ElapsedMilliseconds; #endregion Image converter #region Memory-Mapped File writing Stopwatch MMFWatcher = new Stopwatch(); MMFWatcher.Start(); // Write the image to memory-mapped file mappedFile.AddInformation(Convert.ToBase64String(tempImage)); MMFWatcher.Stop(); testerMemoryMappedFilePerformance = MMFWatcher.ElapsedMilliseconds; #endregion Memory-Mapped File writing ThreadHelperClass.SetText(this, lblStatusValue, "File writen. Sending notification to PTAM Module"); // Notify Kursor3D Module about the image SendReport(PTAMModuleNamedPipeSendingChannel); ThreadHelperClass.SetText(this, lblStatusValue, "Module connected. Now waiting for the module to complete the process"); // Wait for receiver thread to complete if (receiverThread.IsAlive) { receiverThread.Join(); } receiverThread = null; ThreadHelperClass.SetText(this, lblStatusValue, "Module has finished the work. Writing the received values"); #region UI setter #region Pose #region Position ThreadHelperClass.SetText(this, lblPositionXValue, receivedXPosition.ToString()); ThreadHelperClass.SetText(this, lblPositionYValue, receivedYPosition.ToString()); ThreadHelperClass.SetText(this, lblPositionZValue, receivedZPosition.ToString()); #endregion Position #region Orientation ThreadHelperClass.SetText(this, lblOrientationXValue, receivedXOrientation.ToString()); ThreadHelperClass.SetText(this, lblOrientationYValue, receivedYOrientation.ToString()); ThreadHelperClass.SetText(this, lblOrientationZValue, receivedZOrientation.ToString()); #endregion Orientation #endregion Pose #region Points ThreadHelperClass.SetText(this, lblTotalPointCreatedValue, totalPointsCreated.ToString()); #endregion Points overallPerformance.Stop(); testerOverallPerformance = overallPerformance.ElapsedMilliseconds; ThreadHelperClass.SetText(this, lblOverallPerformanceValue, testerOverallPerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblMemoryMappedFilePerformanceValue, testerMemoryMappedFilePerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblImageConversionPerformanceValue, testerImageConversionPerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblPTAMModuleOverallPerformanceValue, PTAMModuleOverallPerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblPTAMModuleTrackingPerformanceValue, PTAMTrackingPerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblPTAMModuleMappingPerformanceValue, PTAMMappingPerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblPTAMModuleImageConversionPerformanceValue, PTAMImageConversionPerformance.ToString() + "ms"); ThreadHelperClass.SetText(this, lblPTAMModuleMemoryMappedFileReadingPerformanceValue, PTAMMemoryMappedFileReadingPerformance.ToString() + "ms"); #endregion UI setter } while (!isExitRequested); }
void ModelerThread() { ThreadHelperClass.SetText(this, lblStatusValue, "Requesting the HUB resolution..."); bool isExitRequested = false; char receivedCode; NamedPipesServer imageNotifier = new NamedPipesServer(); MMF ImageData; Bitmap imageReceived; #region Screen Resolution Request int width; int height; // Connect for first time check for resolution used by main program NamedPipesServer server = new NamedPipesServer(); server.CreateNewServerPipe("res-pipe", NamedPipesServer.PipeDirection.DirectionInOut, NamedPipesServer.SendMode.MessageMode); server.WaitForConnection(); string resolution = server.ReadMessage(); // Separate screen width and height string[] res = resolution.Split('x'); width = Convert.ToInt32(res[0]); height = Convert.ToInt32(res[1]); string receivedResolution = "Connected client resolution: " + width + " x " + height; ThreadHelperClass.SetText(this, lblStatusValue, "HUB's resolution received"); ThreadHelperClass.SetText(this, lblScreenResolutionValue, receivedResolution); #endregion Screen Resolution Request bool isFirstTime = true; do { ThreadHelperClass.SetText(this, lblStatusValue, "Waiting new notification..."); try { // Memory-mapped file access server.CreateNewServerPipe("ImageNotifier", NamedPipesServer.PipeDirection.DirectionInOut, NamedPipesServer.SendMode.ByteMode); server.WaitForConnection(); if (server.ReadByte() == (char)'y') { ThreadHelperClass.SetText(this, lblStatusValue, "Receiving image..."); byte[] file = null; Console.WriteLine('y'); // Load image from Memory-mapped file and display as texture for model. MMF mappedFile = new MMF(); mappedFile.OpenExisting("MapTest"); file = Convert.FromBase64String(mappedFile.ReadContent(MMF.DataType.DataString)); using (var ms = new MemoryStream(file)) { imageReceived = new Bitmap(ms); } ThreadHelperClass.SetImage(this, imgTesterPreviewImage, imageReceived); } } catch (Exception err) { throw; } server.Disconnect(); server.ClosePipe(); //try //{ // imageNotifier.CreateNewServerPipe("test-pipe", NamedPipesServer.PipeDirection.DirectionInOut, NamedPipesServer.SendMode.ByteMode); // imageNotifier.WaitForConnection(); // receivedCode = (char)imageNotifier.ReadByte(); // imageNotifier.WaitForPipeDrain(); // if (receivedCode == 'x') // isExitRequested = true; // else // isExitRequested = false; // if (receivedCode == 'y') // { // ThreadHelperClass.SetText(this, lblStatusValue, "New notification. Receiving..."); // byte[] file = null; // // Load image from Memory-mapped file // MMF mappedFile = new MMF(); // mappedFile.OpenExisting("MapTest"); // file = Convert.FromBase64String(mappedFile.ReadContent(MMF.DataType.DataString)); // using (var ms = new MemoryStream(file)) // { // imageReceived = new Bitmap(ms); // } // ThreadHelperClass.SetImage(this, previewImage, imageReceived); // } //} //catch (Exception err) //{ // throw; //} //server.Disconnect(); //server.ClosePipe(); } while (!isExitRequested); }