how to divide image into 3x3 blocks? '9 parts'
Show older comments
i have one rgb image, i converted it to gray then to binary, did imfill, further i want to split that image into 9 blocks i.e 3x3 , please help me
1 Comment
Jan
on 19 Jan 2014
Please show us, what you have tried so far and explain explicitly which problem occur.
Answers (2)
Matt J
on 19 Jan 2014
splitImage=mat2tiles(grayImage,[3,3]);
2 Comments
nitesh patil
on 20 Jan 2014
Matt J
on 20 Jan 2014
splitImage=mat2tiles(grayImage,ceil(size(grayImage)/3));
Image Analyst
on 19 Jan 2014
0 votes
See the FAQ for 2 different ways of doing it: http://matlab.wikia.com/wiki/FAQ#How_do_I_split_an_image_into_non-overlapping_blocks.3F
4 Comments
Image Analyst
on 20 Jan 2014
Try this. It doesn't get much simpler than this:
grayImage = imread('cameraman.tif'); % Read in image.
% Figure out where to divide it.
[rows, columns, numberOfColorChannels] = size(grayImage);
r3 = int32(rows/3);
c3 = int32(columns/3);
% Extract the 9 images.
image1 = grayImage(1:r3, 1:c3);
image2 = grayImage(1:r3, c3+1:2*c3);
image3 = grayImage(1:r3, 2*c3+1:end);
image4 = grayImage(r3+1:2*r3, 1:c3);
image5 = grayImage(r3+1:2*r3, c3+1:2*c3);
image6 = grayImage(r3+1:2*r3, 2*c3+1:end);
image7 = grayImage(2*r3+1:end, 1:c3);
image8 = grayImage(2*r3+1:end, c3+1:2*c3);
image9 = grayImage(2*r3+1:end, 2*c3+1:end);
% Display the 9 images.
subplot(3,3,1);
imshow(image1);
subplot(3,3,2);
imshow(image2);
subplot(3,3,3);
imshow(image3);
subplot(3,3,4);
imshow(image4);
subplot(3,3,5);
imshow(image5);
subplot(3,3,6);
imshow(image6);
subplot(3,3,7);
imshow(image7);
subplot(3,3,8);
imshow(image8);
subplot(3,3,9);
imshow(image9);
moahaimen talib
on 14 May 2017
Edited: moahaimen talib
on 14 May 2017
hi sir i did your code i made it read 50 images from a dir but it only segmented the last one i need it to segment all 50 images how?
clc;
clear all;
close all;
tic
%Load current directory
myFolder = 'binary';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);
resize=256;
if(ndims(jpegFiles)==3)
jpegFiles=rgb2gray(jpegFiles);
jpegFiles=jpegFiles(:,:,3);
else
jpegFiles=jpegFiles;
end
for k = 1:length(jpegFiles)
% % resize=256;
% % resize
%
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
% Figure out where to divide it.
[rows, columns, numberOfColorChannels] = size(imageArray);
r3 = int32(rows/3);
c3 = int32(columns/3);
% Extract the 9 images.
image1 = imageArray(1:r3, 1:c3);
image2 = imageArray(1:r3, c3+1:2*c3);
image3 = imageArray(1:r3, 2*c3+1:end);
image4 = imageArray(r3+1:2*r3, 1:c3);
image5 = imageArray(r3+1:2*r3, c3+1:2*c3);
image6 = imageArray(r3+1:2*r3, 2*c3+1:end);
image7 = imageArray(2*r3+1:end, 1:c3);
image8 = imageArray(2*r3+1:end, c3+1:2*c3);
image9 = imageArray(2*r3+1:end, 2*c3+1:end);
% Display the 9 images.
subplot(3,3,1);
imshow(image1);
subplot(3,3,2);
imshow(image2);
subplot(3,3,3);
imshow(image3);
subplot(3,3,4);
imshow(image4);
subplot(3,3,5);
imshow(image5);
subplot(3,3,6);
imshow(image6);
subplot(3,3,7);
imshow(image7);
subplot(3,3,8);
imshow(image8);
subplot(3,3,9);
imshow(image9);
end
but i tried to make the loop variable like this
image1(k) = imageArray(1:r3, 1:c3);
image2(k) = imageArray(1:r3, c3+1:2*c3);
image3(k) = imageArray(1:r3, 2*c3+1:end);
image4(k) = imageArray(r3+1:2*r3, 1:c3);
image5(k) = imageArray(r3+1:2*r3, c3+1:2*c3);
image6(k) = imageArray(r3+1:2*r3, 2*c3+1:end);
image7 (k)= imageArray(2*r3+1:end, 1:c3);
image8(k) = imageArray(2*r3+1:end, c3+1:2*c3);
image9 (k)= imageArray(2*r3+1:end, 2*c3+1:end);
but this error message appeared
Subscripted assignment dimension mismatch.
Error in rgn (line 43)
image1(k) = imageArray(1:r3, 1:c3);
thank you
moahaimen talib
on 14 May 2017
Edited: moahaimen talib
on 14 May 2017
i think i solved the problem by putting
figure(k);
subplot(3,3,1);
imshow(image1);
subplot(3,3,2);
imshow(image2);
subplot(3,3,3);
imshow(image3);
subplot(3,3,4);
imshow(image4);
subplot(3,3,5);
imshow(image5);
subplot(3,3,6);
imshow(image6);
subplot(3,3,7);
imshow(image7);
subplot(3,3,8);
imshow(image8);
subplot(3,3,9);
imshow(image9);
--------------------------------------------------------------
now it shows all segmented images but how can i store each part and extract features for them and store them in good dataset for classification?
-------------------------------------------------------------------
i could make this instruction it will extract features for image1 for all 20 images but i need it for all 9 parts(image1...image9)
% Extract the 9 images.
image1 = imageArray(1:r3, 1:c3);
GLCM = graycomatrix(image1,'Offset',[0 2]);
Resultstats = graycoprops(GLCM,{'energy','Homogeneity'})
image2 = imageArray(1:r3, c3+1:2*c3);
image3 = imageArray(1:r3, 2*c3+1:end);
image4 = imageArray(r3+1:2*r3, 1:c3);
image5 = imageArray(r3+1:2*r3, c3+1:2*c3);
image6 = imageArray(r3+1:2*r3, 2*c3+1:end);
image7 = imageArray(2*r3+1:end, 1:c3);
image8 = imageArray(2*r3+1:end, c3+1:2*c3);
image9 = imageArray(2*r3+1:end, 2*c3+1:end);
Walter Roberson
on 15 Dec 2018
atiqah ghaffar comments to Image Analyst
Thank you. This helps a lot.
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!