enter image description here

I have a 8 bit gray level image where if the value = 122 it means no information, value = 0 means highly negative, and value = 255 means highly positive. I want to connect as many positive information pixels with each other as possible to form as large an area as possible (and not let accidental negative values affect it), and get all the contour of it (which I will then filter out based on # of positive dots and total area of the contour). The ideal contours are the red lines I have drawn onto the image above, and the full image is below.

enter image description here

What is the best strategy to go about doing this?

The 3rd image is a zoomed in view of the image to show the pixels after filtering / processing just looks like that.

[enter image description here3

  • $\begingroup$ have you got actual data or just this resampled image? people are going to need something to experiment with, if you don't just want random ideas. -- something based on a median might serve the purpose. I don't mean just applying a median kernel. $\endgroup$ Commented Jan 11, 2023 at 8:36
  • $\begingroup$ The 2nd png image is the actual data. I normalized the values to 0 - 255, with 0 being definitely negative, 255 being definitely positive, and the middle level being no information. $\endgroup$
    – MistaZ
    Commented Jan 11, 2023 at 14:41
  • $\begingroup$ those pixels look resampled/resized. is this a screenshot? that picture definitely doesn't just contain three levels (black, white, single middle level) $\endgroup$ Commented Jan 11, 2023 at 14:46
  • $\begingroup$ Nope this is indeed the actual image, here's a zoomed in view of the actual pixels $\endgroup$
    – MistaZ
    Commented Jan 11, 2023 at 14:49
  • $\begingroup$ how is this image made? is resizing/resampling happening at any place in the previous processing? $\endgroup$ Commented Jan 11, 2023 at 14:49

1 Answer 1


The idea I have is as following:

  1. Treat data as a density measure.
  2. Values > 122, positive density, Values < 122 negative.
  3. Cluster them by threshold.

This is how it goes:

  1. Scale image into [0, 1] range.
  2. Shift the values by 122 / 255 so zero value are zero.
  3. Convolve with Gaussian Kernel.
  4. Apply threshold.
  5. Clean small artifacts.
  6. Do connected components.
  7. Draw contour of connected components.

I will show steps 1-4.

  • The input image

enter image description here

  • The surface after convolution (Hills and valleys)

enter image description here

  • Image after thresholding (Basically which have the same baseline of density)

enter image description here

What I'd do next to finish this:

  1. Tweak parameters.
  2. Clean small artifacts using morphological operations.
  3. Draw the contours bases on the connected components.

The MATLAB code:


zeroLvl = 122 / 255;
thrLvl = 0.01;

mI = imread('https://i.sstatic.net/QMTb5.png');
mI = mI(:, :, 1);
mI = im2double(mI);


mK = fspecial('gaussian', [25, 25], 4.5);

mB = mI - zeroLvl;
mP = imfilter(mB, mK, 'replicate');

surf(mP, 'EdgeColor', 'none');

mT = mP > thrLvl;


sCC = bwconncomp(mT);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.