MATLAB: Blockproc not working as I expected

blockprocimage analysis

Hello, I am having problems with blockproc.
I have an black image with white spots that I want to basic threshold locally rather than globally – that is to perform a basic threshold on tiles.
im127.png
IM=double(getimage(handles.axes4)); [rows,cols]=size(IM); %Get image from axes component
IM=IM-min(IM(:)); %Background subtract
% Number of tiles or chunks you want it divide up into.
xtiles =4; ytiles= 4;
%Numbers pixels per tile to maintain integer number of tiles
nx=floor(cols/xtiles); ny=floor(rows/ytiles);
Call my "LocallythresholdImage" function to define a level for binarising the image where
level = average + 2* standard deviations of the image
centroids=LocallythresholdImage(IM,nx,ny,f) %f=2 i.e. number of standard deviatins above the average
function centroids=LocallythresholdImage(IM,m,n,f)
%Uses the thresh function below and then creates a binary image using local
%threshold of size mxn. I
IM=double(IM); %make sure its a double
%Use blockproc uses my "thresh" function
myfun = @(block_struct) thresh3(block_struct.data,f); % myfun = @(block_struct) thresh2(block_struct.data,f);
%I2 = blockproc(Orig,[200 200],myfun,'BorderSize', [3 3], 'TrimBorder', true);
centroids = blockproc(IM,[m n],myfun,'BorderSize', [3 3], 'TrimBorder', true);
function [centroids]=thresh3(IM,f)
%n=str2num(get(handles.editThreshn,'String'));
%Thresholds image based on mean and S.D.
image=double(IM)-min(IM(:));
threshold=mean(image(:)) + f*std(image(:));
lessThanThreshold = image < threshold;
imageThresholded = image;
imageThresholded(lessThanThreshold) = 0;
regmax = imregionalmax(imageThresholded);
s = regionprops(regmax,double(image), 'WeightedCentroid');
format bank
centroids = cat(1, s.WeightedCentroid);
for each tile, the centroids are calculated correctly, but in my top level function "centroid=LocallythresholdImage(IM,nx,ny,f) ", centroids is empty

Best Answer

Change your 'TrimBorder', true to 'TrimBorder', false. As it is you're removing a border of size 3 on each side of your output of thresh3.
Note that this thresh3 function is not well written. image should not be used as a variable name (it's a matlab function), format bank has no place in there (it only affects command line display) and the cat(1, ...) is completely wrong. I'm not sure what you're trying to do with that. There shouldn't be more than one centroid, but if there were, concatenating them all would result in an error in blockproc since the blocks would return arrays of different sizes.