Opencv Reading Multiple images from Multiple folder - opencv

I have a folder which contain 100 or more sub folders and each of them contain many images.(Caltech Database)
I have problem with how to read them from the different folders ?
I want to store them as a single matrix with stacking columms of each image.

Use Boost Filesystem for C++. You can load all files in a directory and pass the file location to OpenCV in a string.
string folder = "../images/";
vector<string> imageFileLocations;
namespace fs = boost::filesystem;
vec v;
copy(fs::directory_iterator(folder), fs::directory_iterator(), back_inserter(v));
sort(v.begin(), v.end());
for (vec::const_iterator it(v.begin()); it != v.end(); ++it) {
if (fs::is_regular_file(*it)) {
string location = it->string();
imageFileLocations.push_back(location);
}
}
You'll have to add something recursive to be able to go into other folders. You can do that by checking if the iteraotr is at a file or a folder. See the Boost website for examples.

Related

How to batch process ImageJ macro within a specific folder within multiple directories

I have a nested file structure where a parent folder contains multiple folders of different types of data. I am using an ImageJ macro script to batch process all of the image files within one of those folders. I currently need to process each folder separately, but I would like to batch process over the folders. I have looked up some batch processing of multiple folders, but it appears that the code is processing all folders and files within all of the folders. I only need to process one folder within each directory (all named the same). The images come from the instrument without any metadata, so the files are saved as such to separate the experiments, where all data for the experiment is contained within the parent folder. Also, I have two different scripts that I need to run, one after the other. It would be great if I could merge those, but I am don't know how to do that either.
An example of the structure is:
Experiment1/variable1/processed
Experiment1/variable2/processed
I am currently running my macro on each of the "processed" folders individually. I would like to batch each "processed" folder within each of the "variable" folders.
Any help would be greatly appreciated, I am really new to coding and am just really trying to learn and automate as much as possible.
Thank you!
Did you try the batch processing scripts you came across? Reading the batch processing example which is provided with ImageJ leads me to believe it would work for your example. If you haven't tested it, you should do so (you can put in a command like "print(list[i])" in the place of your actual macro while you test that you've got the file finding section working.
To merge two different scripts, the simplest option would be to make them individual functions. i.e.:
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
processOtherWay(input, output, list[i]);
}
}
function processFile(input, output, file) {
// Do the processing here by adding your own code.
// Leave the print statements until things work, then remove them.
print("Processing: " + input + File.separator + file);
print("Saving to: " + output);
}
function processOtherWay(input, output, file) {
// Do the processing here by adding your own code.
// Leave the print statements until things work, then remove them.
print("Processing: " + input + File.separator + file);
print("Saving to: " + output);
}
If the goal isn't to run them on the exact same image, then again make them standalone functions, and have the folder sorting section of the script be in two parts, one for function 1, one for function 2.
You can always just take the code you have and nest it in another for loop or two.
numVariables = ;//number of folders of interest
for(i = 1; i <= numVariables; i++) //here i starts at 1 because you indicated that is the first folder of interest, but this could be any number
{
openPath = "Experiment1/variable" + i + "/processed";
files = getFileList(openPath);
for(count = 0; count < files.length; count++) //here count should start at 0 in order to index through the folder properly (otherwise it won't start at the first file in the folder)
{
//all your other code, etc.
}
}
That should just about do it I think.

How to zip PDF and XML files which are in memorystreams

I'm working with VS2015 and ASP.Net on a webservice application which is installed in the AWS cloud.
In one of my methods i got two files, a PDF and a XML.
These files just exist as instances of type MemoryStream.
Now i have to compress these two "files" in a ZIP file before adding the zip as attachment to an E-mail (class MailMessage).
It seems that i have to save the memorystreams to files before adding them as entries to the zip.
Is ist true or do i have another possibility to add the streams as entries to the zip?
Thanks in advance!
The answer is no.
It is not necessary to save the files before adding them to the stream for the ZIP file.
I have found a solution with the Nuget package DotNetZip.
Here is a code example how to use it.
In that example there two files which only exist in MemoryStream objects, not on a local disc.
It is important to reset the Position property of the streams to zero before adding them to the ZIP stream.
At last i save the ZIP stream as a file in my local folder to control the results.
//DotNetZip from Nuget
//http://shahvaibhav.com/create-zip-file-in-memory-using-dotnetzip/
string zipFileName = System.IO.Path.GetFileNameWithoutExtension(xmlFileName) + ".zip";
var zipMemStream = new MemoryStream();
zipMemStream.Position = 0;
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
{
textFileStream.Position = 0;
zip.AddEntry(System.IO.Path.GetFileNameWithoutExtension(xmlFileName) + ".txt", textFileStream);
xmlFileStream.Position = 0;
zip.AddEntry(xmlFileName, xmlFileStream);
zip.Save(zipMemStream);
// Try to save the ZIP-Stream as a ZIP file. And suddenly: It works!
var zipFs = new FileStream(zipFileName, FileMode.Create);
zipMemStream.Position = 0;
zipMemStream.CopyTo(zipFs);
zipMemStream.WriteTo(zipFs);
}

How to download all files in an Azure Container Directory?

I have an aspnet app which i upload files to the azure blobs. I know that azure don't create structural paths in the containers, just blobs, but you can emulate directories putting a "/" on the uri.
i.e
I'd upload a list of files and my uri is like this
http://myaccount.windowsazure.blob.net/MyProtocolID-01/MyDocumentID-01/FileName01.jpg
http://myaccount.windowsazure.blob.net/MyProtocolID-01/MyDocumentID-01/FileName02.jpg
http://myaccount.windowsazure.blob.net/MyProtocolID-01/MyDocumentID-01/FileName03.jpg
My download method:
public RemoteFile Download(DownloadRequest request)
{
var fileFinal = string.Format("{0}/{1}/{2}",request.IDProtocol ,request.IDDocument, request.FileName);
var blobBlock = InitializeDownload(fileFinal);
if (!blobBlock.Exists())
{
throw new FileNotFoundException("Error");
}
var stream = new MemoryStream();
blobBlock.DownloadToStream(stream);
return File(request.FileName)
}
private CloudBlob InitializeDownload(string uri)
{
var blobBlock = _blobClient.GetBlobReference(uri);
return blobBlock;
}
This way, i'm getting just one file. But i need to see and download all files inside http://myaccount.windowsazure.blob.net/MyProtocolID-01/MyDocumentID-01/
Thanks
Adding more details. You will need to use one of the listing APIs provided by the client library: CloudBlobContainer.ListBlobs(), CloudBlobContainer.ListBlobsSegmented(), and CloudBlobContainer.ListBlobsSegmentedAsync() (and various overloads.). You can specify the directory prefix, and the service will only enumerate blobs matching the prefix. You can then download each blob. You may also want to look at the ‘useFlatBlobListing’ argument, depending on your scenario.
http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblobcontainer.listblobs.aspx
In addition AzCopy (see http://blogs.msdn.com/b/windowsazurestorage/archive/2012/12/03/azcopy-uploading-downloading-files-for-windows-azure-blobs.aspx) also supports this scenario of downloading all blobs in a given directory path.
Since each blob is a separate web resource, function above will download only one file. One thing you could do is list all blobs using the logic you are using and then download those blobs on your server first, zip them and the return that zip file to your end user.
Use AzCopy functionalities, now, it has a lot of supports.
https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-v10

read file from res folder blackberry

I want to read file from "res" folder on blackberry. The file that i used is a file javascript.
I used this code InputStream in = classs.getResourceAsStream("file.js");. But i get "could not find this path" and I use also
String srcFile = "/res/ressourcesWeb/file.js";
FileConnection srcConn = (FileConnection) Connector.open(srcFile, Connector.READ);
InputStream in = srcConn.openInputStream();
but i got an exception.
Can any one help me to read the file and give me the right path that should I use?
Your res folder has to be inside src folder to be accessed from your code.
src folder is the root folder of your project package. And all folders outside of src folder are invisible for the code at runtime.
Check this post for more details: Blackberry runtime error: FRIDG: could not find img/logo.png
There's file location principle described.
You actually do not need to put your resources under the src folder for them to be accessible from your code.
That is one way to do it, but I don't think it's the best way. Files under the src folder should really be source code, not images, or other resources. For JavaScript resources, it's debatable whether those should be under src or not. Most projects I've seen have used the src folder for only Java source code.
In any case, if you would like to keep your file (or other resources, like images) outside the src folder, you can do so. The BlackBerry plugin for Eclipse actually sets it up like this by default, when you create a new project. There is a res folder at the top level, next to (not under) src.
If you have
src\
src\com\mycompany\myapp\
res\
res\resourcesWeb\
res\resourcesWeb\file.js
Then, you can open the file like this:
String jsPath = "/resourcesWeb/file.js";
InputStream input = getClass().getResourceAsStream(jsPath);
byte [] content = IOUtilities.streamToBytes(input);
String contentAsString = new String(content);
P.S. You also can probably do this:
String jsPath = "/file.js";
InputStream input = getClass().getResourceAsStream(jsPath);
and not specify the path to the resource. Obviously, this will only work if there are no naming conflicts in your resource folders (e.g. you don't have /res/resourcesWeb/file.js and also /res/otherPath/file.js)

Blackberry res folder naming convention

I created a static function like this.
public static Bitmap Bitmap(String path) {
Bitmap bitmap = Bitmap
.getBitmapResource(Display.getWidth() + "/" + path);
System.out.println(Display.getWidth() + "" + path);
return bitmap;
}
However, when I called like this,
private Bitmap download = Config_GlobalFunction.Bitmap("btn_download.png");
The output gave me FRIDG could not find 320/btn_download.png.
In my res folder, I got an folder which was img and inside img got 6 different folders which were 160, 240, 320, 360, 480 and 640 folder.
How can I call correct folder's image based on Display.getWidth()?
It is possible to have a folder hierarchy under the /res folder but you must use getClass().getResourceAsStream(path) rather than Bitmap.getBitmapResource() in order to create your resource.
This example creates a Bitmap from the path /res/img/hi_res/ui/action_arrow.png:
String imagePath = "/img/hi_res/ui/action_arrow.png";
InputStream is = getClass().getResourceAsStream(imagePath);
byte[] imageBytes = IOUtilities.streamToBytes(is);
Bitmap b = Bitmap.createBitmapFromBytes(imageBytes, 0, imageBytes.length, 1);
It's a bit more work but it does mean you can have a nice folder structure, rather than hundreds of images lumped together in a single folder.
I've had problems with this before. BlackBerry apps don't seem to be well setup to handle resource files in subfolders. That is, you can store your resources in folders, but when bundled into your app (.cod file), they will essentially all be dumped into the same folder.
As you can see, that causes problems if you have multiple resources with the same name (but in different folders).
I used to use the Netbeans IDE to build BlackBerry apps, and with the Netbeans BlackBerry plugin, it seemed to handle this. But, with the RIM JDE, or Eclipse plugin, it doesn't. Perhaps it's something in the ant build script behind the toolset?
Anyway, I know you would like to do something similar to Android, where you would have:
res/drawable-hdpi/icon.png
res/drawable-mdpi/icon.png
res/drawable-xhdpi/icon.png
and pick the correct version of icon.png based on screen size / resolution. That's a good idea.
However, for simplicity, I would probably recommend changing your system to just use prefixes on your resource names, instead of folders. It's a pain, I know, but BlackBerry seems to handle it better.
So, just call your images:
res/img/320_btn_download.png
res/img/360_btn_download.png
res/img/480_btn_download.png
and then your code can be:
public static Bitmap Bitmap(String path) {
return Bitmap.getBitmapResource(Display.getWidth() + "_" + path);
}
if u want to get images depending on their resolutions then...give names to the images according to its resolution like 320x240_img1, 360x480_img1. no need to place these images in different folders....dump these images in ur res folder and call like this
int x = Display.getWidth();
int y = Display.getHeight();
String xx = Integer.toString(x);
String yy =Integer.toString(y);
_encImg = EncodedImage.getEncodedImageResource(xx+"x"+yy+".jpg");

Resources