image thumbnail

Image Section Generator

by

 

21 May 2009 (Updated )

Image Section Generator grabs cursor-designated linescans from microscope images

MakeSectionPlot.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					                         %
%--------------------------------------------------------------------%

% Data manipulation to support image display, section extraction, and
% section display.    Includes all the array operations on the image data.

%some constants and misc. initialization
set(0,'Units','pixels')
scnsize=get(0,'ScreenSize');

set(handles.SelectionMethodGroup,'ForegroundColor','black')
set(handles.SelectionMethodGroup,'ShadowColor','black')
set(handles.SelectionMethodGroup,'FontWeight','normal')
set(handles.SectionOrientationGroup,'ForegroundColor','black')
set(handles.SectionOrientationGroup,'FontWeight','normal')
set(handles.SectionOrientationGroup,'ShadowColor','black')

imagewidth=handles.ImageWidth;
imageheight=handles.ImageHeight;
imagetype=handles.ImageType;
%ImageSectionGenerator is the main GUI
tiffarray=get(handles.ImageSectionGenerator,'UserData');
scalemax = handles.ScaleMax;

%Numeric or preset designation of section location
if (get(handles.RowColumnChoiceButton,'value') == 1)
    
    rowcolnumber = get(handles.NumericSectionLocation,'value');
    if (rowcolnumber == -1)
        set(handles.NumericSectionLocation,'BackgroundColor','green')
        errordlg('Please designate a section location before creating a plot.')
        return
    end
    
    if handles.rcflag=='v'
        if (rowcolnumber > imagewidth) || (rowcolnumber < 1)
            set(handles.NumericSectionLocation,'BackgroundColor','green')
            errordlg(sprintf('Column number out of bounds; must be 1 <= column number <= %d.', imagewidth),'Illegal row number')
            return
        end
        rowmin=1;
        rowmax=imageheight;
        maxcounter=imageheight;
        %ellnumber=imageheight;
        colmin=rowcolnumber;
        colmax=rowcolnumber;
        temporary=(1:1:imageheight)*handles.ScaleFactor;
    else   %if handles.rcflag=='h'
        if (rowcolnumber > imageheight)  || (rowcolnumber < 1)
            set(handles.NumericSectionLocation,'BackgroundColor','green')
            errordlg(sprintf('Row number out of bounds; must be 1 <= row number <= %d.', imageheight),'Illegal column number')
            return
        end
        colmin=1;
        colmax=imagewidth;
        maxcounter=imagewidth;
        %ellnumber=imagewidth;
        rowmin=rowcolnumber;
        rowmax=rowcolnumber;
        temporary=(1:1:imagewidth)*handles.ScaleFactor;
    end
    
    if (strcmp(imagetype, 'color') == 1)
        redline=tiffarray(rowmin:rowmax,colmin:colmax,1);
        redline=double(redline)/scalemax;
        greenline=tiffarray(rowmin:rowmax,colmin:colmax,2);
        greenline=double(greenline)/scalemax;
        blueline=tiffarray(rowmin:rowmax,colmin:colmax,3);
        blueline=double(blueline)/scalemax;
        
        if handles.rcflag=='v'
            redplot=redline';
            blueplot=blueline';
            greenplot=greenline';
        else   %if handles.rcflag=='h'
            redplot=redline;
            blueplot=blueline;
            greenplot=greenline;
        end
        
        if (get(handles.ForceGray,'value')) % if grayscale forcing is on for color image
            grayline=zeros(maxcounter,1);
            for i=1:maxcounter
                grayline(i)=sqrt((redplot(i)*redplot(i)) + (greenplot(i)*greenplot(i)) + (blueplot(i)*blueplot(i)))/sqrt(3);
            end
            OpenGraySection
        else
            OpenColorSection
        end
        
    else %grayscale image
        grayline=double(tiffarray(rowmin:rowmax,colmin:colmax))/scalemax;
        if handles.rcflag=='v'
            grayplot=grayline';
        else   %if handles.rcflag=='h'
            grayplot=grayline;
        end
        
        OpenGraySection
    end
    
    %show image with selected linescan
    figure;
    handles.Image=gcf;
    guidata(handles.ImageSectionGenerator, handles);
    UpperRightCurrentWindow
    
    %Pop-up image
    lineshow=axes();
    imshow(tiffarray,'Parent',lineshow)
    BetterWindowTitle
    
    if (handles.rcflag == 'v')
        exxvals = zeros(1,imageheight) + rowcolnumber;
        whyvals = (1:imageheight);
    else
        exxvals = (1:imagewidth);
        whyvals = zeros(1,imagewidth) + rowcolnumber;
    end
    
    line(exxvals,whyvals, 'Parent', lineshow,'linewidth',5,'color','yellow');
    
    if (get(handles.DisplayScaleBar,'value') == 1)
        DrawScaleBar
    end
    
    
else %manual cursor designation of section location
    
    handles.Image=figure;
    guidata(handles.ImageSectionGenerator, handles);
    UpperRightCurrentWindow
    %Pop-up image
    imshow(tiffarray)
    BetterWindowTitle
    
    if (get(handles.DisplayScaleBar,'value') == 1)
        DrawScaleBar
    end
    
    %disp('Entering interactive mode.')
    text(0,handles.ImageHeight+20,'Use the left mouse button to draw a line on the image.',...
        'Color','blue', 'BackgroundColor','yellow','FontSize',14)
    
    %alternate method using image processing toolbox
    %[bw,xi,yi]=roipoly;
    %xi_size = size(xi)
    %yi_size = size(yi)
    %xi=xi(1,size(xi)-1)
    %yi=yi(1,size(yi)-1)
    %	pixel info doesn't update while ginput is live
    %	impixelinfo(h)
    %get coordinates from the mouse
    %[xi,yi] = ginput(2);
    sectionline = imline(gca,[]);
    %apparently there is no way to set the width or cursor style of imlines
    %currwdith = findprop(sectionline,'LineWidth')
    setColor(sectionline,'yellow')
    
    %api = iptgetapi(sectionline);
    % prevent resizing or dragging of section outside of image
    % doesn't affect initial drawing of line since the api's handle is needed
    %fcn = makeConstrainToRectFcn('imline',...
    %                          get(gca,'XLim'),get(gca,'YLim'));
    %api.setPositionConstraintFcn(fcn);
    position = sectionline.getPosition();
    [xi yi] = EnforceBounds(position,handles);
    %ell=line(xi,yi);
    %set(ell,'color','g','linewidth',3)
    if xi(1)==xi(2)
        errordlg('ErrorTests:convertTest', 'Interactive mode doesnt work for perfectly vertical lines or when one or more endpoints are outside the plot area.  Try non-interactive column-mode instead.')
    end
    
    %LinesAndMarkers=get(sectionline,'Children');
    %setColor(LinesAndMarkers,'yellow')
    
    %save line endpoints in UserData of image
    set(handles.Image,'UserData',[xi yi])
    
    ellslope=(yi(2)-yi(1))/(xi(2)-xi(1));
    elloffset=yi(2)-(ellslope*xi(2));
    if abs(ellslope) < 1.0   %use x's to calculate y's
        ellnumber=abs(round((xi(2)-xi(1))));
        if xi(2) > xi(1)
            xset=(round(xi(1)):round(xi(2)));
        else
            xset=(round(xi(2)):round(xi(1)));
        end
        yset=round((ellslope*xset)+elloffset);
    else  % use y's to calculate x's
        ellnumber=abs(round((yi(2)-yi(1))));
        if yi(2) > yi(1)
            yset=(round(yi(1)):round(yi(2)));
        else
            yset=(round(yi(2)):round(yi(1)));
        end
        xset=round((yset-elloffset)/ellslope);
    end
    
    %Note dim1 is a 2-element vector
    %dim1=size(xset');
    %paramstring=sprintf('Endpoints of section are (%d, %d) and (%d, %d).', xset(1), yset(1), xset(dim1(1)), yset(dim1(1)));
    %disp(paramstring);
    
    %variable needed by OpenColorSection
    maxcounter=ellnumber;
    
    %	paramstring=sprintf('Endpoints of section are (%d, %d) and (%d, %d).', xi(1), yi(1), xi(2), yi(2));
    %	disp(paramstring);
    redplot=zeros(ellnumber,1);
    greenplot=zeros(ellnumber,1);
    blueplot=zeros(ellnumber,1);
    if (strcmp(imagetype,'color') == 1)
        temporary=(1:ellnumber)*handles.ScaleFactor;
        for i=1:ellnumber
            redplot(i) = double(tiffarray(yset(i),xset(i),1))/scalemax;
            greenplot(i) = double(tiffarray(yset(i),xset(i),2))/scalemax;
            blueplot(i) = double(tiffarray(yset(i),xset(i),3))/scalemax;
        end
        
        if (get(handles.ForceGray,'value')) % if grayscale forcing is on for color image
            grayline=zeros(maxcounter,1);
            for i=1:maxcounter
                grayline(i)=sqrt((redplot(i)*redplot(i)) + (greenplot(i)*greenplot(i)) + (blueplot(i)*blueplot(i)))/sqrt(3);
            end
            OpenGraySection
            
        else        OpenColorSection
            %following lines are for saving in UserData at the end
            redplot=redplot';
            greenplot=greenplot';
            blueplot=blueplot';
        end
        
    else %grayscale
        grayline=zeros(ellnumber,1);
        temporary=(1:ellnumber)*handles.ScaleFactor;
        for i=1:ellnumber
            grayline(i)= double(tiffarray(yset(i),xset(i)))/scalemax;
        end
        
        OpenGraySection
    end
    
    %enable data cursor to pick points off plot
    %set(datacursormode, 'enable', 'on', 'snaptodatavertex','on',
    %'displaystyle', 'window')
    
end   % end of cursor-drawing section


%save section data in plot's UserData field for later use
%transpose to save the line data as columns of UserData
if (strcmp(imagetype,'color') && (get(handles.ForceGray,'value') == 0))
    set(handles.SectionPlot,'UserData',[temporary' redplot' greenplot' blueplot'])
else
    set(handles.SectionPlot,'UserData',[temporary' grayline])
end

Contact us