Last active
December 8, 2022 23:33
-
-
Save MichaelTr7/71eaab442ece262ebd5dfe326ba0c9b7 to your computer and use it in GitHub Desktop.
MATLAB script that splits an image into blocks/tiles.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use the Split_Image_And_Reconstruct.m to modify and reconstruct the image blocks.