Experience of MATLAB reading and storing images in batches from other folders

Source: Internet
Author: User

I did a computer vision task today and found that a large number of images need to be imported. I have not done any work before, but I think it is not as simple as I think, this is especially true for entry-level MATLAB apprentices like me.
First, let's talk about the requirement of the question. the mat file of Mat contains a train structure, which contains a cell array of files. Each element of the cell array is the file name of the image to be imported. 1:

We can see that a total of 198 images need to be read and stored. In actual tasks, these image storage paths are often different from the current working path. Therefore, the first problem to be solved is how to read an image from another path.
For example, if you want to read an image with the editing number image0011.jpg
You may think of this.
Im = imread ('d: \ firstsemester \ computervision \ homework \ hw2 \ hw2 \ handout \ data \ image00111.jpg ')
If the format is image0011.jpg, which is currently in your workshop, it is correct. Because it is not in the current workshop, You need to reference the cell elements in files for the same job. The reason is that figure 1 shows the first element of the cell element, such as image0011.jpg. Therefore, let's write the read Statement as follows:
Im = imread ('d: \ firstsemester \ computervision \ homework \ hw2 \ hw2 \ handout \ data \ train. Files {1 }')
The meaning of this sentence is that the first element image0011.jpg in the filesof the cell phone group is used to import images. However, an error is returned, indicating that the file cannot be found.
What is the cause of the error? The reason is that the result of the cell array element applied by train. Files {1} is enclosed by single quotation marks.
Im = imread ('d: \ firstsemester \ computervision \ homework \ hw2 \ hw2 \ handout \ data \ tables, so an error occurs. The solution is as follows. At first we wanted to remove two single quotes by deleting strings, but we found that this was very troublesome and unnecessary. Because there is another way to write imread,

Imread (['pathname', file name]);

 

This method can solve the problem. I write the read statement in this way.

 

Imread (['d: \ first semester \ computervision \ homework \ hw2 \ hw2 \ handout \ data \ ', train. Files {1}]);

 

In this way, the program can smoothly read the desired image from the specified path using the struct pointer.

 

The last step is to import multiple images. Because the images to be read are all in color (3D), we wanted to use a 4-dimensional array for reading, but because the image dimensions were not uniform, therefore, we finally decided to use a convenient cell matrix for reading. The advantage of cell matrix is that you can store data without the restriction of dimension or cell type. You can store the structure charts, functions, matrices, and floating points together into the cell matrix, and it is very convenient to take out.

 

The read statement is as follows. First, determine the length of the cell matrix for storing Multiple Graphs by determining the length of the files cell array, and then read all the images from other paths cyclically.

 

Rochelle files = length (train. files );

 

For I = 1: l_files

 

Imgsource {I} = imread (['d: \ firstsemester \ computervision \ homework \ hw2 \ hw2 \ handout \ data \ ', train. Files {I}]);

 

End

 

 

After reading it, imgsource will become a 1*198 cell matrix. Each element of imgsource corresponds to a color three-channel image, as shown in figure 2.

Finally, we can select the desired color image in the form of imgsource {}. For example, if I want to display 5th images now, I can use imshow (imgsource {5 }) statement (Figure 3 ). Of course, if you store a two-dimensional matrix, you can use imgsource {}{} to index the elements.

The above are some of my understandings and ideas on batch graph reading and cell matrix. If you have any shortcomings, please kindly advise.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.