How to correctly read the data from an image .dat file?

9 views (last 30 days)
I have a .dat file created by a C++ code segment. The format of the dat file is as per these guidelines: http://users.iit.demokritos.gr/~nstam/ICDAR2013HandSegmCont/Protocol.html
This is the image file:
And here is the dat file for the above image : https://www.dropbox.com/s/q8h3psin2k67vew/color_test.dat
I can tell that the segmented words in the dat file are store in the same format as the bwlabel command would do.
But how do I get the data from the dat file into an array in Matlab for further processing? Suppose the first word is labeled with 1 and the second word with 2. How do I get the 1 to a different array and the 2 to a separate array and so on. Such that I can further work on that array such as imshow or im2bw or even regionprops?
I tried fopen and fread. They did manage to open the file but created a single column vector having 8000+ elements.
thank you

Accepted Answer

Image Analyst
Image Analyst on 7 Mar 2014
Faraz, the words are segmented, and since they're each a different color you must have labeled them with bwlabel() or bwconncomp(), and probably uses label2rgb() to colorize them. If you want, you can save that labeled image to disk with save() and recall it with load().
save('lab.mat', 'labeledImage');
then later...
s = load('lab.mat');
labeledImage = s.labeledImage;
To extract out a region with a certain number, you use ismember
oneRegion = ismember(labeledImage, theNumberYouWant);
That is still a labeled image and the region has the same number. If you want to turn it into a binary (logical) image you can threshold it.
binaryImage = oneRegion > 0;
  4 Comments
Faraz
Faraz on 9 Mar 2014
Edited: Faraz on 9 Mar 2014
Thank you Image Analyst but the segmentation via this method is not as good as it is in the original. In the original complete words are separately segmented whereas here each connected component is colored differently.
I found out from the original author that the variable which contains the segmentation itself is "words".
Now that we know the format of the .dat file( description of the dat file format ) and also the variable used to store the segments, is it possible to load it up in Matlab? ( Here is the .dat file )
I know that you mentioned that this will be possible via
save('lab.mat', words)
but to do that I have to first bring the dat file into the workspace right? How do I do that?
Thank you
Image Analyst
Image Analyst on 10 Mar 2014
You misunderstood what I was trying to show you. I was trying to show you how you can create a labeled image. I know that they created a labeled image differently than I did because somehow they grouped the nearby letters into a single label. The point is, they had a labeled image and I was showing you a way you could create your own labeled image and save your own results.
Now originally you did not know how they saved the labeled image and what the format was, though since then, in your latest comment, you learned what the format is supposed to be. If you are able to regenerate the labeled image they got somehow, then you can save it with the fwrite() function, which will get it in the same format as needed for that contest. It's not how we'd do it in MATLAB, but if you need to read and write that format, you can do so with fread() and fwrite(). You can give an array into fread to tell it how many rows and columns you want. For example, here's a snippet from my code that reads raw data. Look at the fread() to see how it reads into a 2D array:
% Read in image2D image data
% Get original, full-sized 2-D slice.
% Note: fread() requires that x_size and y_size be doubles.
% Note: fread() will return a 2D array if you pass in a 2D array for the number of bytes, as in [x_size y_size].
if stHeader.BytesPerVoxel == 1
oneSlice = fread(fileHandle, [x_size y_size], '*uint8');
elseif stHeader.BytesPerVoxel == 2
oneSlice = fread(fileHandle, [x_size y_size], '*int16'); % It will be a 2D array after this.
else
error('Unsupported BytesPerVoxel %d', stHeader.BytesPerVoxel);
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!