Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sorry if the post is a little bit long. It's not exactly a problem per se, just interested to know how other programmers would have approached this. . .

Essentially, I'm implementing an image processing program. The images are stored in a folder and are numbered for example: Image000.JPG, Image001.JPG, Image002.JPG... Imagexxx.JPG. The names of the images have a constant text in this case "Image" and an extension, in this case "JPG". These files are to be read in order i.e Image000.JPG before Image001.JPG.

1. The first solution simply provides a config file where the user inputs the "constant text"(Image) and the number of images N.This config entry needs to be modified each time the names of images change. Probably small trouble.

The program accesses each image by using a counter:

for(int i = 0; i < N; i++)

image.open("Image" + i + ".JPG")


*note the above code is just an illustration and will not compile!

2. The second simply takes the name of the folder where the images are stored and accesses the files using some system calls.

Since, the files are usually retrieved unsorted the program needs to do the sorting itself. It automatically figures out the "common text" and the likely file extensions. It works quite well and needs to run only once at the start of the program.

It's attractive because one could do OOP stuffs like:
while(!directory.end())
{
 string name = directory.next();
 image.open(name);
}


However, it seems to be complicating what is a simple problem? There is also the small overhead of creating a new class with its associated functions.

Which would you program?
Posted
Updated 7-Aug-10 14:38pm
v2

Well, the question is not whether the code is attractive or nor, rather what is the issue at hand.

Is the file names fixed, say Imagexxx.jpg ?
How do you know which files to process?

May be better solution to give the user an ability to specify a range of image files. This could be either selecting all the files then you sort it out, or the user specifies the first and the last image to be used.
 
Share this answer
 
There's no reason why you can't do both:

- hide your file iteration code behind an interface

- make your initial implementation use command line parameters or a text file to initialise itself

- later on introduce an implementation that uses a directory name and some form of heuristic to work out what it iterates over

This has the added bones that you can write the important bit of the code (assuming the image processing is the important bit) without worrying about how you're going to get the images.

Cheers,

Ash
 
Share this answer
 
Thanks Yusuf and Aescleal.

@Yusuf: the program knows which files to process by their extension (.jpg, .tif etc). the second solution seems to do what you suggest, it selects all the image files, strips out the letters from the names & sorts them out from 0000 to 01000, say.

The first solution implements a loop from i = 0 to N. It then forms the name of the image by concatenating the loop counter, the file extension & the "constant text". It's faster & simpler, but needs a parameter entry for the "constant text".

@Ash: the second approach, does everything from behind an interface.The program already has an image class, the directory class simply provides the image class with the file names in a serial fashion. It is initialised via command line parameters.

Cheers.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900