MATLAB: Spliting an image into 4 blocks and apply a function to each block

image processing

I want to split an image into 4 blocks and apply a function to each block and display the result as one figure. My code is given below. But it gives the error " Error using horzcat. Dimensions of matrices being concatenated are not consistent". How to solve this and get my work done ?
clc;
close all;
workspace;
format longg;
format compact;
fontSize = 20;
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'Photoshop_Resampled\ucid00059u.tif';
rgbImage = imread(baseFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 3, 1);
imshow(rgbImage, []);
imgr = rgb2gray(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Get the rows and columns to split at,
% Taking care to handle odd-size dimensions:
col1 = 1;
col2 = floor(columns/2);
col3 = col2 + 1;
row1 = 1;
row2 = floor(rows/2);
row3 = row2 + 1;
lowerLeft = imcrop(imgr, [col1 row3 col2 row2]);
subplot(2, 3, 2);
imshow(lowerLeft);
upperLeft = imcrop(imgr, [col1 row1 col2 row2]);
subplot(2, 3, 2);
imshow(upperLeft);
upperRight = imcrop(imgr, [col3 row1 columns - col2 row2]);
subplot(2, 3, 2);
imshow(upperRight);
lowerRight = imcrop(imgr, [col3 row3 columns - col2 rows - row2]);
subplot(2, 3, 2);
imshow(upperRight);
blockArray = [upperLeft, lowerLeft,upperRight, lowerLeft];
for i=1:length(blockArray)
imIdx = 8;
% RESAMPLING PARAMETERS
resampleRatio = 1.1;
N = 2; % window size
M = 16 % block size
im = baseFileName;
[H,W,~] = size(imgr);
for k = 1:length(resampleRatio)
r = resampleRatio(k);
img = double(blockArray(i));
% img = imresize(img,[M M],'bilinear');
pmap = emresampleN(img,N,'verbose');
fmap = fft2c(pmap);
%disp(fmap);
% display p-map
subplot(2, 3, 3);
imshow(blockArray(i));
figure;
subplot(131)
imshow(img,[])
subplot(132)
imshow(pmap,[])
subplot(133)
imshow(abs(rmcenter(fmap)),[]);
end
end

Best Answer

I strongly suspect that your blockArray is meant to be a cell array, hence dthe line
blockArray = [upperLeft, lowerLeft,upperRight, lowerLeft];
should be
blockArray = {upperLeft, lowerLeft,upperRight, lowerLeft};
It seems you also don't know how to index cell arrays. The line
img = double(blockArray(i));
should be
img = blockArray{i};
Finally, the line
[H,W,~] = size(imgr);
is completely pointless. You don't use H or W and in any case, you already know the size of imgr, it's [rows, columns, numberOfColorBands].