For a uni project I've been writing a program to simulate video feedback. So basically I have 2D arrays representing the 'screen' and the 'camera' and I'm copying a portion of the screen to the camera then putting the resulting 'image' back on the screen. I'm creating really basic fractals with this and I've written a piece of code to calculate approximately the fractal dimension of my resulting 'images' (the screen is initially an array of ones and zeros representing black and white, eventually I end up with decimals representing a sort of greyscale image). My tutor has said I need to check that my calculation for fractal dimension works, so I should take something I know the fractal dimension of and see if I get an answer close to what it should be. He suggested I read in a file and tested it on that. I've been googling image handling all day and I think I'm sort of starting to understand how to read in a file but I don't really know how to get the data from the file into a format that my program can use - i.e. a 2D array of doubles between 0 and 1. I'm a total beginner at C++, I asked my tutor for a hint and he said it should be relatively easy and could all be done with the standard iostream library but all the code snippets I've found on the internet that I've been trying (unsuccesfully) to amend have seemed really complicated to me! My final program doesn't even need to be able to read in files or anything - I just need to work out how to do it to test part of it. I don't really have code to include to do with the image stuff but here's what I have for fractal dimension: (it basically takes different sized 'boxes' and for each different size box it gives the average number of boxes with an average 'brightness' greater than 0.1. It then outputs this info to a textfile)

//fractal dimension calculation: 
	//screen width is 120, screen hieght is 90

	int maxheight, maxwidth, p, height, width, r, s;
	double averagesqrs, screenh=90, screenw=120, whitesqrs, numboxes, av1, avbright;

	for(p=1; p<11; p=p+1)

		for(height=0; height<maxheight; height=height+p)
			for(width=0; width<maxwidth; width=width+p)

				for(r=0; r<p; r=r+1)
					for(s=0; s<p; s=s+1)






	fractaldimension << "Box size: " << p << endl;
	fractaldimension << "Average number of >0.1 average brightness boxes: " << averagesqrs << endl;
	fractaldimension << endl;


and the image my tutor said would be a good test is the one on the top right of this webpage:

Sorry for the long and slightly muddled post! I'm just pretty confused and hoped someone could point me in the right direction? I've been googling this for hours and haven't managed to solve the problem!

Thanks in advance,


You absolutely cannot read jpg files with iostream! jpeg is a lossy format which means you need the inverse of the jpeg compression algorithm in order to read it! BMP may be possible, but you're crazy to read it yourself! My suggestion would be to use a library that has image reading capabilities. You could use VTK, VXL, and probably OpenCV to read a variety of image formats.

Good luck,


Thanks for the response! I've never used anything other than the standard libraries before - how do I get/use other libraries? Does it explain on the websites of those you've mentioned?

These are very "heavy" libraries. That is, they are DEFINITELY overkill for just reading images, but unless someone recommends something more "lightweight", it will get the job done. The one I am most comfortable with is VTK. You can download it from here
(direct link:

Once it is installed, you will have to link to vtkHybrid. Then you should be able to use the code in the example I sent above. I will actually fix that example to show you how to access individual pixels. I'll let you know when that is done.


I guess I didn't post a link? haha. Here is the code you'll need.

#include <vtkSmartPointer.h>
#include <vtkJPEGReader.h>
#include <vtkImageData.h>
int main ( int argc, char *argv[] )
  //parse command line arguments
  if ( argc < 2 )
    std::cout << "Usage: " << argv[0]
              << " InputFilename(.jpg)" << std::endl;
    return EXIT_FAILURE;
  vtkstd::string inputFilename = argv[1];
  // Read JPG file
  vtkSmartPointer<vtkJPEGReader> reader =
  reader->SetFileName ( inputFilename.c_str() );
  vtkSmartPointer<vtkImageData> image = reader->GetOutput();
  // Get dimensions of the image
  int* dims = image->GetDimensions();
  // Access elements of the image
  for (int row = 0; row < dims[1]; row++)
    for (int col = 0; col < dims[0]; col++)
      //double* pixel = static_cast<double*>(image->GetScalarPointer(row,col,0));
      //std::cout << "(" << row << " , " << col << ") = (" << pixel[0] << " , " << pixel[1] << ")";
      unsigned char* pixel = static_cast<unsigned char*>(image->GetScalarPointer(col,row,0));
      std::cout << "(" << row << " , " << col << ") = (" << int(pixel[0]) << " , " << int(pixel[1]) << ")" << std::endl;
  return EXIT_SUCCESS;

I could have row and col reversed, you'll have to check it out.


I have experience with image processing, and according to what I know about c++ libraries, OPENCV is the best one you can use for image analysis and manipulation.I use java for image processing, but i have friends working in C++ using OPENCV to obtain images from camera and save them.

Additionally if you are using a linux based system Image Magick++ is a very handy tool. I used it to read JPEG images.

Hope this helps.

OpenCV is just as heavy as VTK, so take your pick. But ImageMagick would be much more lightweight than VTK, so that is probably the best suggestion.

Thank you so much for all your kind help!

It's all sorted now!

I really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.