MATLAB Answers

0

How to write for loop and execute data

Asked by Bualookkaew Sakchatchawan on 16 Sep 2018 at 11:28
Latest activity Answered by KALYAN ACHARJYA on 16 Sep 2018 at 15:30

Hello everyone

I have 1800 projections of the image with .xim file. Now, I have a code to read information of the image, but it works for one by one. Then, I write for loop cover the read image code, but it dosesn work and show the result as too many data. I'm very new on MATLAB, please check my code and fixed it.

fileFolder = fullfile('765898760');
files = dir(fullfile(fileFolder,'*.xim'));
filename = {files.name};
      for k = 1:numel (files); 
          s = dir(fileFolder, files(k).name);
          ximHead(k).= s
         %% read .xim file
         %% Actually, it is function.m, but I can't call function inside another function %%
        %% orginal *****  function img=ReadXim(filename, varargin)
  %   READXIM(FILENAME, {READ_PIXEL_DATA})
  %       reads XIM image files from Varian TrueBeam accelerators
  %       and returns a struct containing the XIM image information
  %       and pixel data.
  %
  %   FILENAME is the full path to the XIM-image file
  %   READ_PIXEL_DATA determines whether pixel data should be decoded
  %       0: skip
  %       1: decode  (default)
  %
  %   Developed by Fredrik Nordstr๖m 2015
  %   The program has not been tested with uncompressed images
  %
  %   modified May 2016 C. Pelizzari
  %   sort fieldnames before returning - makes it easier to use
  %
  %   modified Oct 2016 C. Pelizzari
  %   use c function for uncompressing via MEX - reduces time from seconds to
  %   hundredths of seconds
  %
  %
          read_pixel_data=1;
          if nargin==2 && varargin{1}==0
             read_pixel_data=0;
          end
          % Decode header
  img.file_name=filename;
  img.file_format_identifier=fread(fid,8,'*char')';
  img.file_format_version=fread(fid,1,'*int32');
  img.image_width=fread(fid,1,'*int32');
  img.image_height=fread(fid,1,'*int32');
  img.bits_per_pixel=fread(fid,1,'*int32');
  img.bytes_per_pixel=fread(fid,1,'*int32');
  img.compression_indicator=fread(fid,1,'*int32');
% Decode pixel data
if img.compression_indicator==1
    lookup_table_size = fread(fid,1,'int32');
    lookup_table=fread(fid,lookup_table_size*4,'ubit2=>uint8');
    img.lookup_table=lookup_table;
    compressed_pixel_buffer_size = fread(fid,1,'int32');
    img.lookup_table_size=lookup_table_size;
    img.compressed_pixel_buffer_size=compressed_pixel_buffer_size;
    if read_pixel_data==1
        if exist('myreadxim') == 3,  % exist=3 means we found a MEX file
          % decompress image using C function (fast)
          compressed_pixel_buffer=fread(fid,compressed_pixel_buffer_size,'*int8');
          img.compressed_pixel_buffer=compressed_pixel_buffer;
          pixel_data = myreadxim(img.image_height, img.image_width, ...
              compressed_pixel_buffer, lookup_table);
        else
        %Decompress image in Matlab (slow)
            pixel_data=int32(zeros(img.image_width*img.image_height,1));
            pixel_data(1:img.image_width+1)=fread(fid,img.image_width+1,'*int32');
            lookup_table_pos=1;
            for image_pos=(img.image_width+2):(img.image_width*img.image_height)
                if lookup_table(lookup_table_pos)==0
                    diff=int32(fread(fid,1,'*int8'));
                elseif lookup_table(lookup_table_pos)==1
                    diff=int32(fread(fid,1,'*int16'));
                else
                    diff=int32(fread(fid,1,'*int32'));
                end
                pixel_data(image_pos)=diff+pixel_data(image_pos-1)+...
                    pixel_data(image_pos-img.image_width)-...
                    pixel_data(image_pos-img.image_width-1);
                lookup_table_pos=lookup_table_pos+1;
            end
        end
        if img.bytes_per_pixel==2
            img.pixel_data=int16(reshape(pixel_data,img.image_width,img.image_height))';
        else
            img.pixel_data=reshape(pixel_data,img.image_width,img.image_height)';
        end
    else
        fseek(fid,compressed_pixel_buffer_size,'cof');
    end
    uncompressed_pixel_buffer_size = fread(fid,1,'*int32');
else
    uncompressed_pixel_buffer_size = fread(fid,1,'*int32');
    if read_pixel_data==1
        switch img.bytes_per_pixel
            case 1
                pixel_data=fread(fid,uncompressed_pixel_buffer_size,'*int8');
            case 2
                pixel_data=fread(fid,uncompressed_pixel_buffer_size/2,'*int16');
            otherwise
                pixel_data=fread(fid,uncompressed_pixel_buffer_size/4,'*int32');
        end
        img.pixel_data=reshape(pixel_data,img.image_width,img.image_height)';
    else
        fseek(fid,uncompressed_pixel_buffer_size,'cof');
    end
end
% Decode histogram
number_of_bins_in_histogram = fread(fid,1,'*int32');
if number_of_bins_in_histogram>0
    img.histogram.number_of_bins_in_histogram=number_of_bins_in_histogram;
    img.histogram.histogram_data = fread(fid,number_of_bins_in_histogram,'*int32');
end
% Decode properties
number_of_properties = fread(fid,1,'*int32');
if number_of_properties>0
    img.properties=[];
    properties=[];
end
for property_nr=1:number_of_properties
    property_name_length = fread(fid,1,'*int32');
    property_name = fread(fid,property_name_length,'*char')';
    property_type = fread(fid,1,'*int32');
    switch property_type
        case 0
            property_value = fread(fid,1,'*int32');
        case 1
            property_value = fread(fid,1,'double');
        case 2
            property_value_length = fread(fid,1,'*int32');
            property_value = fread(fid,property_value_length,'*char')';
        case 4
            property_value_length = fread(fid,1,'*int32');
            property_value = fread(fid,property_value_length/8,'double');
        case 5
            property_value_length = fread(fid,1,'*int32');
            property_value = fread(fid,property_value_length/4,'*int32');
        otherwise
            disp(' ')
            disp([property_name ': Property type ' num2str(property_type) ' is not supported! Aborting property decoding!']);
            fclose(fid);
            return;
    end
    %img.properties=setfield(img.properties,property_name,property_value);
    properties.(property_name)=property_value;
end
fclose(fid);
sortnames=sort(fieldnames(properties));
for i=1:numel(sortnames)
    img.properties.(sortnames{i})=properties.(sortnames{i});
end
      end

  0 Comments

Sign in to comment.

1 Answer

Answer by KALYAN ACHARJYA on 16 Sep 2018 at 15:30

Check here, If you find any problem, let me know here.

  0 Comments

Sign in to comment.