Complement Image (Negative Image)


Citra negatif merupakan citra yang nilai pikselnya berkebalikan dengan citra aslinya. Untuk citra grayscale 8-bit, apabila citra asli disimbolkan dengan I, maka negatif dari citra tersebut adalah I’ = 255-I. Contoh perhitungan nilai piksel dari citra negatif ditunjukkan pada gambar di bawah ini:

Untitled-1
Berikut ini merupakan pemrograman GUI Matlab mengenai citra komplemen/ citra negatif
1. Citra RGB (RGB Image)
RGB

2. Citra Grayscale (Grayscale Image)
Gray

3. Citra Biner (Binary Image)
bw

Source Code dan Citra komplemen dapat diunduh melalui laman berikut ini: Source Code

Sedangkan tampilan source codenya adalah:

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

% Last Modified by GUIDE v2.5 29-Sep-2015 00:12:38

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Negative_Image_OpeningFcn, ...
                   'gui_OutputFcn',  @Negative_Image_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 Negative_Image is made visible.
function Negative_Image_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 Negative_Image (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);
movegui(hObject,'center');
clc;clear;

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

% --- Outputs from this function are returned to the command line.
function varargout = Negative_Image_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('*.*');

if ~isequal(filename,0)

    Img = imread(fullfile(pathname,filename));
    [~,~,m] = size(Img);
    if m == 3
        axes(handles.axes1)
        imshow(Img)
        handles.Img = Img;
        guidata(hObject, handles)
    else
        msgbox('Please insert RGB Image')
    end
else
    return
end

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

% Hint: get(hObject,'Value') returns toggle state of radiobutton1
set(handles.radiobutton1,'Value',1)
set(handles.radiobutton2,'Value',0)
set(handles.radiobutton3,'Value',0)

Img = handles.Img;
axes(handles.axes1)
cla('reset')
imshow(Img)

R = Img(:,:,1);
G = Img(:,:,2);
B = Img(:,:,3);

axes(handles.axes2)
cla('reset')
h = histogram(R(:),256);
h.FaceColor = [1 0 0];
h.EdgeColor = 'r';
hold on

h = histogram(G(:),256);
h.FaceColor = [0 1 0];
h.EdgeColor = 'g';

h = histogram(B(:),256);
h.FaceColor = [0 0 1];
h.EdgeColor = 'b';
grid on
set(gca,'Xlim',[0 255])
hold off

% Image Complement
Img_Comp = imcomplement(Img);

axes(handles.axes3)
cla('reset')
imshow(Img_Comp)

R = Img_Comp(:,:,1);
G = Img_Comp(:,:,2);
B = Img_Comp(:,:,3);

axes(handles.axes4)
cla('reset')
h = histogram(R(:),256);
h.FaceColor = [1 0 0];
h.EdgeColor = 'r';
hold on

h = histogram(G(:),256);
h.FaceColor = [0 1 0];
h.EdgeColor = 'g';

h = histogram(B(:),256);
h.FaceColor = [0 0 1];
h.EdgeColor = 'b';
set(gca,'Xlim',[0 255])
grid on
hold off

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

% Hint: get(hObject,'Value') returns toggle state of radiobutton2
set(handles.radiobutton1,'Value',0)
set(handles.radiobutton2,'Value',1)
set(handles.radiobutton3,'Value',0)

Img = handles.Img;
Gray = rgb2gray(Img);

axes(handles.axes1)
cla('reset')
imshow(Gray)

axes(handles.axes2)
cla('reset')
h = histogram(Gray(:),256);
h.FaceColor = [0.5 0.5 0.5];
h.EdgeColor = [0.5 0.5 0.5];
set(gca,'Xlim',[0 255])
grid on

% Image Complement
Gray_Comp = imcomplement(Gray);
axes(handles.axes3)
cla('reset')
imshow(Gray_Comp)

axes(handles.axes4)
cla('reset')
h = histogram(Gray_Comp(:),256);
h.FaceColor = [0.5 0.5 0.5];
h.EdgeColor = [0.5 0.5 0.5];
set(gca,'Xlim',[0 255])
grid on

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

% Hint: get(hObject,'Value') returns toggle state of radiobutton3
set(handles.radiobutton1,'Value',0)
set(handles.radiobutton2,'Value',0)
set(handles.radiobutton3,'Value',1)

Img = handles.Img;
Gray = rgb2gray(Img);
bw = im2bw(Gray,graythresh(Gray));

axes(handles.axes1)
cla('reset')
imshow(bw)

axes(handles.axes2)
h = histogram(double(bw(:)),2);
h.FaceColor = [0 0 0];
h.EdgeColor = [0 0 0];
set(gca,'Xlim',[0 1])
grid on

% Image complement
bw_Comp = ~bw;

axes(handles.axes3)
cla('reset')
imshow(bw_Comp)

axes(handles.axes4)
h = histogram(double(bw_Comp(:)),2);
h.FaceColor = [0 0 0];
h.EdgeColor = [0 0 0];
set(gca,'Xlim',[0 1])
grid on

Save

Save

Save

Posted on September 29, 2015, in Pengolahan Citra and tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: