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

조회 수: 1 (최근 30일)
NC
NC 2018년 6월 25일
댓글: NC 2018년 6월 26일
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

채택된 답변

Guillaume
Guillaume 2018년 6월 25일
편집: Guillaume 2018년 6월 25일
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].
  댓글 수: 1
NC
NC 2018년 6월 26일
Thank you very much, The problem was solved by your instructions.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Read, Write, and Modify Image에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by