image thumbnail

Mix color and black and white in the same picture -custom colormaps

by

 

10 Mar 2008 (Updated )

This GUI will allow you to take a color image and set regions of it to be black and white.

ColorMix(varargin)
function varargout = ColorMix(varargin)
% COLORMIX M-file for ColorMix.fig
%      COLORMIX, by itself, creates a new COLORMIX or raises the existing
%      singleton*.
%
%      H = COLORMIX returns the handle to a new COLORMIX or the handle to
%      the existing singleton*.
%
%      COLORMIX('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in COLORMIX.M with the given input arguments.
%
%      COLORMIX('Property','Value',...) creates a new COLORMIX or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ColorMix_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ColorMix_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ColorMix

% Last Modified by GUIDE v2.5 15-Feb-2008 14:23:03

% Copyright 2008 - 2008 The MathWorks, Inc.

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ColorMix_OpeningFcn, ...
                   'gui_OutputFcn',  @ColorMix_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before ColorMix is made visible.
function ColorMix_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ColorMix (see VARARGIN)

% Choose default command line output for ColorMix
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ColorMix wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = ColorMix_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in browse.
function browse_Callback(hObject, eventdata, handles)
% hObject    handle to browse (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

set(handles.addroi,'Enable','on');
set(handles.SaveImage,'Enable','on');
set(handles.savetext,'Visible','off');
handles.numberofaxis =1;


[handles.filename,pathname] = uigetfile('*.*');
rgbX = imread(fullfile(pathname,handles.filename));
[handles.X, handles.map] = rgb2ind(rgbX,256);
handles.grayX = ind2gray(handles.X, handles.map);
[handles.grayX ,handles.graymap] = gray2ind(handles.grayX,size(handles.map,1));


 %image(handles.grayX);colormap(handles.graymap);
imshow(handles.grayX,handles.graymap);



hold on;
handles.axes1 =gca;
set(gca,'YTickLabel',[]);
set(gca,'XTickLabel',[]);
set(gca,'YTick',[]);
set(gca,'XTick',[]);
iptsetpref('ImshowAxesVisible','off')
% Update handles structure

handles.graymaphistory{handles.numberofaxis} = handles.graymap;
handles.axislist{handles.numberofaxis} = handles.axes1;
guidata(hObject, handles);

% --- Executes on button press in addroi.
function addroi_Callback(hObject, eventdata, handles)
% hObject    handle to addroi (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

figH = figure;


%image(handles.X);colormap(handles.map);
imshow(handles.X,handles.map);



roicoord = roipoly;
close(figH);

handles.numberofaxis = handles.numberofaxis+1; 

if(handles.numberofaxis >1)
    set(handles.undolast,'Enable','on');
end

temp = double(handles.X(roicoord));
handles.graymap =double(handles.graymap);
handles.graymaphistory{handles.numberofaxis-1} = handles.graymap;
origmapsize = size(handles.graymap,1);
for ind =  1: numel(temp)
    if(temp(ind) <= origmapsize)
        handles.graymap =[handles.graymap; handles.map(temp(ind)+1,1:3)];
        mask = (temp == temp(ind));
        temp(mask) = size(handles.graymap,1);
    end
end

I =double(0*handles.X);
I(roicoord) = double(temp);

imAlphaData = 0*I;
imAlphaData(roicoord) =1;

%**************%
%I_handle = image(I);colormap(handles.graymap);
 I_handle = imshow(ind2rgb(I,handles.graymap));
%**************%


set(I_handle,'AlphaData',imAlphaData);


% Update handles structure

handles.axislist{handles.numberofaxis} = I_handle;
guidata(hObject, handles);

% --- Executes on button press in undolast.
function undolast_Callback(hObject, eventdata, handles)
% hObject    handle to undolast (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.graymap = handles.graymaphistory{handles.numberofaxis -1};
handles.graymaphistory{handles.numberofaxis} =[];

delete(handles.axislist{handles.numberofaxis});
colormap(handles.graymap);
handles.numberofaxis = handles.numberofaxis-1;

if(handles.numberofaxis ==1 )
    set(handles.undolast,'Enable','off');
end
% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in SaveImage.
function SaveImage_Callback(hObject, eventdata, handles)
% hObject    handle to SaveImage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

x = getframe;
ind = find (handles.filename == '.');
tempname = handles.filename(1:ind-1);
imwrite(x.cdata,[tempname,'mod.bmp']);
set(handles.savetext,'Visible','on');
set(handles.savetext,'String',['The file has been saved as: ',tempname,'mod.bmp']);


Contact us