Building a custom image reader function in MATLAB

10 views (last 30 days)
I want to build a custom image reader function in MATLAB without using inbuilt functions like imread to read different image types (bmp,jpg,png etc..)
Can someone help me to get started with this and point me to the resources which I need to learn (for eg. how to read the file pointer of the image)
Thanks for you help in advance!

Answers (2)

Jan
Jan on 6 Sep 2021
Edited: Jan on 6 Sep 2021
It is nearly impossible to write any useful code in Matlab without using builtin functions. See https://www.mathworks.com/matlabcentral/answers/38787-what-can-be-programmed-without-any-built-in-functions
Reading BMP, JPG, PNG and "etc." (please do not let us guess, what you want to do) needs a huge program. Nobody would implement such tools by his own, because there are well tested libraries for this topic, which care for all different standards. Even Matlab's imread does not perform the import by its own, but Matlab containts the needed DLLs.
It is a bad idea to start programming with such a complex project, which might take several years for a professional programmer. You cannot expect, that the forum delivers a list of ISO/IEC papers, which contain the format sepcifications of all usual image files. Start e.g. here: https://en.wikipedia.org/wiki/JPEG
What is a "file pointer of the image"?
  3 Comments
Aravind S
Aravind S on 6 Sep 2021
Thank you for your response.
I don't think I'll be required to handle all the types of bmp images (color table mapping isn't necessary). Just reading the stock images in MATLAB (Cameraman/Seville) converted to bmp and grayscale format using imsave and im2gray will work.
I've gone through the link you sent earlier as a part of prepartory reading material for this but I'm clueless on where and how to start with writing the function for it.
Any help would be highly appreciated.
Jan
Jan on 6 Sep 2021
@A: File pointers are not specific for the file type. So the general method is:
[fid, msg] = fopen(Filename, 'r');
assert(fid > 0, msg);
...
fclose(fid);
Reading all kinds of BMP files is a really tough project for a beginner. Because this will take some thousands lines of code, you need toget some programming skills at first. Learn how to divide the problem into parts, which can be implemented and tested separately. Without reliable parts, the complete program cannot work reliably. Decide for a testing strategie and a version management. Working in a team would be a useful idea also, if you want to finish the problem in less than a year. Set some milestones and learn how to manage a project.
In the original question you ask for "bmp,jpg,png etc.". Now it is BMP only, but even this is a demanding project already. Please note, that asking too general questions wastes the time of you and the ones, who post answers.
Compared with the complexity of the complete problem, asking for a file pointer is confusing. Take into account that your professor showed a decent level of humor. Ask her or him for an exact definition of the job.

Sign in to comment.


Walter Roberson
Walter Roberson on 6 Sep 2021
You need to know the following functions:
  • fopen() the image file for reading. For your purpose, the default "permission", 'r' is fine
  • fseek() to move to a particular location in the file, or to a certain relative location
  • fread() to read data from the file. Much of the time you should be reading with "precision" given as *uint8 . Be sure to put in the size parameter too
  • fclose() after you are done with the file
  • you might find that you also need ftell() to find out where you are in the file.
So generally you would fseek() to a particular position in the file, and fread() several bytes, and then interpret them. That might give you size information that would inform the location you need to fseek() to next.
There will be places where there are multi-byte numeric values in the file. Be careful with those. It is tempting to fread() them using (for example) *uint32 for a 32 bit integer. However, the byte arrangement of numbers in the file is possibly Most Significant Byte first, similar to reading off a number in decimal, in which the first value encountered reading from the left is the value that affects the magnitude the most. If it is MSB numbers, then beware that reading with *uint32 uses the order called LSB, Least Significant Byte -- sort of like reading a decimal number backwards. EIther read the bytes separately and put them together, such as
sum(double(bytes) .* 256.^(3:0))
... or use swapbytes()

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!