Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to get color of pixel at mouse click in live video control

How to get color of pixel at mouse click in live video control

Scheduled Pinned Locked Moved C#
graphicsquestiontutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sebogawa
    wrote on last edited by
    #1

    I have a program which has a control that gets live web cam video feed and plays it on a rectangle in the main form. I have the following code for the control "Mouse_Click" event:

    private void videoSourcePlayer_MouseClick(object sender, MouseEventArgs e)
    {
    txtMouseX.Text = e.X.ToString();
    txtMouseY.Text = e.Y.ToString();
    }

    i need to convert the e.X and e.Y to integer format, and use those values in the getPixel function of the bitmap image taken from the video source player in the following code:

    private void videoSourcePlayer_NewFrame( object sender, ref Bitmap image )
    {

                bool showOnlyObjects = onlyObjectsCheck.Checked;
    
                Bitmap objectsImage = null;
    
               
                
                    objectsImage = image;
                    colorFilter.ApplyInPlace( image );
                
    
                // lock image for further processing
                BitmapData objectsData = objectsImage.LockBits( new Rectangle( 0, 0, image.Width, image.Height ),
                    ImageLockMode.ReadOnly, image.PixelFormat );
    
                // grayscaling
                UnmanagedImage grayImage = grayscaleFilter.Apply( new UnmanagedImage( objectsData ) );
    
                // unlock image
                objectsImage.UnlockBits( objectsData );
    
                // locate blobs 
                blobCounter.ProcessImage( grayImage );
                Rectangle\[\] rects = blobCounter.GetObjectRectangles( );
    
                if ( rects.Length > 0 )
                {
    
                
                    Rectangle objectRect = rects\[0\];
                    
                    Graphics g = Graphics.FromImage( image );
                    
                    using ( Pen pen = new Pen( Color.FromArgb( 160, 255, 160 ), 3 ) )
                    {
                        //draw something here
                    }
    
                    g.Dispose( );
    
           
                }
             }
    

    how do i get the mouse x and y coordinate values from the first function into the second function so i can use the "image.getPixel()" call?

    H S 2 Replies Last reply
    0
    • S sebogawa

      I have a program which has a control that gets live web cam video feed and plays it on a rectangle in the main form. I have the following code for the control "Mouse_Click" event:

      private void videoSourcePlayer_MouseClick(object sender, MouseEventArgs e)
      {
      txtMouseX.Text = e.X.ToString();
      txtMouseY.Text = e.Y.ToString();
      }

      i need to convert the e.X and e.Y to integer format, and use those values in the getPixel function of the bitmap image taken from the video source player in the following code:

      private void videoSourcePlayer_NewFrame( object sender, ref Bitmap image )
      {

                  bool showOnlyObjects = onlyObjectsCheck.Checked;
      
                  Bitmap objectsImage = null;
      
                 
                  
                      objectsImage = image;
                      colorFilter.ApplyInPlace( image );
                  
      
                  // lock image for further processing
                  BitmapData objectsData = objectsImage.LockBits( new Rectangle( 0, 0, image.Width, image.Height ),
                      ImageLockMode.ReadOnly, image.PixelFormat );
      
                  // grayscaling
                  UnmanagedImage grayImage = grayscaleFilter.Apply( new UnmanagedImage( objectsData ) );
      
                  // unlock image
                  objectsImage.UnlockBits( objectsData );
      
                  // locate blobs 
                  blobCounter.ProcessImage( grayImage );
                  Rectangle\[\] rects = blobCounter.GetObjectRectangles( );
      
                  if ( rects.Length > 0 )
                  {
      
                  
                      Rectangle objectRect = rects\[0\];
                      
                      Graphics g = Graphics.FromImage( image );
                      
                      using ( Pen pen = new Pen( Color.FromArgb( 160, 255, 160 ), 3 ) )
                      {
                          //draw something here
                      }
      
                      g.Dispose( );
      
             
                  }
               }
      

      how do i get the mouse x and y coordinate values from the first function into the second function so i can use the "image.getPixel()" call?

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      sebogawa wrote:

      how do i get the mouse x and y coordinate values from the first function into the second function

      Short answer, you can't! Judging from its signature, your videoSourcePlayer_NewFrame method is an event handler for the video player you are using. Its parameters are fixed and interfering with then will break the code. One possible solution is to make the UnmanagedImage grayImage declaration a class field (Move the declaration outside the handler), then in your MouseClick handler do something like

      private void videoSourcePlayer_MouseClick(object sender, MouseEventArgs e)
      {
      txtMouseX.Text = e.X.ToString();
      txtMouseY.Text = e.Y.ToString();

        // Either
            Color selectedColor = grayImage.GetPixel(e.X, e.Y);
            // do stuff with selectedColor here
        // OR
            SomeMethodThatUsesTheColor(grayImage.GetPixel(e.X, e.Y));
      

      }

      Your biggest problem might turn out to depend on how quickly the grayImage changes.

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      1 Reply Last reply
      0
      • S sebogawa

        I have a program which has a control that gets live web cam video feed and plays it on a rectangle in the main form. I have the following code for the control "Mouse_Click" event:

        private void videoSourcePlayer_MouseClick(object sender, MouseEventArgs e)
        {
        txtMouseX.Text = e.X.ToString();
        txtMouseY.Text = e.Y.ToString();
        }

        i need to convert the e.X and e.Y to integer format, and use those values in the getPixel function of the bitmap image taken from the video source player in the following code:

        private void videoSourcePlayer_NewFrame( object sender, ref Bitmap image )
        {

                    bool showOnlyObjects = onlyObjectsCheck.Checked;
        
                    Bitmap objectsImage = null;
        
                   
                    
                        objectsImage = image;
                        colorFilter.ApplyInPlace( image );
                    
        
                    // lock image for further processing
                    BitmapData objectsData = objectsImage.LockBits( new Rectangle( 0, 0, image.Width, image.Height ),
                        ImageLockMode.ReadOnly, image.PixelFormat );
        
                    // grayscaling
                    UnmanagedImage grayImage = grayscaleFilter.Apply( new UnmanagedImage( objectsData ) );
        
                    // unlock image
                    objectsImage.UnlockBits( objectsData );
        
                    // locate blobs 
                    blobCounter.ProcessImage( grayImage );
                    Rectangle\[\] rects = blobCounter.GetObjectRectangles( );
        
                    if ( rects.Length > 0 )
                    {
        
                    
                        Rectangle objectRect = rects\[0\];
                        
                        Graphics g = Graphics.FromImage( image );
                        
                        using ( Pen pen = new Pen( Color.FromArgb( 160, 255, 160 ), 3 ) )
                        {
                            //draw something here
                        }
        
                        g.Dispose( );
        
               
                    }
                 }
        

        how do i get the mouse x and y coordinate values from the first function into the second function so i can use the "image.getPixel()" call?

        S Offline
        S Offline
        sebogawa
        wrote on last edited by
        #3

        Thank you for the help Henry. I was wondering if the videosourcePlayer_NewFrame can inherit the videoSourcePlayer_MouseClick values and apply them to the _NewFrame block? I have been playing around with basic inheritance but involves only classes. I stunbled upon a C# lesson involving polymorphism (inheriting from a method) at the following link: http://www.softsteel.co.uk/tutorials/cSharp/lesson14.html[^] Do you think this will help? If not then how can i solve my problem, detaching the greyimage declaration and applying it to mouse click does nothing due to the fact that the greyImage can not be changed. I am trying to make it so that when i click on the live video feed control, it takes the ARGB values of the pixel clicked on (plus or minus 30) and apply it to the ARGB filter textboxes which saturates the image to only show the given color. (color tracking) the purpose of this app is for a robot that i am working on that tracks while traffic lines, 3 inches thick which the robot must stay in between. I am using webcams, and the AForge library for video processing and I scrapped together pieces of the "Lego Pan and Tilt App" from a Code Project Article. The app works but i have to manually set all the ARGB ranges to find the color i want. It would be easier to just click on the image and get the values. I hope this information helps, im stuck and at a loss for ideas. Any help would be greatly appreciated. :-D

        modified on Tuesday, April 21, 2009 3:11 PM

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups