Reading a PGM file in Matlab - image

I'm trying to run the command:
[face,MAP]=imread('face1.pgm');
but I get an empty array for MAP. I was able to successfully read the file data into face though.

PGM is a grayscale image format. There is no colormap. Were you expecting one? The normal behavior for imread is to return an empty colormap if the file didn't contain one.

Related

Octave, display image received by socket connection does't show

I have an octave script in which i open a socket server an receive some commands from connected clients. This already works. Now i need to send data to Octave, mostly images and process them. To test this i wanted to receive and display a grayscale test image.
bufflen = 4096;
[data,count]=recv(b,bufflen);
imshow (data)
the image window opens but it is empty. The size of data is exactly the size of the image file i am sending. I also tried saving the image with
imwrite (data, "test.jpg");
this produced a file but every line of the image was in one long line. When i open the image with
imshow test.jpg
everything works as it should, the image window appears and shows the image.
I am sending the data via netcat with
>ncat.exe 127.0.0.1 12346 < test.jpg
this seems to work, i was able to transfer the image to another pc and view it there.
Every hint or tip is greatly appreciated, thank you.
You are sending your jpeg as byte stream (ncat.exe 127.0.0.1 12346 < test.jpg) but you are trying to show is with imshow as it would be an uncompressed RGB, grayscale or index image. You can receive it and save it to a tempfile and then load it with imread. In this case graphics/image-magick will do the uncompression from JPE to RGB to you.
Guessing here since you didn't provide much information, but it sounds like you're data is coming as a vector and you need to reshape it into an array for imshow
>> newdata = reshape(data, 64, 64)
You haven't shown us an example of the input data, so it is also possible that your data is a string of characters, while image arrays need to be numerical values. To verify before reshaping you could run:
>> class(data)
If so you will need to convert it to an array of numerical values. You can use str2num for that, but exactly how to do so will depend on what the string looks like, are there value separators, etc.
See:
https://www.gnu.org/software/octave/doc/interpreter/String-Conversions.html

Save uint16 tiff image as truecolor with Matlab

I am processing microscopy images (in Matlab) in the tiff format, normally uint8 or uint16. Basically I read them, put them in a cell array for processing and then export them in the tiff format either as an image sequence or a stack (using imwrite and either the 'overwrite' or 'append' writemode property of imwrite, respectively). Up to now everything works very well.
The problem I'm having is the following:
When I open the images with ImageJ, they are not in truecolor "RGB" color mode, but rather in composite mode. For example ImageJ reads the data as 8 bit, which it is, but does not open the image as a truecolor (Sorry for the bad choice of words I don't know the right terminology). Hence I have to manually combine the 3 channels together, which is bothersome for large datasets.
Here is a screen shot explaining. On the left is what I would like,i.e. what I obtain if I open the image directly with ImageJ, and on the right is what I currently have after saving images with Matlab and opening them with ImageJ, which I don't want.
The code I'm using to export the image sequence is the following. "FinalSequenceToExport" is the cell array containing the images.
for i = 1:SliceNumber
ExportedName = sprintf('%s%s%d.tiff',fileName,'Z',i);
imwrite(FinalSequenceToExport{i},ExportedName,'tif','WriteMode','overwrite','Compression','none');
end
If I ask Matlab the size of FinalSequenceToExport{1}, for instance, it gives 512 x 512 x 3.
If I open a given image in the command window and then save it with the same code as above, it does what I want and the resulting image opens as I want in ImageJ. Hence my guess would be that the problem arises from the use of the cell array but I don't understand how.
I hope I've been clear enough. If not please ask for more details.
Thanks for the help!
You need to specify the the 'ColorSpace'
Try this
imwrite(FinalSequenceToExport{i},ExportedName,...
'tif','WriteMode','overwrite','Compression','none', ...
'ColorSpace', 'rgb');
After revisiting this question I found the following to work, thanks to the hint from #Ashish:
imwrite(uint8(FinalSequenceToExport{i}/255),...);
I just needed to divide by 255 after converting to uint8.

Matlab image file convert from *.PPM into *.PGM?

I have dataset of training images in .ppm format.
But when I want to extract the feature using SIFT descriptor from Lowe, I have an problem.
Because my training images dataset is in *.PPM format, but to use SIFT function from lowe, the images required is in *.PGM format.
Everytime I give my *.PPM image file, I always get this error
Error using '
Transpose on ND array is not defined.
Error in sift (line 38)
fwrite(f, image', 'uint8');
I know that I can convert my *.PPM image inti *.PGM by using
imwrite(image,'test.pgm')
But it means, that I'll make new file with *.PGM format, And I dont need it..
Any idea??

Converting image to data file (.dat) in matlab

Is there a way in matlab to convert an image file i.e; .png to .dat file?
It depends on the format of your .dat file. .dat is not a standard extension so you can get this from somewhere or design your own writer and reader. In terms of reading the .png you can just use matlabs imread('filename') function. This will return a NxMx3 matrix of the image and RGB values.
I would suggest combining the RGB values into a single float value and then using dlmwrite or csvwrite to store it. If you want to manipulate the image first that will be up to you.

Reading a .dat image file

I have an image stored in .dat format an I want to read and then show the image. imshow and imread functions don't work.what are the other functions that I can use?
Note that I'm using Matlab R2010a
The load function should work
load("filename.dat")

Resources