从目录读取图像 [英] Reading Images from a Directory

查看:77
本文介绍了从目录读取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果帖子有点长.本身并不是一个完全问题,只是想知道其他程序员将如何解决这个问题. . .

本质上,我正在实现图像处理程序.图像存储在文件夹中,并且编号为:Image000.JPG,Image001.JPG,Image002.JPG ... Imagexxx.JPG.图像的名称在此情况下具有恒定的文本"Image",在此情况下具有扩展名"JPG".这些文件应在Image001.JPG之前按顺序读取,即Image000.JPG.

1.第一种解决方案只是提供一个配置文件,用户在其中输入常量文本"(Image)和图像数N.每次图像名称更改时,都需要修改此配置项.可能是小麻烦.

该程序通过使用计数器访问每个图像:

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")



*请注意,以上代码仅是示例,不能编译!

2.第二个简单地使用存储图像的文件夹的名称,并使用一些系统调用来访问文件.

由于文件通常是按未排序的方式检索的,因此程序需要自己进行排序.它会自动找出通用文本"和可能的文件扩展名.它工作得很好,并且在程序启动时只需要运行一次.

之所以具有吸引力是因为它可以做OOP之类的东西:



*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?

推荐答案

好吧,问题不在于代码是否具有吸引力,也不在于手头的问题.

文件名是否固定,例如Imagexxx.jpg?
您怎么知道要处理哪些文件?

可能是使用户能够指定一定范围的图像文件的更好的解决方案.这可以是选择所有文件然后进行整理,或者用户指定要使用的第一个和最后一个图像.
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.


没有理由可以这样做"两者都做:

-将文件迭代代码隐藏在界面后面

-使您的初始实现使用命令行参数或文本文件来初始化自身

-稍后介绍一种使用目录名和某种启发式方法的实现,以计算迭代的内容

它具有增加的骨骼,您可以编写代码的重要部分(假设图像处理是重要的部分),而不必担心如何获取图像.

干杯,

Ash
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


感谢优素福和埃斯克莱尔.

@Yusuf:程序知道其扩展名(.jpg,.tif等)要处理的文件.第二种解决方案似乎按照您的建议进行操作,它会选择所有图像文件,从名称中去除字母并从0000到01000对其进行排序.

第一种解决方案实现了从i = 0到N的循环.然后,通过将循环计数器,文件扩展名和常量文本"连接起来,形成了图像的名称.它更快,更简单,但是需要为常量文本"输入参数.

@Ash:第二种方法,是在接口后面执行所有操作,程序已经具有图像类,目录类只是以串行方式为图像类提供文件名.通过命令行参数进行初始化.

干杯.
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.


这篇关于从目录读取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆