Yuita A. Sari

“If you want to be a writer, you must do two things : read a lot and write a lot. There's no way around these two things that I'm aware of, no shortcut.” ― Stephen King

[Matlab] How to Split Image into Small Parts


Split image is one of methods for understanding image in details. Usually, this function is applied in several programs of computer vision or pattern recognition.  Before we code, prepare a picture which will be split into small dimensions. For instance,  you can see a cat as shown below  (Is it right that a cat is so innocent ?😀 ) . I called this image file with “Kucing.png”

From the image above, now we will split in, and put every single part into a folder which named “folderPus”. The results of running program is shown below.  How build a function of split image in Matlab code? Well, let check this out!

Screen Shot 2013-08-19 at 5.18.26 AM

Here some codes in Matlab. I have explained through comments among codes.  You can save this function using file name “Split.m”

% Created by Yuita Arum Sari
% Input yang dibutuhkan adalah
% file : nama file tempat kita meletakkan file gambar
% dimensi : ukuran dimensi yang digunakan untuk memotong gambar menjadi
% bagian yang lebih kecil
% folderTitle : folder tempat menyimpan file-file gambar dengan dimensi
% yang lebih kecil

function Split(file,dimensi,folderTitle);

[pathstr, name, ext] = fileparts(file);
[panjang lebar tinggi]=size(gambar);

% pecahan dimensi panjang
% banyaknya kotak dari panjang berdasarkan dimensi
for i=1:panjang;

% pecahan dimensi lebar
% banyaknya kotak dari lebar berdasarkan dimensi
for i=1:lebar;

% menambahkan elemen 0 diatas data panjang
% menambahkan elemen 0 diatas data lebar

folder=(folderTitle); % mendeklarasikan nama folder
mkdir(folder); % mebuat folder baru
% relative path yang digunakan untuk menyimpan file gambar yang telah
% terpotong kecil-kecil
basePath = (['/Users/yuitaarumsari/Documents/testing/',folder])

for k=1:size(p,1)
    for i=1:a
        for j=1:size(l,1)
            baseFileName = sprintf(([name,'-','%d.png']), j+jumlah*(a-1));
            fullFileName = fullfile(basePath, baseFileName);
            imwrite(g, fullFileName, 'png');

And you can run function above using these codes

file = '/Users/yuitaarumsari/Documents/testing/Kucing.png';
dimensi = 100;
folderTitle = 'folderPus';
Split(file, dimensi, folderTitle);

Well, that’s my simple case and you can improve or modify the code based on your necessity. Happy coding!🙂

2 thoughts on “[Matlab] How to Split Image into Small Parts

  1. Wew…. saknoe kucinge di mutilasi😦

  2. @Bang Luqman potone tok pak sing dimutilasi….😀

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