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

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

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

Thank you for your reply ImageAnalyst. But I do not fully understand the concept of the save command. The matlab help says that it is used to save all of the variables in the workspace into the lab.mat file.
The thing is, I do not know how to open the .dat file I uploaded here.
This file was not generated by me or by Matlab it was done by my colleague in C++ and I have to continue the work in Matlab.
I still attempted to use your mentioned method here, but for that I had to bring the dat file into workspace some how. for that I used
file = fopen('color_test.dat');
read = fread(file);
this created a ``830280x1 double`` matrix. When using the save('lab.mat', 'labaledImage') I got this error:
??? Error using ==> save
Variable 'labeledImage' not found.
Thank you
You need to determine the format of the dat file. If it's unknown then we cannot reliably retrieve its contents. Can you open it in wordpad? Does it look like test. Can you use importdata(), csvread(), dlmread(), textscan(), or other data reading functions?
You probably used a different name for your labeled image - I didn't know what it was so I just called it that. You have to replace labeledImage with the exact name you used. For example, I took your colorized image and recreated the binary image just to get to a place like where you started. Then I labeled and colorized the image just like you did. Here is the code and image:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 13;
% Read in a standard MATLAB color demo image.
folder = 'C:\Users\Faraz\Documents\Temporary';
baseFileName = 'color_test.bmp';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Try to estimate what the original black and white image looked like
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
whitePixels = redChannel == 255 & greenChannel == 255 & blueChannel == 255;
binaryImage = ~whitePixels;
% Display the binary image.
subplot(2, 2, 2);
imshow(binaryImage);
title('Binary Image', 'FontSize', fontSize);
% Label the image;
labeledImage = bwlabel(binaryImage);
% Display the labeled image.
subplot(2, 2, 3);
imshow(labeledImage, []);
title('Labeled Image', 'FontSize', fontSize);
% Colorize it
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
% Display the labeled image.
subplot(2, 2, 4);
imshow(coloredLabels);
title('Colored Label Image', 'FontSize', fontSize);
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
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)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!