Blockprocessing with a counting function for bodies in a binary image results in incorrect density maps

Hi,
I am trying to register a sort of (not literal) 'population density map' with its corresponding image. I have my original massive image which is sent through a bunch of processing to isolate every single individual body and display them on a binary image. I then use bwconncomp in blockproc to get a population density map of original massive binary image, where each pixel has a value equal to the number of bodies counted in that pixel's corresponding block. I then hope to register this density map with its corresponding image of a different resolution.
I have two major problems, the second of which I only just discovered:
-----------
My first problem is that the scaling is not accurate because of rounding when determining block size. My population density map ends up being smaller than my target scaled image.
BlockSize is determined by the ratio of the dimensions of the target registration image and the massive image.
So, I have
BigIm, the image I'm trying to get a map of
regtarget, the image I'm trying to register the map to
block1 = size(BigIm,1)/size(regtarget,1);
blocksize1 = ceil(block1)
block2 = size(BigIm,2)/size(regtarget,2);
blocksize2 = ceil(block2);
PopMap = blockproc(BigIm,[blocksize1,blocksize2],bwconncomp) % callbacks hidden
The problem is when block1 and block2 are not integers - which, they basically never are. For an example I'm working with, size(BigIm) = [5000 5850], size(regtarget) = [436 511], size(PopMap) = [417 488], so around 20 pixels too small in either direction. Block1,block2 ends up being around 11.4x11.4, which then rounds to a 12x12 block. So since I have too few blocks, I have population map that ends up being smaller.
Scaling the image up to the size it needs to be using imresize() results in an increased count total, so that doesn't seem to be an option. I'm wondering if anyone can offer feedback on this - how to get these maps to the proper size without compromising the count number.
-----
My second problem resulted from a major oversight. While bwconncomp is great for counting the binary mask as a whole, I did not take into account that blockprocessing would split the bodies, resulting in a lot of bodies being counted twice if they cross over the edge of a block. So for instance, I may end up with 150,000 objects in BigIm, but sum(sum(PopMap)) gives me something like 260,000. If anyone has any suggestions at all on how to go about this, it would be much appreciated. I recognize I don't have much to offer at the point in regards to how I would think to go about this, so I apologize for that.
Any help or thoughts at all are appreciated. Have a great day!

1 Comment

So in regards to problem 2, I am thinking that if I could save the adjacent blocks, I could identify where pixels overlap from block to block, and subtract a body for the current block. But is it possible to save and reference previous blocks? Based on my recent research, it's not.

Sign in to comment.

Answers (1)

A diagram or image would surely help. blockproc() moved in jumps of the window size which must be an integer. If you want to have the same size image, you have to do what I do in my attached demos.
If you want to move over a pixel at a time, use imfilter(), conv2() or nlfilter().

Asked:

on 11 Dec 2015

Answered:

on 11 Dec 2015

Community Treasure Hunt

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

Start Hunting!