image thumbnail

Image Section Generator

by

 

21 May 2009 (Updated )

Image Section Generator grabs cursor-designated linescans from microscope images

SaveSectionData.m
%--------------------------------------------------------------------%
%	    Alison Chaiken, sole author and maintainer     			     %
%        ------------------------------------------------------      %
%	    GUI-based Matlab and FEMLAB data analysis, instrument	     %
%	    control, statistical analysis, finite-element modelling,     %
%	    image acquisition and analysis          				     %
%       -------------------------------------------------------      %
%	    alchaiken@gmail.com			                    		     %
%	    http://www.exerciseforthereader.org/         			     %
%	    (001)650-279-5600					                         %
%--------------------------------------------------------------------%

% Save section data into an ASCII file with (distance along section, intensity) 
% data in tab-delimited data format.    RGB data has 3 data sets, while
% grayscale data has only one.    Some image metadata is included in the
% file header as comments delimited with #, the comment character used by
% many programs on Linux.   The comment character can easily be replaced by
% something else.    Is there a standard comment character for Windows?

global DEFAULT_BACKGROUND_COLOR
set(handles.SaveSectionDataButton,'BackgroundColor',DEFAULT_BACKGROUND_COLOR)

error_condition = true;
try
    SectionData = get(handles.SectionPlot,'UserData');
    error_condition = false;
catch
    errordlg('An open section plot is necessary to save data.','Section data save error')
    set(handles.MakeSectionPlot,'BackgroundColor','green')
    guidata(handles.ImageSectionGenerator,handles)
end

if (get(handles.CursorChoiceButton,'value')) && (~ishandle(handles.Image))
      errordlg('An open image with section line is necessary to save a cursor-drawn section.')
      error_condition = true;
end

if (~error_condition)
    prompt={'Enter file name'};
    name='Section data file name';
    numlines=1;
    defaultanswer={'section.dat'};
    holder = inputdlg(prompt,name,numlines,defaultanswer);
    %needed because holder is a cell while outfilename must be a string
    if isempty(holder)
        return
    end
    outfilename=holder{1};
    if (CheckForExistingFile(outfilename) == 1)
        prompt={sprintf('Overwrite existing file %s?',outfilename)};
        name='File exists';
        writeanswer = questdlg(prompt,name);
        if strcmp(writeanswer,'Yes')
            try
                fid=fopen(outfilename,'w');
            catch
                errordlg('File open failed.')
                return
            end
        else return
        end
    else
        try
                fid=fopen(outfilename,'w');
            catch
                errordlg('File open failed.')
                return
            end
    end
    
    
    %These are the position values in microns.
    temporary=SectionData(:,1);
    
    if (get(handles.RowColumnChoiceButton,'value')) %numeric selection of section
        %1 for vertical sections
        rcflag=get(handles.VerticalButton,'value');
        if rcflag
            ellnumber = handles.ImageHeight;
            rcstring='column';
        else
            ellnumber = handles.ImageWidth;
            rcstring='row';
        end
    else %cursor-drawn selection of section
        ellnumber =size(temporary);
        ellnumber = ellnumber(1);
    end
    
    fprintf(fid,'#From file %s.\n', get(handles.ImageFileName,'string'));
    if (get(handles.RowColumnChoiceButton,'value')) %numeric selection of section(
        fprintf(fid,'#From %s number %.0f\n', rcstring, get(handles.NumericSectionLocation,'value'));
    else %cursor-drawn selection of section
        %get endpoints of section stored in UserData of image
        endpoints = get(handles.Image,'UserData');
        fprintf(fid,'#Endpoints of section are (%.0f, %.0f) and (%.0f, %.0f).\n', endpoints(1,1), ...
            endpoints(1,2), endpoints(2,1), endpoints(2,2));
    end
    fprintf(fid,'#\n');
    if strcmp(handles.ImageType,'color')
        if (get(handles.ForceGray,'value') == 0)
            redline=SectionData(:,2);
            greenline=SectionData(:,3);
            blueline=SectionData(:,4);
            
            fprintf(fid,'#Red data\n');
            fprintf(fid,'#Position(microns) Saturation\n');
            for i=1:ellnumber
                fprintf(fid,'%g\t%g\n',temporary(i),redline(i));
            end
            
            fprintf(fid,'\n#Green data\n');
            fprintf(fid,'#Position(microns) Saturation\n');
            for i=1:ellnumber
                fprintf(fid,'%g\t%g\n',temporary(i),greenline(i));
            end
            
            fprintf(fid,'\n#Blue data\n');
            fprintf(fid,'#Position(microns) Saturation\n');
            for i=1:ellnumber
                fprintf(fid,'%g\t%g\n',temporary(i),blueline(i));
            end
        else
            grayplot=SectionData(:,2);
            fprintf(fid,'#Forced grayscale data\n');
            fprintf(fid,'#Position(microns) Saturation\n');
            for i=1:ellnumber
                fprintf(fid,'%g\t%g\n',temporary(i),grayplot(i));
            end
        end
    else
        fprintf(fid,'#\n#Grayscale data\n');
        fprintf(fid,'#Position(microns) Saturation\n');
        grayplot=SectionData(:,2);
        for i=1:ellnumber
            fprintf(fid,'%g\t%g\n',temporary(i),grayplot(i));
        end
    end
    fclose(fid);
end

Contact us