Apply a function to each block of an image

조회 수: 2 (최근 30일)
NC
NC 2018년 6월 28일
I divide the input image into blocks of 57*57 and then apply the function "emresample" to each block and draw the probability map and periodicity map. For that I used the following functions. Then I got the error Undefined function or variable 'pmap'.Error in blockofImageNew (line 11)pmap2 = pmap;. How to solve it ?
.m
function [Pmap,cen,RN,RS,Y,varargout] = emresample(img,N,varargin)
RN = 0;
RS = 0;
MAXITER = 100;
EPSILON = 1e-3;
MAXBLOCKS = 3.5e3;
p0 = 1/256;
if(size(img,3)~=1)
error('Image must be grayscale.')
end
if(nargin>2)
verbose = (varargin{1}=='verbose');
else
verbose = 0;
end
wlen = 2*N+1; % window length
cen = floor(wlen^2/2) + 1; % center element
[height width] = size(img);
imcol = im2col(img,[wlen wlen]);
[~,nblocks] = size(imcol);
Y = imcol';
Y(:,cen) = []; % delete center element
fprintf('imcol :');
% initialize parameters
alpha = rand(1,wlen^2-1)';
alpha = alpha./sum(alpha(:)); % normalize weights
sigma = 0.75;
y = img(N+1:end-N,N+1:end-N);
y = y(:);
idx_rand = randperm(nblocks);
idx = idx_rand(1:min(MAXBLOCKS,nblocks));
Yt = Y(idx,:);
yt = y(idx,:);
if(verbose==1)
fprintf('Current EM Iteration\n')
end
for iter = 1:MAXITER
prev = alpha;
if(verbose==1)
disp(iter)
end
% E-step
R = yt - Yt*alpha; % find residuals
P = 1/(sigma*sqrt(2*pi)) * exp(-R.^2/(2*sigma.^2));
w = P./(P+p0);
RN = (RN+R(MAXITER))/length(R);
RS= RS+R(MAXITER);
% M-step
sigma = sqrt( sum(w.*R.^2)/sum(w) );
alpha = (Yt'*diag(w)*Yt) \ Yt'*diag(w)*yt; %new alpha
if(max(abs(prev-alpha))<EPSILON)
break;
end
end
if(verbose==1)
fprintf('Number of iterations = %i\n',iter);
fprintf('sigma = %.5f\n',sigma);
end
Pmap = 1/(sigma*sqrt(2*pi)) * exp(-(y-Y*alpha).^2/(2*sigma.^2));
Ndecimals = 2 ;
f = 10.^Ndecimals ;
Pmap= round(f*Pmap)/f ;
Pmap = reshape(Pmap,[height-2*N width-2*N]);
fprintf('Pmap Value : ');
disp(Pmap);
% optional outputs
if(nargout==1)
varargout{1} = alpha;
elseif(nargout==2)
varargout{1} = alpha;
varargout{2} = sigma;
elseif(nargout==3)
varargout{1} = alpha;
varargout{2} = sigma;
varargout{3} = iter;
end
%%YourFunction2.m
function [result,pmap,fmap] = YourFunction(block_of_image)
N=2
[Pmap,fmap] = emresampleN(block_of_image,N,'verbose');
fmap = fft2c(Pmap);
result = fft2c(Pmap);
%%script.m
clc;
clear all;
close all;
img = imread('Original\ucid\ImageDataSet\ucid00320sp.tif');
img = rgb2gray(img);
img= double(img);
aux_fun = @(block_info) YourFunction2(block_info.data);
result = blockproc(img,[57 57], aux_fun, 'TrimBorder', false)
pmap2 = Pmap;
fmap2 = fmap;
subplot(2, 3, 3);
imshow(img);
figure;
subplot(131)
imshow(img,[])
subplot(132)
imshow(pmap2,[])
subplot(133)

답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by