Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Using white parts of a binary image  (Read 2045 times)

0 Members and 1 Guest are viewing this topic.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Using white parts of a binary image
« on: November 28, 2012, 04:59:46 pm »
Hello everybody!
I am facing a problem, on which I would like to get some input on from some of the experts in this forum :)
I have a binary image (basically a mask) so a pixel is either black or white (Example). Now i want to get the coordinates of all the white pixel, to use them as positions for some sprites. The only thing I can think of is something like this (pseudocode):
for(x = 0; x <= width; x++)
   for(y = 0; y <= height; y++)
      if(image[x][y] == white)
          whitepixels.push_back(pair(x,y));
But this seems very chuncky and inefficient to me, because the binary image is fairly big and it's updated every frame. So my question is, is there a more efficient or optimized way to do this? I basically want to use the white parts of the binary image to draw stuff there.
Thanks in advance, Foaly :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Using white parts of a binary image
« Reply #1 on: November 28, 2012, 06:49:32 pm »
If the changes in your update step are significantly smaller than the data in the image itself, you should look there that you directly change the positions, instead of recomputing them completely.

Do you need an actual sf::Image for your black/white image? Because this wastes 97% of memory (1 color bit instead of 32 would be enough). If not, you can try the following data structures:
  • std::set<sf::Vector2i> -- good if the matrix is sparse, i.e. only few white or black pixels
  • std::vector<bool> -- saves a lot of memory
  • std::vector<sf::Uint8> with each entry being 0 or 1 -- leaves 87.5% of memory unused, but may be faster
  • std::bitset<N> or boost::dynamic_bitset<> -- saves memory, provides bitwise operations
What do you need the positions exactly for?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Using white parts of a binary image
« Reply #2 on: November 29, 2012, 02:45:58 pm »
You can create a minimal program that computes the white pixels in the image and saves this data to a file. Then you just load the positions from this file in a std::vector <sf::Vector2f>
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Using white parts of a binary image
« Reply #3 on: November 30, 2012, 02:21:14 pm »
Thanks for the quick and helpful response! I implemented the code I described above and I noticed that on average it only takes 1.8 milliseconds on an image 480x640. So I'm gonna focus on optimizing the other parts of my program first.
thanks again :)

@Nexus: Don't worry I'm not using an sf::Image. I'm using cv::Mat which automatically scales to the appropriate format. Thanks for the heads up though!

 

anything