/// <summary> /// Detect edge transitions using value deltas /// </summary> /// <param name="parms">Detection parameters</param> public void DetectEdges(DetectorParams parms) { Bitmap srcImg = parms.SourceImage; int threshold = parms.Threshold; Bitmap destImg = new Bitmap(srcImg.Width, srcImg.Height); float step = 100f / (float)(srcImg.Height + 1); State = DetectorState.Processing; int[,] levels = DetectorHelper.GetChannel(srcImg, parms.Channel, UpdateUi); for (int y = 0; y < destImg.Height - 1 && State == DetectorState.Processing; y++) { for (int x = 0; x < destImg.Width - 1; x++) { int dx = levels[x + 1, y] - levels[x, y]; int dy = levels[x, y + 1] - levels[x, y]; Color rgb = (dx > threshold || dy > threshold) ? Color.White : Color.Black; destImg.SetPixel(x, y, rgb); } UpdateUi(50 + y * 50 / destImg.Height); } EndUpdate(destImg); State = DetectorState.Ready; }
/// <summary> /// Update the image details for the current image /// </summary> private void UpdateDetails() { DetectorParams parms = _images[_imageIndex]; TbImageIndex.Text = (_imageIndex + 1).ToString(); TbTotalImages.Text = _images.Count.ToString(); if (parms.DestImage != null) { TbImageSize.Text = string.Format("{0} x {1}", parms.DestImage.Width, parms.DestImage.Height); } else { TbImageSize.Text = ""; } if (parms.Detector != null) { TbImageAlg.Text = parms.Detector.Name; TbImageThreshold.Text = parms.Threshold.ToString(); TbImageChannel.Text = parms.Channel.ToString(); } else { TbImageAlg.Text = ""; TbImageThreshold.Text = ""; TbImageChannel.Text = ""; } }
private void BtnOpenFile_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = Directory.GetCurrentDirectory(); if (ofd.ShowDialog() == DialogResult.OK) { Stream fs = null; try { fs = ofd.OpenFile(); Image img = Image.FromStream(fs); DetectorParams parm = new DetectorParams(); parm.DestImage = new Bitmap(img); _images.Add(parm); _imageIndex = _images.Count - 1; UpdateDetails(); PbImage.Image = img; PbImage.SizeMode = PictureBoxSizeMode.Zoom; Text = string.Format("Edge Detect - {0} ({1},{2})", Path.GetFileName(ofd.FileName), img.Width, img.Height); } finally { if (fs != null) { fs.Dispose(); } } } }
/// <summary> /// Called when detection is complete /// </summary> /// <param name="result">Image created from edge detection</param> void DetectUpdateEnd(Bitmap result) { if (InvokeRequired) { Invoke((MethodInvoker) delegate { DetectUpdateEnd(result); }); } else { // Update current then add to the list _currentParams.DestImage = result; _images.Add(_currentParams); _imageIndex = _images.Count - 1; _currentParams = null; CmbChannel.Enabled = true; CmbEdgeDector.Enabled = true; BtnDetect.Text = "Detect"; DetectProgress.Visible = false; UpdateDetails(); PbImage.Image = result; PbImage.Invalidate(); } }
/// <summary> /// User clicked [Detect] button. If active, stop the detection process. Otherwise start a new detection thread. /// </summary> private void BtnDetect_Click(object sender, EventArgs e) { if (_currentParams != null && _currentParams.Detector.State == DetectorState.Processing) { _currentParams.Detector.State = DetectorState.Abort; } else { DetectProgress.Value = 0; DetectProgress.Visible = true; CmbEdgeDector.Enabled = false; CmbChannel.Enabled = false; BtnDetect.Text = "Stop"; _currentParams = new DetectorParams(); _currentParams.Detector = CmbEdgeDector.SelectedItem as IEdgeDetector; _currentParams.SourceImage = _images[_imageIndex].DestImage; _currentParams.Threshold = int.Parse(TbThreshold.Text); _currentParams.Channel = (DetectorChannel)CmbChannel.SelectedIndex; Thread thd = new Thread(StartDetect); thd.Start(); } }