Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Thursday, January 12, 2023 5:29:16 PM(UTC)

Amin  
Amin

Groups: Manager, Tech Support
Posts: 367

Was thanked: 1 time(s) in 1 post(s)

File Attachment(s):
KeepOneColor.zip (12kb) downloaded 7 time(s).


If 2 moderately different colors share the same name when expressed by a person, they usually have similar or close Hue values. This proximity in Hue values can be used to select all the shades that are considered close to a certain color as explained below.

The attached C# project allows the user to load an image and click on any point in it. It then displays a Color dialog box to allow verifying and fine-tuning the color selection, after which it selects all pixels that have Hue values that are distant from the selected color and fills them all with black.

Here are the main parts of the project and their corresponding code:

1. When the "Pick Color" menu item is clicked after an image is loaded, a "color indicator" window is created and the mouse events are enabled.
Pick Color

Code:

Form ColorIndicator = null;

private void pickColorToolStripMenuItem_Click(object sender, EventArgs e)
{
   if (_viewer.Image == null)
   {
      MessageBox.Show("Unable to pick color! Please load an image first");
      return;
   }
   MessageBox.Show("Click a point on the image to select it. A small window will show the currently selected point when you move the mouse\n\nAfter selecting a point, verify the color using the Color dialog box");
   if (ColorIndicator == null)
   {
      ColorIndicator = new Form();
      ColorIndicator.TopMost = true;
      ColorIndicator.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
      ColorIndicator.MaximizeBox = false;
      ColorIndicator.MinimizeBox = false;
      ColorIndicator.Text = "Color indicator";
      ColorIndicator.Location = new Point(100, 100);
      ColorIndicator.Size = new System.Drawing.Size(200, 150);
   }
   ColorIndicator.Show();
   _viewer.MouseMove += _viewer_MouseMove;
   _viewer.MouseClick += _viewer_MouseClick;
}



2. As the mouse cursor moves, the color from the image pixel below the cursor is shown in the indicator window. The code performs coordinate conversion in case the image is scrolled.

Color indicator when the mouse moves

Code:

// Convert mouse click to actual pixel coordinates
public Point ClientToBitmapPt(float x, float y)
{
   //Convert Client Coordinates to Bitmap Coordinates
   var t = _viewer.ImageTransform; //tems[0].Transform;
   Matrix m = new Matrix((float)t.M11, (float)t.M12, (float)t.M21, (float)t.M22, (float)t.OffsetX, (float)t.OffsetY);
   Transformer transformer = new Transformer(m);

   //Convert to compensate for view size mode, scale, scroll, etc...
   Point ptBmp = Point.Round(transformer.PointToLogical(new PointF(x, y)));

   // Put into LeadPoint
   LeadPoint leadPt = new LeadPoint(ptBmp.X, ptBmp.Y);

   //This is always a TopLeft, so convert to image view perspective
   leadPt = _viewer.Image.PointToImage(Leadtools.RasterViewPerspective.TopLeft, leadPt);

   // Put back into point
   ptBmp = new Point(leadPt.X, leadPt.Y);
   return ptBmp;
}

private void _viewer_MouseMove(object sender, MouseEventArgs e)
{
   var img = _viewer.Image;
   Point point = ClientToBitmapPt(e.X, e.Y);
   if (point.X >= img.Width || point.Y >= img.Height)
      return;
   var c = img.GetPixel(point.Y, point.X);
   ColorIndicator.BackColor = Color.FromArgb(c.R, c.G, c.B);
}


3. When a point is clicked, the corresponding pixel's color is shown in a dialog to allow refining.

Color dialog displayed after click

Important note:
The Hue, Sat and Lum numbers in the Windows color dialog do NOT have the same range of the Hue, Saturation and Value numbers used by LEADTOOLS.

Code:

private void _viewer_MouseClick(object sender, MouseEventArgs e)
{
   ColorIndicator.Hide();
   _viewer.MouseClick -= _viewer_MouseClick;
   _viewer.MouseMove -= _viewer_MouseMove;
   Point point = ClientToBitmapPt(e.X, e.Y);
   var leadColor = _viewer.Image.GetPixel(point.Y, point.X);
   ColorDialog colorDialog = new ColorDialog();
   colorDialog.Color = Color.FromArgb(leadColor.R, leadColor.G, leadColor.B);
   colorDialog.AllowFullOpen = true;
   colorDialog.AnyColor= true;
   colorDialog.FullOpen = true;
   colorDialog.ShowDialog();
   leadColor = new RasterColor(colorDialog.Color.R, colorDialog.Color.G, colorDialog.Color.B);
   // Define upper and lower colors that conver the entire range of Saturation and Value, but limit Hue to values close to the selectec color.
   RasterHsvColor hsvLower = new RasterHsvColor(leadColor);
   RasterHsvColor hsvUpper = new RasterHsvColor(leadColor);
   hsvUpper.H = (byte)((hsvUpper.H + 25) % 255);
   hsvLower.H = (byte)((hsvLower.H - 25 + 255) % 255);
   hsvUpper.S = 255;
   hsvUpper.V = 255;
   hsvLower.S = 0;
   hsvLower.V = 0;
   // Define region for pixels that are NOT in the color range
   _viewer.Image.AddColorHsvRangeToRegion(hsvLower, hsvUpper, RasterRegionCombineMode.SetNot);
   // Fill the region with black
   FillCommand fill = new FillCommand(RasterColor.Black);
   fill.Run(_viewer.Image);

   _viewer.Image.MakeRegionEmpty();
}


4. The MouseClick event code also fills the colors that are different from the selected color with black

Pixels not Red are converted to Black

Edited by user Friday, January 13, 2023 9:28:08 AM(UTC)  | Reason: Not specified

Amin Dodin

Senior Support Engineer
LEAD Technologies, Inc.
LEAD Logo
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.046 seconds.