Texo Data Processing Prior to 6.0.x

From WikiSonix
(Redirected from Texo Data Processing)
Jump to: navigation, search

Data representation[edit]

The data in the CINE-Buffer and respectively in the output files of the console example program is stored in the following manner:


DATA structure


Each block represents one byte. The number of samples in one line can be derived by:

Pseudocode: (Texo::getFrameSize()-4)/(numberOfScanlinesPerFrame*sizeof(short))

MATLAB method to read data-file[edit]

To display the results of Texo data capture, the MATLAB code below provides a simple example. It is a function that will take in raw RF values outputted from Texo, and create a large matrix that will hold all RF lines organized into frames.

  • filename: Name of the raw RF data file, including all the directory information.
    • eg. \\D:\Data\Texo\RF\output.raw
  • w: Number of scanlines per frame. This information is determined by counting the times the function texo::addLine() was called for the sequence
  • h: Size of each line in bytes. This value can be calculated by dividing the frame size in bytes by the number of scanlines per frame (w). However, the header of each file takes up 4 bytes, so it is necessary to get rid of first 4 bytes header which is used as a time-stamp for each frame collected from the cine buffer. The value of frame size is obtained by the function texo::getFrameSize().
  • frame_no: Number of frames acquired. This information is obtained by calling texo::getCollectedFrameCount().

A known error on the Sonix hardware is the fact that the first RF line on the first frame captured will be distorted. The easiest way to deal with this problem is to ignore the very first frame. Therefore it is advised to acquire one extra frame than actually necessary and discard the first frame.

fid = fopen(filename,'r'); 
frame_no*(w*h/2+2); 
 [v,count] = fread(fid,inf,'short'); 
count; 
images = []; 
BigMatrix=[]; 
  
for frame_count = 1:frame_no, 
    BigMatrix(frame_count,:) = v(((frame_count-1)*(h/2*w+2)+3) : ((frame_count)*(h/2*w+2)))'; 
end; 
for i=1:frame_no, 
    A = reshape(BigMatrix(i,:),h/2,w); 
    images(:,:,i) = A; 
   plot(A);%,axis([0 xmax ymin ymax  ]);%imagesc(A);colormap gray; 
end; 
fclose(fid); 

The following MATLAB code provides an alternative method to read RF-data from a file. It will create a 3d-matrix containing the single scanlines sorted by frames.

clear;
%Constants 
filename = 'frames.rf'; %Filename of the RF-file
headerSize = 4;         %Size of frame header in bytes (constant if not changed by a new version of Texo)
frameSize = 124676;      %Size of a single frame in bytes
numberOfFrames = 10;     
linesPerFrame = 4;      %Number of scanlines per frame

%Read RF-file
fid = fopen(filename,'r');
 [dataFromFile] = fread(fid,inf,'short');
fclose(fid);            %Close the file, we have what we needed

%Sort frames
numberOfFrames = numberOfFrames-1;           %drop first frame as invalid, as described in forum
frames = zeros(frameSize/2,numberOfFrames);  %create empty matrix (speeds up copying)
for i = 1:numberOfFrames,                    %copy whole frames in matrix
    frames(:,i) = dataFromFile(i*frameSize/2+1:(i+1)*frameSize/2);
end;
clear dataFromFile;                           %Delete data to free memory

%Split up frames into scanlines
samplesPerLine = ((frameSize-headerSize)/2)/linesPerFrame;
frames(1:2,:) = [];                                         %delete frame headers
linesByFrame = zeros(samplesPerLine,linesPerFrame,numberOfFrames); %empty 3d-matrix to hold lines
for i = 1:numberOfFrames,                                   
    for j = 1:linesPerFrame,
        linesByFrame(:,j,i) = frames((j-1)*samplesPerLine+1:j*samplesPerLine,i);
    end;
end;
clear frames;   %Delete data to free memory

%Example of how to extract a single frame (frame 2 in this case)
frame1(:,:) = linesByFrame(:,:,2);

A Short Introduction to Imaging[edit]

To create an image from scanlines a, the ultrasound data have to be envelope detected. Using Texo, the easiest way to achieve this, is to order envelope detected data from the machine:

tex->addLine(envData, tx, rx))

The enevelope detected samples have to be allocated to grayvalues. Note that more than 8bit resolution are not neccessary or useful, since the human eye can only resolve less, than a hundred grayvalues. One single frame of Texo's envelope detected RF data has to be split into the single scanlines it consists of and these scanlines have to be arranged next to each other. In the following image, the process is simplified and broken down for laymen, so there are only two grayvalues, black and white.


Creating Images


Here is an pseudocode example of a thread's function, that constructs an image out of the raw data. The sequence that goes hand in hand with this image generation method, is a simple linear sequence:

    for(int i=0;i<tex->getProbeNumElements();i++) {
        rx.centerElement = i*10+5;
        tx.centerElement = i*10+5;
        tex->addLine(envData, tx, rx);
    }

Since the process of generating an image can be quite time consuming, it should be delegated to a thread, in order to not make the program unresponsive. Note, that the geometrical dimensions of the wavelength and the dimension of the probe are not respected in this simple example. Also it is not an ideal representation of the grayvalues, since the human eye has a higher sensitivity for green colors, depending on the method to display the image a weighting function should be used.


    i=0

    for(x=0, x<numberOfLines, x++) {                           //iterate through the scanlines along the transducer
        for(y=0, y<samplesPerLine, y++) {                      //iterate through the samples perpendiculat to the transducer
            brightness = data[i]
            setColorRGB(brightness, brightness, brightness)    //a RGB-color with identical values in every channel is always a shade of gray
            drawPoint(x,y)
            i++
        }
    }