function varargout = assignment_1(varargin)

% ASSIGNMENT_1 M-file for assignment_1.fig
%      ASSIGNMENT_1, by itself, creates a new ASSIGNMENT_1 or raises the existing
%      singleton*.
%
%      H = ASSIGNMENT_1 returns the handle to a new ASSIGNMENT_1 or the handle to
%      the existing singleton*.
%
%      ASSIGNMENT_1('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ASSIGNMENT_1.M with the given input arguments.
%
%      ASSIGNMENT_1('Property','Value',...) creates a new ASSIGNMENT_1 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before assignment_1_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to assignment_1_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 assignment_1

% Last Modified by GUIDE v2.5 30-Nov-2011 02:28:11

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @assignment_1_OpeningFcn, ...
                   'gui_OutputFcn',  @assignment_1_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 assignment_1 is made visible.
function assignment_1_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 assignment_1 (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = assignment_1_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile({'*.jpg';'*.bmp';'*.gif';'*.*'}, 'Pick an Image File');
orginal = imread([pathname,filename]);
%orginal = rgb2gray(orginal);
axes(handles.axes1);
imshow(orginal);

handles.orginal = orginal;
guidata(hObject, handles);

%%%%%%%%%%%%%%%%%%gray%%%%%%%%%%%%%%%%%%

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Im = handles.orginal;
Gray_Im=uint8(zeros(size(Im,1),size(Im,2)));

%[r,c]=size(Im);

%GIm=uint8(zeros(r,c));

for i=1:size(Im,1)
      for j=1:size(Im,2)
          Gray_Im(i,j)=0.2989*Im(i,j,1)+0.5870*Im(i,j,2)+0.1140*Im(i,j,3);
      end
end
axes(handles.axes2);
imshow(Gray_Im);

%%%%%%%%%%%%%%%%%%log%%%%%%%%%%%%%%%%%%

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

Im = handles.orginal;
Im = rgb2gray(Im);
Im = im2double(Im);

Im = Im*2^10;
c = 1/log(1+2^10);
 
log_trans_im = c*log(1 + double(Im));

axes(handles.axes2);
imshow(log_trans_im);

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

a = handles.orginal;                                  


a=rgb2gray(a);

axes(handles.axes1);
imshow(a);


[m n]=size(a);

r2=max(max(a)) ;      %this function is used for finding the max value of a pixel in an image
r1=min(min(a)) ;         %this function is used for finding the min value of a pixel in an image
s1=0;                             % this is the min and max of an image i.e. 0-255 gray levels
s2=255;
for i=1:m            %here we use 2 for loops for traversing the image in x & y coordinate
for j=1:n
if(a(i,j) <=r1)%we nave the basic formula (x-x1)/(x2-x1) = (y-y1)/(y2-y1)
s(i,j)=(r1/s1)*(a(i,j));      % we apply this formula on th epoint as shown in figure
else  %and then find the new formula
    if(a(i,j) >=r2)              % the logic is that we shift white pixels on brighter side and
s(i,j)=(((255-s2)/(255-r2))*(a(i,j)-r2))+s2; % black pixels on darker side and stretched the
    else % middle pixels having medium gray levels
s(i,j)=(((s2-s1)/(r2-r1)*(a(i,j)-r1))+(s1));
    end
end
end
end
axes(handles.axes2);
imshow(s);



% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton3.
function pushbutton3_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

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

Im = handles.orginal;
im=rgb2gray(Im);

axes(handles.axes1);
subplot(1,2,1);imshow(im);
subplot(1,2,2);Imagesc(im);  % display

imhist(im);
im_histeq=histeq(im);

axes(handles.axes2);
subplot(1,2,1);imshow(im_histeq);  % display
subplot(1,2,2);imhist(im_histeq);


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over popupmenu1.
function popupmenu1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on key press with focus on popupmenu1 and none of its controls.
function popupmenu1_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)



%%%%%%%%%%%%%%%%%%bit%%%%%%%%%%%%%%%%%%

% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
f = handles.orginal;
f=rgb2gray(f);

axes(handles.axes1);
imshow(f);

[m n] = size(f);
c = cell(1,8) ;
for i = 1:m
for j = 1:n
b = f(i,j);
if ( b >= 128)
b = b-128; c{1,1}(i,j) = 1;
else
c{1,1}(i,j) = 0;
end
if (b >= 64)
b = b - 64; c{1,2}(i,j) = 1;
else
c{1,2}(i,j) = 0;
end
if (b >= 32)
b = b - 32; c{1,3}(i,j) = 1;
else
c{1,3}(i,j) = 0;
end
if (b >= 16)
b = b - 16; c{1,4}(i,j) = 1;
else
c{1,4}(i,j) = 0;
end
if (b >= 8)
b = b - 8; c{1,5}(i,j) = 1;
else
c{1,5}(i,j) = 0;
end
if (b >= 4)
b = b - 4; c{1,6}(i,j) = 1;
else
c{1,6}(i,j) = 0;
end
if (b >= 2)
b = b - 2; c{1,7}(i,j) = 1;
else
c{1,3}(i,j) = 0;
end
if (b >= 1)
b = b - 1; c{1,8}(i,j) = 1;
else
c{1,8}(i,j) = 0;
end
end
end
f1 = c{1,1}; f2 = c{1,2}; f3 = c{1,3}; f4 = c{1,4};
f5 = c{1,5}; f6 = c{1,6}; f7 = c{1,7}; f8 = c{1,8};
figure,imshow(f1),title('Bit-Plane 0');
figure,imshow(f2),title('Bit-Plane 1');
figure,imshow(f3),title('Bit-Plane 2');
figure,imshow(f4),title('Bit-Plane 3');
figure,imshow(f5),title('Bit-Plane 4');
figure,imshow(f6),title('Bit-Plane 5');
figure,imshow(f7),title('Bit-Plane 6');
figure,imshow(f8),title('Bit-Plane 7'); 

axes(handles.axes2);
imshow(f1);


% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2


% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
