Skip to content

Instantly share code, notes, and snippets.

@MichaelTr7
Last active December 8, 2022 23:33
Show Gist options
  • Save MichaelTr7/71eaab442ece262ebd5dfe326ba0c9b7 to your computer and use it in GitHub Desktop.
Save MichaelTr7/71eaab442ece262ebd5dfe326ba0c9b7 to your computer and use it in GitHub Desktop.
MATLAB script that splits an image into blocks/tiles.
clf;
Image = imread("peppers.png");
[Image_Height,Image_Width,Number_Of_Colour_Channels] = size(Image);
Block_Size = 50;
Number_Of_Blocks_Vertically = ceil(Image_Height/Block_Size);
Number_Of_Blocks_Horizontally = ceil(Image_Width/Block_Size);
Image_Blocks = struct('Blocks',[]);
Index = 1;
for Row = 1: +Block_Size: Number_Of_Blocks_Vertically*Block_Size
for Column = 1: +Block_Size: Number_Of_Blocks_Horizontally*Block_Size
Row_End = Row + Block_Size - 1;
Column_End = Column + Block_Size - 1;
if Row_End > Image_Height
Row_End = Image_Height;
end
if Column_End > Image_Width
Column_End = Image_Width;
end
Temporary_Tile = Image(Row:Row_End,Column:Column_End,:);
%Storing blocks/tiles in structure for later use%
Image_Blocks(Index).Blocks = Temporary_Tile;
subplot(Number_Of_Blocks_Vertically,Number_Of_Blocks_Horizontally,Index); imshow(Temporary_Tile);
Index = Index + 1;
end
end
%***************************************************%
%Uncomment to save the images to seperate .jpg files%
%***************************************************%
% for Block_Index = 1: length(Image_Blocks)
% imwrite(Image_Blocks(Block_Index).Blocks,"Block" + num2str(Block_Index) + ".jpg");
% end
clf;
Image = imread("peppers.png");
[Image_Height,Image_Width,Number_Of_Colour_Channels] = size(Image);
Block_Size = 50;
Number_Of_Blocks_Vertically = ceil(Image_Height/Block_Size);
Number_Of_Blocks_Horizontally = ceil(Image_Width/Block_Size);
Image_Blocks = struct('Blocks',[]);
Index = 1;
for Row = 1: +Block_Size: Number_Of_Blocks_Vertically*Block_Size
for Column = 1: +Block_Size: Number_Of_Blocks_Horizontally*Block_Size
Row_End = Row + Block_Size - 1;
Column_End = Column + Block_Size - 1;
if Row_End > Image_Height
Row_End = Image_Height;
end
if Column_End > Image_Width
Column_End = Image_Width;
end
Temporary_Tile = Image(Row:Row_End,Column:Column_End,:);
%Storing blocks/tiles in structure for later use%
Image_Blocks(Index).Blocks = Temporary_Tile;
% subplot(Number_Of_Blocks_Vertically,Number_Of_Blocks_Horizontally,Index); imshow(Temporary_Tile);
Index = Index + 1;
end
end
%*************************************************************************%
%MODIFYING THE BLOCKS%
%*************************************************************************%
for Block_Index = 1: length(Image_Blocks)
if(mod(Block_Index,2) == 0)
%Darkening every other block%
Image_Blocks(Block_Index).Blocks = 0.5.*(Image_Blocks(Block_Index).Blocks);
end
end
%*************************************************************************%
%RECONSTRUCTING THE IMAGE%
%*************************************************************************%
Reconstructed_Image = zeros(size(Image));
Block_Index = 1;
for Row = 1: +Block_Size: Image_Height
for Column = 1: +Block_Size: Image_Width
Height_Of_Block = size(Image_Blocks(Block_Index).Blocks,1);
Width_Of_Block = size(Image_Blocks(Block_Index).Blocks,2);
Reconstructed_Image(Row:Row+Height_Of_Block-1,Column:Column+Width_Of_Block-1,:) = Image_Blocks(Block_Index).Blocks;
Block_Index = Block_Index + 1;
end
end
imshow(uint8(Reconstructed_Image));
clf;
Image = imread("peppers.png");
[Image_Height,Image_Width,Number_Of_Colour_Channels] = size(Image);
Block_Size = 50;
Number_Of_Blocks_Vertically = floor(Image_Height/Block_Size);
Number_Of_Blocks_Horizontally = floor(Image_Width/Block_Size);
Image_Blocks = struct('Blocks',[]);
Index = 1;
for Row = 1: +Block_Size: Number_Of_Blocks_Vertically*Block_Size
for Column = 1: +Block_Size: Number_Of_Blocks_Horizontally*Block_Size
Row_End = Row + Block_Size - 1;
Column_End = Column + Block_Size - 1;
if Row_End > Image_Height
Row_End = Image_Height;
end
if Column_End > Image_Width
Column_End = Image_Width;
end
Temporary_Tile = Image(Row:Row_End,Column:Column_End,:);
%Storing blocks/tiles in structure for later use%
Image_Blocks(Index).Blocks = Temporary_Tile;
subplot(Number_Of_Blocks_Vertically,Number_Of_Blocks_Horizontally,Index); imshow(Temporary_Tile);
Index = Index + 1;
end
end
%***************************************************%
%Uncomment to save the images to seperate .jpg files%
%***************************************************%
% for Block_Index = 1: length(Image_Blocks)
% imwrite(Image_Blocks(Block_Index).Blocks,"Block" + num2str(Block_Index) + ".jpg");
% end
%Getting all the images in the directory%
Folder_Directory = "";
File_Type = "png";
Images = dir(fullfile(Folder_Directory,"*." + File_Type));
for Image_Index = 1: length(Images)
Image_Name = Images(Image_Index).name;
Image = imread(fullfile(Folder_Directory,Image_Name));
File_Prefix = strsplit(Image_Name,".");
File_Prefix = File_Prefix{1};
[Image_Height,Image_Width,Number_Of_Colour_Channels] = size(Image);
Block_Size = 100;
Number_Of_Blocks_Vertically = floor(Image_Height/Block_Size);
Number_Of_Blocks_Horizontally = floor(Image_Width/Block_Size);
Image_Blocks = struct('Blocks',[]);
Index = 1;
for Row = 1: +Block_Size: Number_Of_Blocks_Vertically*Block_Size
for Column = 1: +Block_Size: Number_Of_Blocks_Horizontally*Block_Size
Row_End = Row + Block_Size - 1;
Column_End = Column + Block_Size - 1;
if Row_End > Image_Height
Row_End = Image_Height;
end
if Column_End > Image_Width
Column_End = Image_Width;
end
Temporary_Tile = Image(Row:Row_End,Column:Column_End,:);
%Storing blocks/tiles in structure for later use%
Image_Blocks(Index).Blocks = Temporary_Tile;
% subplot(Number_Of_Blocks_Vertically,Number_Of_Blocks_Horizontally,Index); imshow(Temporary_Tile);
Index = Index + 1;
end
end
mkdir(File_Prefix + "_Blocked/");
for Block_Index = 1: length(Image_Blocks)
imwrite(Image_Blocks(Block_Index).Blocks,fullfile(File_Prefix + "_Blocked/",File_Prefix + "_" + num2str(Block_Index) + ".jpg"));
end
end
@MichaelTr7
Copy link
Author

MichaelTr7 commented Mar 29, 2021

Output Results Preview

@febbyftrp
Copy link

Jepretan Layar 2021-12-12 pada 19 10 06
Hello, can you tell me how to fix this error while I want to save the splitting blocks? Thank you in advance.

In TA1 (line 158)
Error using imwrite>parse_inputs (line 523)
A filename must be supplied.

Error in imwrite (line 418)
[data, map, filename, format, paramPairs] = parse_inputs(varargin{:});

Error in TA1 (line 160)
imwrite(Image_Blocks(Block_Index).Blocks,fullfile(File_Prefix + "_Blocked/",File_Prefix +
"BlockDivision" + num2str(Block_Index) + ".png"));

@MichaelTr7
Copy link
Author

MichaelTr7 commented Dec 12, 2021

@febbyftrp Any chance you could attach the full code. I can’t seem to find anything in this code snippet that would cause issues.

@sabrid369
Copy link

sabrid369 commented Dec 13, 2021 via email

@febbyftrp
Copy link

febbyftrp commented Dec 14, 2021

@MichaelTr7 Here is my full code to split the blocks, I copy it from your github sir. Thank you so much for the help sir.

%Splitting the frames into 8x8 blocks
Folder_Directory = '/Users/febbyfitri/Documents/MATLAB/febby/AVI(Uncompressed)_subtraction';
File_Type = "png";
Images = natsortfiles(dir(fullfile(Folder_Directory,"*." + File_Type)));
for Image_Index = 1
    Image_Name = Images(Image_Index).name;
    Image = imread(fullfile(Folder_Directory,Image_Name));
    File_Prefix = strsplit(Image_Name,".");
    File_Prefix = File_Prefix{1};
    [Image_Height,Image_Width,Number_Of_Colour_Channels] = size(Image);

    % The first way to divide an image up into blocks is by using mat2cell()
    Block_SizeC = 240; %Columns in block.
    Block_SizeR = 135; % Rows in block.
    Number_Of_Blocks_Vertically = floor(Image_Height/Block_SizeR);
    Number_Of_Blocks_Horizontally = floor(Image_Width/Block_SizeC);
    Image_Blocks = struct('Blocks',[]);
    Index = 1;
    for Row = 1: +Block_SizeR: Number_Of_Blocks_Vertically*Block_SizeR
        for Column = 1: +Block_SizeC: Number_Of_Blocks_Horizontally*Block_SizeC

        Row_End = Row + Block_SizeR - 1;
        Column_End = Column + Block_SizeC - 1;

        if Row_End > Image_Height
           Row_End = Image_Height;
        end

        if Column_End > Image_Width
           Column_End = Image_Width;
        end

     
        Temporary_Tile = Image(Row:Row_End,Column:Column_End,:);
        %Storing blocks/tiles in structure for later use%
        Image_Blocks(Index).Blocks = Temporary_Tile;
        subplot(Number_Of_Blocks_Vertically,Number_Of_Blocks_Horizontally,Index); 
        imshow(Temporary_Tile);
        Index = Index + 1;
        end  
    end
    
    mkdir(File_Prefix + "_Blocked/");
    for Block_Index = 1: length(Image_Blocks)
        imwrite(Image_Blocks(Block_Index).Blocks,fullfile(File_Prefix + "_Blocked/",File_Prefix + "BlockDivision" + num2str(Block_Index) + ".png")); 
    end
end

@MichaelTr7
Copy link
Author

MichaelTr7 commented Dec 14, 2021

@febbyftrp I believe that the path must be added since this is an absolute path that MATLAB does not have access to. Relative paths in the MATLAB Current Folder side panel do not have to be added. To add your folder the following code can be added to the top of your script:

Folder_Directory = '/Users/febbyfitri/Documents/MATLAB/febby/AVI(Uncompressed)_subtraction';
addpath(Folder_Directory);

@febbyftrp
Copy link

I have already tried it sir, but still can't work. Does the version of Matlab affect sir? btw, I use Matlab 2018a @MichaelTr7

@MichaelTr7
Copy link
Author

MichaelTr7 commented Dec 27, 2021

@febbyftrp I don’t believe the version of MATLAB should have any effect on this. Is it possible to just load an image from that directory/folder using the imread() function?

@MichaelTr7
Copy link
Author

MichaelTr7 commented Feb 27, 2022

Use the Split_Image_And_Reconstruct.m to modify and reconstruct the image blocks.

Screen Shot 2022-02-27 at 5 56 47 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment