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. WPF
  4. Problems converting color image to grayscale [modified]

Problems converting color image to grayscale [modified]

Scheduled Pinned Locked Moved WPF
comhelp
5 Posts 3 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.
  • L Offline
    L Offline
    Lenquist
    wrote on last edited by
    #1

    Hi everyone, it's my first post so be gentle :). I'm having difficulties converting a colored image to grayscale in a silverlight application. Clicking the link you can see the results of my code (left is the original pic right the resulting grayscale pic): Link I have no idea what I'm doing wrong. It should work but it doesn't. Help would be appreciated! One other thing, the WriteableBitmap.setPixel and WriteableBitmap.getPixel functions are part of the WritableBitmapEx library downloaded from Codeplex. Here's the code:

    private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
    {
    convertToGrayscale(new WriteableBitmap((BitmapSource)this.Original.Source));
    }

    	public void convertToGrayscale(WriteableBitmap image)
    	{
    		var p = image.Pixels;		
    		
    		for(int y=0; y
    

    modified on Sunday, September 26, 2010 8:09 AM

    A P 2 Replies Last reply
    0
    • L Lenquist

      Hi everyone, it's my first post so be gentle :). I'm having difficulties converting a colored image to grayscale in a silverlight application. Clicking the link you can see the results of my code (left is the original pic right the resulting grayscale pic): Link I have no idea what I'm doing wrong. It should work but it doesn't. Help would be appreciated! One other thing, the WriteableBitmap.setPixel and WriteableBitmap.getPixel functions are part of the WritableBitmapEx library downloaded from Codeplex. Here's the code:

      private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
      {
      convertToGrayscale(new WriteableBitmap((BitmapSource)this.Original.Source));
      }

      	public void convertToGrayscale(WriteableBitmap image)
      	{
      		var p = image.Pixels;		
      		
      		for(int y=0; y
      

      modified on Sunday, September 26, 2010 8:09 AM

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      Well - could be something wrong with the algorithm. Try byte gray = (byte)((c.R + c.G + c.B)/3); instead. See here.

      The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.

      L 1 Reply Last reply
      0
      • A Abhinav S

        Well - could be something wrong with the algorithm. Try byte gray = (byte)((c.R + c.G + c.B)/3); instead. See here.

        The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.

        L Offline
        L Offline
        Lenquist
        wrote on last edited by
        #3

        Already tried few different formulas (the one you proposed also) for calculating a gray pixel always with the same (bad looking) results.

        1 Reply Last reply
        0
        • L Lenquist

          Hi everyone, it's my first post so be gentle :). I'm having difficulties converting a colored image to grayscale in a silverlight application. Clicking the link you can see the results of my code (left is the original pic right the resulting grayscale pic): Link I have no idea what I'm doing wrong. It should work but it doesn't. Help would be appreciated! One other thing, the WriteableBitmap.setPixel and WriteableBitmap.getPixel functions are part of the WritableBitmapEx library downloaded from Codeplex. Here's the code:

          private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
          {
          convertToGrayscale(new WriteableBitmap((BitmapSource)this.Original.Source));
          }

          	public void convertToGrayscale(WriteableBitmap image)
          	{
          		var p = image.Pixels;		
          		
          		for(int y=0; y
          

          modified on Sunday, September 26, 2010 8:09 AM

          P Offline
          P Offline
          Paul Daniel Ruston
          wrote on last edited by
          #4

          Hi Lenquist Ran some tests (i.e. setting the pixel to the returned GetPixel with no modification) and the image changed. Turns out there is a problem with the GetPixel in the extensions library, which has been fixed. You need to go to http://writeablebitmapex.codeplex.com/SourceControl/list/changesets and download the latest source and compile it yourself.

          L 1 Reply Last reply
          0
          • P Paul Daniel Ruston

            Hi Lenquist Ran some tests (i.e. setting the pixel to the returned GetPixel with no modification) and the image changed. Turns out there is a problem with the GetPixel in the extensions library, which has been fixed. You need to go to http://writeablebitmapex.codeplex.com/SourceControl/list/changesets and download the latest source and compile it yourself.

            L Offline
            L Offline
            Lenquist
            wrote on last edited by
            #5

            It works now, thanks for your answer!

            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