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. Fastest way to scan an image for pixel color (black and white)

Fastest way to scan an image for pixel color (black and white)

Scheduled Pinned Locked Moved C#
graphicsdata-structuresquestion
4 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.
  • S Offline
    S Offline
    sebogawa
    wrote on last edited by
    #1

    i have a black and white image, i need to quickly scan the entire image for white (255,255,255 RGB) pixels and add then to an array. I have a for loop in a for loop to do this as follows: <pre private void trackLane()             {                   Bitmap leftLane = new Bitmap(detectLeft.Image);                   Bitmap rightLane = new Bitmap(detectRight.Image);                   for (int i = 0; i <= detectLeft.Width; i++)                   {                         for (int j = 0; j <= detectLeft.Height; j++)                         {                               leftLane.GetPixel(i, j);                               rightLane.GetPixel(i, j);                         }                   }                            } ></pre> Where j is the y axis and i is the x axis, i think there is a more efficient way of doing this, any suggestions?

    L 1 Reply Last reply
    0
    • S sebogawa

      i have a black and white image, i need to quickly scan the entire image for white (255,255,255 RGB) pixels and add then to an array. I have a for loop in a for loop to do this as follows: <pre private void trackLane()             {                   Bitmap leftLane = new Bitmap(detectLeft.Image);                   Bitmap rightLane = new Bitmap(detectRight.Image);                   for (int i = 0; i <= detectLeft.Width; i++)                   {                         for (int j = 0; j <= detectLeft.Height; j++)                         {                               leftLane.GetPixel(i, j);                               rightLane.GetPixel(i, j);                         }                   }                            } ></pre> Where j is the y axis and i is the x axis, i think there is a more efficient way of doing this, any suggestions?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, here are some ideas: 1. j <= detectLeft.Height gets executed for every pixel, you'd better store detectLeft.Height in a local int variable 2. executing a lot of GetPixel() calls tends to be slow since each time the coordinates are checked against the image boundaries, and translated into a linear index, something that can be done more easily once one knows they all get handled sequentially 3. the most practical approach to (2) is by using pointers 4. why do you use 4 bytes per pixel if all it can be is black and white; an array (bools, bytes, ints, ...) or a BitArray would be more economical in space, and hence in speed (assuming your image is say 512*512, it takes 1MB which is about the size of the level 2 cache); less data would run faster. Maybe you should swith to one byte per pixel (optimum between density and simplicity) 5. there are special code snippets to find the lowest bit set in an integer, this could be much faster assuming a binary image (yours is), and a majority of zeroes. x & -x results in zero if x is zero, and in an int with exactly one bit set which is also set in x otherwise. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      S 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, here are some ideas: 1. j <= detectLeft.Height gets executed for every pixel, you'd better store detectLeft.Height in a local int variable 2. executing a lot of GetPixel() calls tends to be slow since each time the coordinates are checked against the image boundaries, and translated into a linear index, something that can be done more easily once one knows they all get handled sequentially 3. the most practical approach to (2) is by using pointers 4. why do you use 4 bytes per pixel if all it can be is black and white; an array (bools, bytes, ints, ...) or a BitArray would be more economical in space, and hence in speed (assuming your image is say 512*512, it takes 1MB which is about the size of the level 2 cache); less data would run faster. Maybe you should swith to one byte per pixel (optimum between density and simplicity) 5. there are special code snippets to find the lowest bit set in an integer, this could be much faster assuming a binary image (yours is), and a majority of zeroes. x & -x results in zero if x is zero, and in an int with exactly one bit set which is also set in x otherwise. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


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

        Thanks, I'll keep working on it. : )

        A 1 Reply Last reply
        0
        • S sebogawa

          Thanks, I'll keep working on it. : )

          A Offline
          A Offline
          Alan Balkany
          wrote on last edited by
          #4

          This link describes the fast way to access pixels in C#: http://www.bobpowell.net/lockingbits.htm[^]

          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