Stacking DICOM files into one multi-slice series - matlab

I'm trying to stack different DICOM files into one multi-slice series, so to visualize them on ITK-Snap. However, I don't seem to be able to obtain a functioning DICOM series.
I have sorted all files with regards to their slice positioning, and I have a number of ordered single .dcm files, with their original info. I substituted all their original series instance UIDs with one single uid, and their series number with one custom series number I have set to '999' (so to make them belong to one series). Image orientation is set to [1;0;0;0;1;0] for all files, and slice thickness at 8 mm for all files.
I have then created an array of info structures, with the original slice positionings [info(num)].
I have tried something like:
for i=1:num %where num is the number of dicom files
k = num2str(i);
dicomwrite(imm,k,info(i),'CreateMode','Copy'); %where imm is the matrix I obtained with dicomread
end
I have obtained a new set of dicom files, named as numbers from 1 to num, however when I try to open the series on ITK-snap, it runs into an exception stating the vector is too long. I can open single dicom files on ITK-snap, however when more than one images are part of the series, and the series is visualized as 256x212xnum where num is the number of files, I run into the exception.
What am I doing wrong?

What you are trying to do is called Multi-frame in the DICOM standard. In short you need to, next to making sure that all your image meta data is still correct, specify Number of Frames (0028,0008) and Frame Increment Pointer (0028,0009). Unfortunately the wording on how the Frame Increment Pointer tag exactly works is a bit vague:
The frames within a Multi-frame Image shall be conveyed as a logical sequence. The information that determines the sequential order of the frames shall be identified by the Data Element Tag or Tags conveyed by the Frame Increment Pointer (0028,0009). Each specific Image IOD that supports the Multi-frame Module specializes the Frame Increment Pointer (0028,0009) to identify the Attributes that may be used as sequences.
Even if only a single frame is present, Frame Increment Pointer (0028,0009) is still required to be present and have at least one value, each of which shall point to an Attribute that is also present in the Data Set and has a value. 1
Hope that helps.

Related

Matlab function reshape doesnt´t calculate the last dimension while trying to create a 3D image from .raw binary image file

I created binarized images by using the Otsu methode in Matlab and cut out parts of the resulting image using a function. Now i want to take a look at these images with the VolumeViewer command. I know the x,y and z dimensions of the resulting imgages. I currently run this code doing it(excluding the volumeViewerwhich happens after the loop):
files= {'C3\C3_000mal_550_539_527.raw';...
};
for i=1:numel(files)
Image = fopen(files{i},'r');
ImageData{i} = fread(Image,Inf,'uint16=>uint16');
ImageData{i} = reshape(ImageData{i},550,539,[]);
fclose(openedCrystalImage);
end
Using this code runs into the following error using reshape:
Error using reshape
Product of known dimensions, 296450, not divisible into total number of elements, 78114575.
I did the maths and 550*539=296450 and 296450 * 527=156229150: If we divide the last number by the number of elements it equals 2 and thus is divisible into the total number of elements. In my opinion the reshape function is not able to find the size of the last dimension or defines it as 1.
Defining the size of z also results in an error suggesting using the brackets [], so the function can find it.
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size
for that dimension.
Now to the weird part. This code works for another set of images, with diffrent sizes of the x,y and z ranges. So don´t know where the issue lies to be frank. So i would really appreciate and Answer to my question
I figured it out. The error lies here:
ImageData{i} = fread(Image,Inf,'uint16=>uint16');
Apparently by saving them as .raw before it converts the image to an 8 bit file rather than 16 bits it had before. Therefore, my dimension is double the size of the number of elements. With this alteration it works:
ImageData{i} = fread(Image,Inf,'uint8=>uint8');
The reason i was able to look at the other pictures was that the z range was divisble by 2.
So the reshape function was not the problem but size of the integer data while creating the array for the variable ImageData.
P.S. I just started out programming so the accuracy in the answer should be taken with a grain of salt

Matlab variable taking forever to save

I have a MATLAB variable that is a 3x6 cell array. One of the columns of the cell array holds at most 150-200 small RGB images, like 16x20 pixel size (again, at most). The rest of the columns are:
an equal number of labels that are strings of a 3 or 4 characters,
an image mask, which is about 350x200
3 integers
For some reason saving this object is taking a very long time, or at least for the size of the object. It has already been 10 minutes(which isn't too bad, but I plan on expanding the size of the object to hold several thousand of those small images) and MATLAB doesn't seem to be making any progress. In fact, when I open the containing directory of the variable, its size is cycling between 0 bytes to about 120kB. (i.e. it will increase to 120 in steps of 30 or 40 kB, then restart).
Is this normal behavior? Do MATLAB variables always take so long to save? What's going on here?
Mistake: I'm saving AllData, not my SVM variable. AllData has the same data as the SVM keeper, less the actual SVM itself and one integer.
What particular points of the code would be helpful to show for solving this? The code itself is a few hundred lines and broken up in several functions. What would be important to consider to troubleshoot this? When the variable is created? when it's saved? The way I create the smaller images?
Hate to be the noob who takes a picture of their desktop. but the machine I'm working has problems taking screenshots. Anyway, here it is
Alldata/curdata are just subsets of the 3x7 array... actually it's a 3x8, but the last is just an int.
Interesting side point: I interrupted the saving process and the variable seemed to save just fine. I trained a new svm on the saved data and it works just fine. I'd like to not do that in the future though.
Using whos:
Name Size Bytes Class Attributes
AllData 3x6 473300 cell
Image 240x352x3 253440 uint8
RESTOREDEFAULTPATH_EXECUTED 1x1 1 logical
SVMKeeper 3x8 2355638 cell
ans 3x6 892410 cell
curData 3x6 473300 cell
dirpath 1x67 134 char
im 240x352x3 1013760 single
s 1x1 892586 struct
Updates:
1.Does this always happen, or did you only do it once?
-It always happens
2.Does it take the same time when you save it to a different (local) drive?
-I will investigate this more when I get back to that computer
3.How long does it take to save a 500kb matrix to that folder?
-Almost instantaneous
4.And as asked above, what is the function call that you use?
-Code added below
(Image is a rgb image)
MaskedImage(:,:,1)=Image(:,:,1).*Mask;
MaskedImage(:,:,2)=Image(:,:,2).*Mask;
MaskedImage(:,:,3)=Image(:,:,3).*Mask;
MaskedImage=im2single(MaskedImage);
....
(I use some method to create a bounding box around my 16x20 image)
(this is in a loop of that occurs about 100-200 times)
Misfire=input('is this a misfire?','s');
if (strcmpi(Misfire,'yes'))
curImageReal=MaskedImage(j:j+Ybound,i:i+Xbound,:);
Training{curTrainingIndex}=curImageReal; %Training is a cell array of images
Labels{curTrainingIndex}='ncr';
curTrainingIndex=curTrainingIndex+1;
end
(the loop ends)...
SaveAndUpdate=input('Would you like to save this data?(say yes,definitely)','s');
undecided=1;
while(undecided)
if(strcmpi(SaveAndUpdate,'yes,definitely'))
AllData{curSVM,4}=Training;
AllData{curSVM,5}=Labels;
save(strcat(dirpath,'/',TrainingName),'AllData'); <---STUCK HERE
undecided=0;
else
DontSave=input('Im not going to save. Say YESNOSAVE to NOT SAVE','s')
if(strcmpi(DontSave,'yesnosave'))
undecided=0;
else
SaveAndUpdate=input('So... save? (say yes,definitely)','s');
end
end
end
It is a bit unclear if you are doing some custom file saving or not. If it is the first, I'm guessing that you have a really slow save loop going on, maybe some hardware issues. Try to save the data using MATLAB's save function:
tic
save('test.mat', 'AllData')
toc
if that works fine try to work your way from there e.g. saving one element at a time etc.
You can profile your code by using the profiler, open it with the command profile viewer and then type in the code, script or function that you want to profile in the input text field.
This isn't a great answer, but it seems that the problem was that I was saving the version of my image after I had converted it to a single. I don't know why this would cause such a dramatic slowdown (after removing this line of code it worked instantly) so if someone could edit my answer to shed more light on the situation, that would be appreciated.

how to code a function in matlab which will display pixels of an gray image which is not repeated

hi i m trying to implement huffman code which has following steps:
Development of Huffman Coding and Decoding Algorithm
Step1- Read the image on to the workspace of the mat
lab.
Step2- Convert the given colour image into grey level
image.
Step3- Call a function which will find the symbols (i.e.
pixel value which is non-repeated).
Step4- Call a function which will calculate the
probability of each symbol.
Step5- Probability of symbols are arranged in decreasing
order and lower probabilities are merged and this
step is continued until only two probabilities are
left and codes are assigned according to rule that
:the highest probable symbol will have a shorter
length code.
Step6- Further Huffman encoding is performed i.e.
mapping of the code words to the corresponding
symbols will result in a compressed data.
Step7- The original image is reconstructed i.e.
decompression is done by using Huffman
decoding.
Step8- Generate a tree equivalent to the encoding tree.
Step9- Read input character wise and left to the table II
until last element is reached in the table II.
Step10 -Output the character encode in the leaf and return
to the root, and continue the step9 until all the
codes of corresponding symbols are known.
i have implemented steps 1 & 2..in 3rd steps i m able to display the pixels of an image using function impixelregion; but i m not able to find how to code a function which will display pixels values which are not repeated..kindly help me plzzz...
See imhist i.e. use imhist(I,256), where I is your gray image.

Create a circular buffer for image acquisition

I'm new in programming with matlab and trying to do the following:
I continously capture an image (size 1024x1024) with a camera to have a live image using the getdata function.
To do a measurement I would like to store only 100 images using a circular buffer- more precisely I'm thinking of storing 100 images and erasing the oldest images if new data is acquired and do a measurement on the last 100 images.
Hope my concern is understandable...
Thanks for an answer!
This question has been answered here by a worker from MathWorks : Create a buffer matrix for continuous measurements. ( He also made a video of it : http://blogs.mathworks.com/videos/2009/05/08/implementing-a-simple-circular-buffer/
The code :
buffSize = 10;
circBuff = nan(1,buffSize);
for newest = 1:1000;
circBuff = [newest circBuff(1:end-1)]
end
Check the update made by gnovice which applies the circular buffer to image processing.
What you call a "circular buffer" is known as a queue or FIFO (First In, First Out). Usually this would be stored in a linked list data structure, where every object (matrix, in your case) points to the next object. In Matlab however, there is not built-in linked list structure, but Matlab arrays (vectors/matrices) are pretty flexible and efficient when it comes to manipulating them.
So you can simply store each image as a matrix inside an array of length 100, giving you a 3 dimensional matrix of dimensions 100x1024x1024. Then, when you get new data you simply remove the last matrix from the array and insert a new matrix at the beginning of the array. Hopefully this will be fast enough for you.
Good luck!
May you can create an array of 100 1024x1024-matrices. and refer the following link to maintain the read and write position.
logic of circular buffer

MATLAB Saving and Loading Feature Vectors

I am trying to load feature vectors into classifiers such as a k-nearest neighbors classifier.
I have my code for GLCM, so I get contrast, correlation, energy, homogeneity in numbers (feature vectors).
My question is, how can I save every set of feature vectors from all the training images? I have seen somewhere that people had a .set file to load into classifiers (may be it is a special case for the particular classifier toolbox).
load 'mydata.set';
for example.
I suppose it does not have to be a .set file.
I'd just need a way to store all the feature vectors from all the training images in a separate file that can be loaded.
I've google,
and I found this that may be useful
but I am not entirely sure.
Thanks for your time and help in advance.
Regards.
If you arrange your feature vectors as the columns of an array called X, then just issue the command
save('some_description.mat','X');
Alternatively, if you want the save file to be readable, say in ASCII, then just use this instead:
save('some_description.txt', 'X', '-ASCII');
Later, when you want to re-use the data, just say
var = {'X'}; % <-- You can modify this if you want to load multiple variables.
load('some_description.mat', var{:});
load('some_description.txt', var{:}); % <-- Use this if you saved to .txt file.
Then the variable named 'X' will be loaded into the workspace and its columns will be the same feature vectors you computed before.
You will want to replace the some_description part of each file name above and instead use something that allows you to easily identify which data set's feature vectors are saved in the file (if you have multiple data sets). Your array of feature vectors may also be called something besides X, so you can change the name accordingly.