1

Hello there and thank you for at the very least, trying to help me.

I need to, firstly, load an image and then loop through all pixels of that image and check the color of each pixel.

I have never tried messing around with images or whatnot.

Any help is greatly appreciated.

Thank you.

BartBB
  • 77
  • 8
  • What were you hoping to do with the pixels once you can access them? What Operating System are you using? – Mark Setchell Dec 08 '15 at 10:45
  • I do not need to do anything with the pixels afterwards. I just need a documentation of their color and that is it. After the loop they can go to hell for all I care. I am using Windows. Just an FYI, I am using CodeBlocks, if that matters. @MarkSetchell – BartBB Dec 08 '15 at 10:53
  • What do you mean *"a documentation of their colour"*? If your image is small at, say, 200px by 100px, you will have a list of 20,000 pixels each with its own colour - what would you want with that? – Mark Setchell Dec 08 '15 at 10:56
  • Well, the intention of this project is to know how many dots a dice shows. I have a way of doing that in mind. Probably an inefficient one, but I do have a plan and I would very much like to try it. (Unless you have a suggestion). So I want to check the color of several pixels. Pretty much.. a group of them. Like, comparing this one to the last one and to the next one to see if they're the same color. Then comparing these to the ones below and above to see if it's a group, a dot. Basically, during the loop is all the processing I do, afterwards I need nothing. @MarkSetchell – BartBB Dec 08 '15 at 11:02

3 Answers3

1

Looking at the bigger picture, of counting the dots on a dice, I would look at using ImageMagick - with the C++ binding called Magick++ from here

I would be looking at using "Blob Analysis", or "Connected Component Analysis" to count the dots on a dice.

Using this dice...

enter image description here

If I use ImageMagick at the command line like this:

convert dice.png     -colorspace gray -threshold 50% \
   -define connected-components:verbose=true         \
   -define connected-components:area-threshold=10    \
   -connected-components 8 -auto-level output.png

Output

Objects (id: bounding-box centroid area mean-color):
  0: 380x362+0+0 189.6,180.0 103867 srgba(255,255,255,1)
  2: 93x92+248+32 293.8,77.5 6743 srgba(0,0,0,1)
  4: 92x93+39+241 84.8,286.7 6741 srgba(0,0,0,1)
  5: 93x93+248+241 293.8,286.8 6738 srgba(0,0,0,1)
  1: 92x92+39+32 84.8,77.5 6736 srgba(0,0,0,1)
  3: 93x93+143+136 189.3,182.1 6735 srgba(0,0,0,1)

You can see it has found 5 dots (the first one is actually the whole, white image), and I can put a red box around each dot like this so you can see what it has found:

convert dice.png -stroke red -fill none -strokewidth 1 -draw "rectangle 248,32 341,124" -draw "rectangle 39,241 131,334" -draw "rectangle 248,241 341,334" -draw "rectangle 39,32 131,124" -draw "rectangle 143,136 236,229" result.png

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • ImageMagick isn't all that helpful (I think) because I have to do all the work myself. It's a school project. That's why all I need is a way to load an image and loop through the pixels. (And access each pixel's color) @Mark Setchell – BartBB Dec 08 '15 at 11:51
0

I think you may use OpenCV image processing library. You have a detailed manual for installing for VS2013 here: OpenCV installation for Visual Studio 2013

After you've installed it. You will get a lot of functions for image processing, including what you are looking for.

For example:

Mat inputImage = imread(filename, CV_LOAD_IMAGE_COLOR);

then: Cycle through pixels with opencv

Community
  • 1
  • 1
  • 1
    What makes you think the OP is using Visual Studio? Or Windows even? – Mark Setchell Dec 08 '15 at 10:51
  • I cannot use Visual Studio for this project. I would have if I could have. There are a lot of examples for this kind of question in OpenCV. I wish I could have used it. Thank you though! @Assaf Rabinowitz – BartBB Dec 08 '15 at 10:55
  • 1. OpenCV is cross platform. You can use it on any OS. 2. I know you can install OpenCV for Eclipse too, maybe for more IDEs. What exactly do you work with? @Mark – Assaf Rabinowitz Dec 08 '15 at 11:06
  • Maybe this will work for you: [OpenCV with CodeBlocks](http://kevinhughes.ca/tutorials/opencv-install-on-windows-with-codeblocks-and-mingw/) – Assaf Rabinowitz Dec 08 '15 at 13:06
0

One of the methods you could use, is to interpret the file as binary. If you know how to interpret the header part and know what color depth the image has (the header has such info), then it wont be a longshot to just compare binary or hexadecimal color codes - probably hexadecimal since C++ doesnt have a built in binary variable.

if you dont think you can handle binary and need a library to work with, refer to How do I read JPEG and PNG pixels in C++ on Linux?

EDIT - or just use any image processing libraries such as http://cimg.eu since the objective appears to be interpretation of dice from an image.

Community
  • 1
  • 1
Mahoraz
  • 1
  • 1
  • I've tried using cimg but I couldn't seem to get it to install properly. Did I forget to mention I'm new to all this stuff? Anyway, if you could find a guide to install it (which I couldnt find) or if you could explain how to, I would be great. Also all I want is to load an image and loop through all pixels. Nothing else. Thank you. – BartBB Dec 08 '15 at 11:33
  • Since Cimg is a header-only template library, heres what you need to do - put the files from the downloaded zip into the same directory as your project and include the header file like so #include "Cimg.h" Afterwards just look at the examples directory for instructions on how to use it. – Mahoraz Dec 08 '15 at 11:55
  • I'm not that new. I thought there was more to it. I did do that and got lots of different errors. That's why I thought I did something wrong – BartBB Dec 08 '15 at 11:57
  • The errors are saying that I am missing several header files. cstdio.h, cstdlib.h and so on. – BartBB Dec 08 '15 at 12:05
  • These are standard libraries included in a C/C++ compiler package. You should #include the said header files similar to "#include " it will throw these type of error since Cimg is using standard library functions - these arent usually added to other library files to avoid duplicate includes that would create linker errors. – Mahoraz Dec 10 '15 at 11:56