How to write slice by slice from nifti images in matlab? - matlab

I read nifti images by using matlab toolbox but how we can write slice by slice into another format like .jpg or png?
I try this like:
V=niftiread('Brats17_2013_2_1_flair.nii.gz');
imshow(V(:,:,1),[]);
imwrite(V,'test.jpg')
Error using imwrite (line 442)
Cannot write signed integer data to a JPEG file.
imwrite((V(:,:,1),[]),'test.jpg');
imwrite((V(:,:,1),[]),'test.jpg');
↑
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.

just save the picture by "saveas" command. it worked correctly.
h = imshow(V(:,:,1),[]);
saveas (h,'test.jpg');
Good luck

Related

How to convert PPM images to JPG in Matlab?

I have some PPM images (stereo) that I read with imread() and I want to save the same images in JPEG with different Quality factors.
Here is my code.
%Read PPM image
L = imread(filename_L);
%Create JPEG Q85 from PPM
filename_L85 = strcat(filename_L,'_ppm_to_jpeg.jpg');
imwrite(L,filename_L85,'JPEG','Quality',85);
And here the error I get.
Error using imwrite>parse_inputs (line 528)
The colormap should have three columns.
Error in imwrite (line 418)
[data, map, filename, format, paramPairs] = parse_inputs(varargin{:});
Error in testFinale (line 75)
imwrite(L,filename_L85,'JPEG','Quality',85);
How can I write JPEG images previously read in PPM format?
Thanks
Could it be that is just has to do with your case of 'JPEG', the documentation of imwrite specifies parameters for file type as lowercase.
Apart from that you might not even need it as the file type is derived from the extension which in this case is set explicitly to .jpg already.
So you might either go for:
imwrite(L,filename_L85,'jpeg','Quality',85);
or perhaps even easier:
imwrite(L,filename_L85,'Quality',85);

Matlab error while creating csv file from image set using imread and csvwrite

I want to convert a set of images into a csv file. I was working with Matlab and I need each row corresponding to one image. I tried to do it with the following code
I=imread(c{n});
csvwrite('C:\Users\HP\Desktop\test.csv',I(:).','-append'); % c{n} contains the name of image files to be taken
but I am getting the following error
Error using dlmwrite (line 112)
Invalid attribute tag: ,.
Error in csvwrite (line 42)
dlmwrite(filename, m, ',', r, c);
Error in Untitled (line 7)
csvwrite('C:\Users\HP\Desktop\test.csv',I(:).','-append');
but if I try to do it without the '-append' there is no error.
How to change the code such that it takes all the images at once and produces a csv file with the single execution of the code.
csvwrite is meant for writing comma separated values, so adding that ',' is wrong. Then you have put a dot('.') after I(:), which is also wrong. I think you should better use dlmwrite if you want to append the files. It would go like dlmwrite('C:\Users\HP\Desktop\test.csv',I(:)','-append') (since you want each image as one row, you need to transpose the array).
For using this on all images, start by reading all the images into a cell array & then you use cellfun(#(x) dlmwrite('C:\Users\HP\Desktop\test.csv',x(:)','-append'),a). Or for a much simpler version just run the lines in your code inside a for loop.

Issue when writing matrix to file using csvwrite

I am trying to output the matrix to a CSV file (comma separated) using this function csvwrite('myMatrix.dat',L); ( where L is square matrix) I got this error:
>> csvwrite('myMatrix.dat',L);
Error using sprintf
Function is not defined for sparse inputs.
Error in dlmwrite (line 169)
str = sprintf(format,m(i,:));
Error in csvwrite (line 42)
dlmwrite(filename, m, ',', r, c);
Kindly, what's wrong with this?
This answer was to answer OP's original error from using:
csvwrite(string('myMatrix'),L);
Error using csvwrite (line 30)
FILENAME must be a character vector.
The error you're seeing is an issue with your input (arguments). It's telling you that the file name must be a "character vector".
According to Matlab's documentation:
There are two ways to represent text in MATLAB®. Starting in R2016b, you can store text in string arrays. And in any version of MATLAB, you can store text in character arrays. A typical use for character arrays is to store pieces of text as character vectors. MATLAB displays strings with double quotes and character vectors with single quotes.
http://mathworks.com/help/matlab/matlab_prog/creating-character-arrays.html#briuv_1-1
In simple words. The wrong type was being used as an argument.
To provide a hint for debugging the new error.
Limitations
csvwrite writes a maximum of five significant digits. If you need greater precision, use dlmwrite with a precision argument.
csvwrite does not accept cell arrays for the input matrix M. To export a cell array that contains only numeric data, use cell2mat to convert the cell array to a numeric matrix before calling csvwrite.
http://mathworks.com/help/matlab/ref/csvwrite.html?requestedDomain=www.mathworks.com
Try checking what's in L. whos('L') would also help you get more info on it. An easy way to view what's in your variables, is double click from the workspace. Another way is by creating a break point on your call to csvwrite within a script, then use the debugger and calling for L once you know it's loaded into memory. If you still don't know what's going on, then try 'step in' line by line.
csvwrite does not accept cell arrays.

MATLAB uint8 data type variable save

does anyone know how to save an uint8 workspace variable to a txt file?
I tried using MATLAB save command:
save zipped.txt zipped -ascii
However, the command window displayed warning error:
Warning: Attempt to write an unsupported data type to an ASCII file.
Variable 'zipped' not written to file.
In order to write it, simply cast your values to double before writing it.
A=uint8([1 2 3])
toWrite=double(A)
save('test.txt','toWrite','-ASCII')
The reason uint8 can't be written is hidden in the format section of the save doc online, took myself a bit to find it.
The doc page is here: http://www.mathworks.com/help/matlab/ref/save.html
The 3rd line after the table in the format section (about halfway down the page) says:
Each variable must be a two-dimensional double or character array.
Alternatively, dlmwrite can write matrices of type uint8, as the other poster also mentioned, and I am sure the csv one will work too, but I haven't tested it myself.
Hopefully that will help you out, kinda annoying though! I think uint8 is used almost exclusively for images in MATLAB, but I am assuming writing the values as an image is not feasible in your situation.
have you considered other write-to-file options in Matlab?
How about dlmwrite?
Another option might be cvswrite.
For more information see this document.
Try the following:
%# a random matrix of type uint8
x = randi(255, [100,3], 'uint8');
%# build format string
frmt = repmat('%u,',1,size(x,2));
frmt = [frmt(1:end-1) '\n'];
%# write matrix to file in one go
f = fopen('out.txt','wt');
fprintf(f, frmt, x');
fclose(f);
The resulting file will be something like:
16,108,149
174,25,138
11,153,222
19,121,68
...
where each line corresponds to a matrix row.
Note that this is much faster than using dlmwrite which writes one row at a time

Image processing using MATLABR2010a

I tried to read an image and display it but i faced an error and i didn't understand it.can any one help me please, note that i use MATLAB R2010a, and the display below is the type of error.
>> imread('tas.jpg');
>> imshow('tas.jpg');
??? Attempt to call constructor image with incorrect letter case.
**Error in ==> basicImageDisplay at 9
hh = image(xdata,ydata,cdata, ...
Error in ==> imshow at 246
hh = basicImageDisplay(fig_handle,ax_handle,...**
I = imread('tas.jpg');
imshow(I);
The imread function reads the file and converts it to a RGB matrix of pixels. This is stored on the variable I. Then, you can call imshow passing this RGB matrix as a parameter ;)
edit you can call imshow with the filename as well, but it's not that useful because it does not return the matrix you will later use for processing. And as the error is thrown only on imshow, I'm guessing the imread function, for some reason, is working.
If not, just double check if the image is on the actual directory or in a directory on the path, or if it is not corrupted.
This might be the reason (from the below thread):
Reason: "current directory folder name is match with built-in function in matlab library and gives the error - Attempt to call constructor image with incorrect letter case".
Solution: change the folder name with unique name.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/256922