Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the most dense regions in an image?

Consider a black and white image like this

alt text

What I am trying to do is to find the region where the white points are most dense. In this case there are 20-21 such dense regions (i.e. the clusters of points makes a dense region).

Can anyone give me any hint on how this can be achieved?

like image 613
Tasbeer Avatar asked Jan 04 '10 19:01

Tasbeer


1 Answers

If you have access to the Image Processing Toolbox, you can take advantage of a number of filtering and morphological operations it contains. Here's one way you could approach your problem, using the functions imfilter, imclose, and imregionalmax:

% Load and plot the image data:
imageData = imread('lattice_pic.jpg');  % Load the lattice image
subplot(221);
imshow(imageData);
title('Original image');

% Gaussian-filter the image:
gaussFilter = fspecial('gaussian', [31 31], 9);  % Create the filter
filteredData = imfilter(imageData, gaussFilter);
subplot(222);
imshow(filteredData);
title('Gaussian-filtered image');

% Perform a morphological close operation:
closeElement = strel('disk', 31);  % Create a disk-shaped structuring element
closedData = imclose(filteredData, closeElement);
subplot(223);
imshow(closedData);
title('Closed image');

% Find the regions where local maxima occur:
maxImage = imregionalmax(closedData);
maxImage = imdilate(maxImage, strel('disk', 5));  % Dilate the points to see
                                                  % them better on the plot
subplot(224);
imshow(maxImage);
title('Maxima locations');

And here's the image the above code creates:

enter image description here

To get things to look good I just kept trying a few different combinations for the parameters for the Gaussian filter (created using fspecial) and the structuring element (created using strel). However, that little bit of trial and error gave a very nice result.

NOTE: The image returned from imregionalmax doesn't always have just single pixels set to 1 (to indicate a maxima). The output image often contains clusters of pixels because neighboring pixels in the input image can have equal values, and are therefore both counted as maxima. In the code above I also dilated these points with imdilate just to make them easier to see in the image, which makes an even bigger cluster of pixels centered on the maxima. If you want to reduce the cluster of pixels to a single pixel, you should remove the dilation step and modify the image in other ways (add noise to the result or filter it, then find the new maxima, etc.).

like image 117
gnovice Avatar answered Oct 08 '22 03:10

gnovice