Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

peak_local_max attribute 'min_distance' issue #5598

Closed
taroko-mooncake opened this issue Oct 11, 2021 · 9 comments
Closed

peak_local_max attribute 'min_distance' issue #5598

taroko-mooncake opened this issue Oct 11, 2021 · 9 comments
Labels
🫂 Support User help and QA ❓ Needs info Needs more information or investigation

Comments

@taroko-mooncake
Copy link

taroko-mooncake commented Oct 11, 2021

Description

The attribute 'min_distance' in peak_local_max function is not working. Distance can be set at any integer and coordinates of centres remain unchanged.

Way to reproduce

distance = ndi.distance_transform_edt(binary_mask)
coords = peak_local_max(distance, footprint=np.ones((50, 50)), labels=binary_mask, threshold_rel=0.3)

Version information

# Paste the output of the following python commands
3.7.10 (default, Jun  4 2021, 14:48:32) 
[GCC 7.5.0]
Linux-4.14.243-185.433.amzn2.x86_64-x86_64-with-debian-10.6
scikit-image version: 0.18.1
numpy version: 1.20.3
/opt/conda/lib/python3.7/site-packages/matplotlib/image.py:446: RuntimeWarning: overflow encountered in double_scalars
  newmin = vmid - dv * fact
/opt/conda/lib/python3.7/site-packages/matplotlib/image.py:451: RuntimeWarning: overflow encountered in double_scalars
  newmax = vmid + dv * fact
/opt/conda/lib/python3.7/site-packages/matplotlib/image.py:503: RuntimeWarning: invalid value encountered in multiply
  A_resampled *= ((a_max - a_min) / frac)
/opt/conda/lib/python3.7/site-packages/matplotlib/image.py:504: RuntimeWarning: invalid value encountered in multiply
  vrange *= ((a_max - a_min) / frac)
@taroko-mooncake taroko-mooncake changed the title peak_local_max attribute min_distance not working peak_local_max attribute min_distance issue Oct 11, 2021
@taroko-mooncake taroko-mooncake changed the title peak_local_max attribute min_distance issue peak_local_max attribute 'min_distance' issue Oct 11, 2021
@alexdesiqueira
Copy link
Member

Thanks for opening this issue @taroko-mooncake! Could you please provide us a full, runnable example for us to check this bug?
Thanks again!

@taroko-mooncake
Copy link
Author

taroko-mooncake commented Oct 12, 2021

# Generate an initial image with two overlapping circles
x, y = np.indices((80, 80))
x1, y1, x2, y2 = 28, 28, 44, 52
r1, r2 = 16, 20
mask_circle1 = (x - x1)**2 + (y - y1)**2 < r1**2
mask_circle2 = (x - x2)**2 + (y - y2)**2 < r2**2
image = np.logical_or(mask_circle1, mask_circle2)

# Now we want to separate the two objects in image
# Generate the markers as local maxima of the distance to the background
distance = ndi.distance_transform_edt(image)
coords = peak_local_max(distance, footprint=np.ones((3, 3)), labels=image)`

@rfezzani
Copy link
Member

@taroko-mooncake, can you please be more precise in your bug report. Displaying the result of your provided example gives

fig, (ax0, ax1) = plt.subplots(1, 2)
ax1.imshow(distance)
ax0.imshow(image)
ax1.plot(coords[:, 1], coords[:, 0], 'rx')

Figure_1
Isn't it what you expected?

@taroko-mooncake
Copy link
Author

taroko-mooncake commented Oct 12, 2021

Yes the algorithm works but if you apply 'min_distance' attribute, nothing changes.

# Generate an initial image with two overlapping circles
x, y = np.indices((80, 80))
x1, y1, x2, y2 = 28, 28, 44, 52
r1, r2 = 16, 20
mask_circle1 = (x - x1)**2 + (y - y1)**2 < r1**2
mask_circle2 = (x - x2)**2 + (y - y2)**2 < r2**2
image = np.logical_or(mask_circle1, mask_circle2)

# Now we want to separate the two objects in image
# Generate the markers as local maxima of the distance to the background
distance = ndi.distance_transform_edt(image)
coords = peak_local_max(distance, footprint=np.ones((3, 3)), labels=image, min_distance=70)`

There is something about min_distance that is causing a bug but I can't tell you anymore than that.

@rfezzani rfezzani added the ❓ Needs info Needs more information or investigation label Oct 20, 2021
@rfezzani
Copy link
Member

rfezzani commented Oct 22, 2021

@taroko-mooncake, as the Note section stipulates in the doc:

The peak local maximum function returns the coordinates of local peaks (maxima) in an image. Internally, a maximum filter is used for finding local maxima. This operation dilates the original image. After comparison of the dilated and original image, this function returns the coordinates or a mask of the peaks where the dilated image equals the original image.

So the peak candidates are selected from this image
Figure_1

obtained using

import numpy as np
from scipy import ndimage as ndi
import matplotlib.pyplot as plt

from skimage.feature import peak_local_max

# Generate an initial image with two overlapping circles
x, y = np.indices((80, 80))
x1, y1, x2, y2 = 28, 28, 44, 52
r1, r2 = 16, 20
mask_circle1 = (x - x1)**2 + (y - y1)**2 < r1**2
mask_circle2 = (x - x2)**2 + (y - y2)**2 < r2**2
image = np.logical_or(mask_circle1, mask_circle2)

# Now we want to separate the two objects in image
# Generate the markers as local maxima of the distance to the background
distance = ndi.distance_transform_edt(image)
footprint = np.ones((3, 3))

# Show peak candidates
plt.imshow(ndi.maximum_filter(distance, footprint=footprint,
                              mode='constant') == distance)

Now with different values for min_distance, we obtain

# Show the output with different min_distance
fig, ax_list = plt.subplots(2, 2, figsize=(5, 5))
for ax, min_dist in zip(ax_list.ravel(), [1, 5, 25, 30]):
    coords = peak_local_max(distance, footprint=np.ones((3, 3)),
                            min_distance=min_dist)

    ax.imshow(distance)
    ax.plot(coords[:, 1], coords[:, 0], 'rx')
    ax.set_title(f"min_distance={min_dist}")
fig.tight_layout()
plt.show()

Figure_2

Please note that with min_distance=30 no peaks are detected because exclude_border is True by default and pixels within min_distance from the border are excluded.

@rfezzani rfezzani added 🫂 Support User help and QA and removed type: bug labels Oct 22, 2021
@jonaszeuner
Copy link

jonaszeuner commented Dec 30, 2021

Hi,
I also ran into an issue with the min_distance feature when using experimental data. My understanding of min_distance is, that the peak coordinates are not allowed to be closer than the value specified as min_distance. In the code example below this is clearly not the case (min_distance=10).
Figure_1

The runnable code including the image as float array below.

img=[[0.07185473, 0.07237354, 0.07473869, 0.07588312, 0.07338064,
        0.0685893 , 0.06788739, 0.07005417, 0.07190051, 0.07367056,
        0.07673762, 0.08041505, 0.08322271, 0.09103532, 0.0942855 ,
        0.09536889, 0.0936141 , 0.09129473, 0.09097429, 0.08816663,
        0.09777981, 0.10525673, 0.10922408, 0.11287098, 0.11616693,
        0.12251469, 0.12773327, 0.12834363, 0.13076982, 0.13530175],
       [0.07287709, 0.07579156, 0.07939269, 0.07614252, 0.07319753,
        0.07020676, 0.06991684, 0.06982528, 0.06932174, 0.07158007,
        0.07747005, 0.08441291, 0.09262226, 0.10464637, 0.11248951,
        0.11317617, 0.11308461, 0.11239796, 0.10971237, 0.11168078,
        0.12133974, 0.12573434, 0.12942702, 0.1328603 , 0.1363241 ,
        0.14705119, 0.15480278, 0.15773251, 0.15944152, 0.15269703],
       [0.07690547, 0.0801709 , 0.07913329, 0.07707332, 0.07710384,
        0.07612726, 0.07466239, 0.07159533, 0.07254139, 0.07891966,
        0.08923476, 0.1105211 , 0.13031205, 0.14895857, 0.16563668,
        0.1654078 , 0.15901427, 0.14918746, 0.13527123, 0.13517967,
        0.14599832, 0.15809873, 0.17221332, 0.17415122, 0.178851  ,
        0.17650111, 0.18323033, 0.18271153, 0.18051423, 0.17975128],
       [0.08044556, 0.08233768, 0.08540475, 0.08058289, 0.08056764,
        0.07867552, 0.0754406 , 0.07289235, 0.07115282, 0.08264286,
        0.1057145 , 0.13731594, 0.17752346, 0.21478599, 0.24649424,
        0.24957656, 0.24103151, 0.21493858, 0.20553902, 0.19511711,
        0.19822995, 0.20770581, 0.22546731, 0.23237964, 0.23430228,
        0.23083848, 0.21844816, 0.22291905, 0.22032502, 0.20735485],
       [0.07713436, 0.07962158, 0.08088808, 0.07916381, 0.07756161,
        0.07371633, 0.07545586, 0.0736553 , 0.08253605, 0.10061799,
        0.13951324, 0.19591058, 0.2620737 , 0.32628367, 0.35275807,
        0.35532158, 0.34709697, 0.32512398, 0.2867628 , 0.25554284,
        0.25499351, 0.26767376, 0.2804303 , 0.28729686, 0.28651865,
        0.27038987, 0.25963226, 0.24295415, 0.24213016, 0.23714046],
       [0.07457084, 0.07481498, 0.07519646, 0.07612726, 0.07734798,
        0.07537957, 0.07676814, 0.08091859, 0.09613184, 0.13318074,
        0.19253834, 0.28331426, 0.36543832, 0.44122988, 0.48641184,
        0.49250019, 0.47475395, 0.43721675, 0.38077363, 0.36330205,
        0.35648127, 0.35898375, 0.36124208, 0.35980774, 0.35187304,
        0.32892348, 0.30913252, 0.28918898, 0.28278019, 0.26912337],
       [0.07692073, 0.07153429, 0.07174792, 0.07307546, 0.07586786,
        0.07777523, 0.0816968 , 0.0914168 , 0.11578546, 0.16229496,
        0.24623484, 0.36723888, 0.49283589, 0.6037995 , 0.66649882,
        0.68049134, 0.65001907, 0.5857023 , 0.53644617, 0.48502327,
        0.45104143, 0.44589914, 0.43721675, 0.41820401, 0.40958267,
        0.39359121, 0.35474174, 0.31502251, 0.30200656, 0.29286641],
       [0.07362478, 0.06865034, 0.06836042, 0.07058824, 0.07685969,
        0.08712902, 0.09512474, 0.10942245, 0.14717327, 0.21319905,
        0.31894408, 0.44005493, 0.5972076 , 0.73974212, 0.79563592,
        0.80424201, 0.77680629, 0.72439155, 0.66315709, 0.6205539 ,
        0.59136339, 0.55407034, 0.51183337, 0.48126955, 0.45905241,
        0.44359503, 0.39360647, 0.33247883, 0.29878691, 0.28598459],
       [0.07661555, 0.07098497, 0.07164111, 0.07847715, 0.08960098,
        0.10205234, 0.11189441, 0.13377584, 0.17953765, 0.24435798,
        0.37021439, 0.53138018, 0.71470207, 0.86723125, 0.93109026,
        0.97068742, 0.9206836 , 0.82059968, 0.77393759, 0.72518502,
        0.69723049, 0.63820859, 0.57349508, 0.54044404, 0.5125658 ,
        0.48104067, 0.42185092, 0.3650721 , 0.32027161, 0.30070954],
       [0.0761883 , 0.07347219, 0.07655451, 0.0873579 , 0.104448  ,
        0.12504768, 0.13238727, 0.14227512, 0.17976654, 0.25525292,
        0.39641413, 0.58200961, 0.79380484, 0.94383154, 1.        ,
        1.        , 1.        , 0.94389258, 0.88152895, 0.85290303,
        0.77772183, 0.72477302, 0.64327459, 0.60082399, 0.53676661,
        0.48607614, 0.42684062, 0.37242695, 0.32503243, 0.29857328],
       [0.0804303 , 0.07548638, 0.08142214, 0.09590295, 0.11879149,
        0.14137484, 0.14877546, 0.1485771 , 0.17628748, 0.25600061,
        0.39954223, 0.59349966, 0.82539101, 0.98010224, 1.        ,
        0.9956817 , 1.        , 0.99919127, 0.98258946, 0.9612726 ,
        0.8808423 , 0.8037995 , 0.70806439, 0.64277104, 0.59205005,
        0.52112612, 0.46559854, 0.40114443, 0.34848554, 0.30846113],
       [0.08137636, 0.07981994, 0.08737316, 0.10576028, 0.12225528,
        0.14230564, 0.14819562, 0.142916  , 0.15841917, 0.21976043,
        0.3707332 , 0.57045853, 0.80846876, 0.97276265, 0.99960327,
        0.99920653, 0.99819944, 0.99978637, 0.99832151, 0.96365301,
        0.90923934, 0.81765469, 0.71725032, 0.66317235, 0.60846876,
        0.53975738, 0.466392  , 0.40297551, 0.33667506, 0.29703212],
       [0.07716487, 0.07788205, 0.08696117, 0.1032578 , 0.11734188,
        0.13397421, 0.1363241 , 0.13612573, 0.16073854, 0.23685054,
        0.39136339, 0.58260472, 0.80894179, 0.96975662, 1.        ,
        1.        , 1.        , 1.        , 1.        , 0.98011749,
        0.91194018, 0.85104143, 0.73804837, 0.67684443, 0.60799573,
        0.53466087, 0.45799954, 0.3849546 , 0.33391318, 0.28957046],
       [0.07530327, 0.07371633, 0.08041505, 0.09617762, 0.1160296 ,
        0.13000687, 0.13362325, 0.13542382, 0.17042802, 0.2531014 ,
        0.39249256, 0.55681697, 0.75895323, 0.9013962 , 0.96737621,
        0.9741207 , 0.97988861, 0.97930877, 0.96166934, 0.93058671,
        0.8920119 , 0.82078279, 0.72402533, 0.66617838, 0.60035096,
        0.51851682, 0.4548867 , 0.38654154, 0.33800259, 0.29245441],
       [0.07612726, 0.07347219, 0.07942321, 0.09263752, 0.1111162 ,
        0.12732128, 0.14398413, 0.16021973, 0.20712596, 0.30276951,
        0.44501411, 0.61261921, 0.77622644, 0.86001373, 0.93060197,
        0.95735103, 0.93022049, 0.90074006, 0.89314107, 0.90177768,
        0.8677348 , 0.78344396, 0.6966659 , 0.61287861, 0.56684215,
        0.49781033, 0.43706416, 0.35625238, 0.30019074, 0.26782635],
       [0.07536431, 0.07389944, 0.07913329, 0.09329366, 0.10745403,
        0.13299763, 0.16777295, 0.20903334, 0.28087282, 0.3669337 ,
        0.51096361, 0.65160601, 0.76145571, 0.82662699, 0.84771496,
        0.84402228, 0.81643397, 0.80097658, 0.80257877, 0.82801556,
        0.80445563, 0.7347982 , 0.64538033, 0.58434424, 0.53020523,
        0.47002365, 0.40288396, 0.33575952, 0.28700694, 0.25340658],
       [0.07156481, 0.07115282, 0.07252613, 0.08186465, 0.0985275 ,
        0.13136492, 0.17587549, 0.24162661, 0.33498131, 0.42914473,
        0.56014343, 0.65604639, 0.73432517, 0.77367819, 0.76427863,
        0.71807431, 0.69190509, 0.70617227, 0.72240787, 0.71902037,
        0.70576028, 0.66306554, 0.57520409, 0.50041962, 0.45349813,
        0.40636301, 0.35544366, 0.30244907, 0.26001373, 0.23016709],
       [0.07124437, 0.07095445, 0.07251087, 0.07920958, 0.1025864 ,
        0.14584573, 0.197467  , 0.26108186, 0.3662623 , 0.47635615,
        0.58738079, 0.67333486, 0.72286564, 0.7260853 , 0.69083696,
        0.65348287, 0.62357519, 0.63982605, 0.65975433, 0.67405203,
        0.65381857, 0.60027466, 0.54483864, 0.47833982, 0.44098573,
        0.40209049, 0.33618677, 0.27406729, 0.24048219, 0.22073701],
       [0.07092393, 0.07154955, 0.0743267 , 0.08226139, 0.09831388,
        0.13495079, 0.19009689, 0.26419471, 0.35919738, 0.44888991,
        0.5562829 , 0.62475013, 0.65661097, 0.6290837 , 0.58365759,
        0.55210193, 0.54930953, 0.53910124, 0.55684749, 0.58478676,
        0.57051957, 0.51674678, 0.46550698, 0.42099641, 0.38222324,
        0.34908064, 0.29947356, 0.25461204, 0.2242771 , 0.2061799 ],
       [0.07499809, 0.07765316, 0.08079652, 0.08564889, 0.0952163 ,
        0.11625849, 0.16472114, 0.23291371, 0.32831312, 0.42885481,
        0.50887312, 0.56369879, 0.57940032, 0.54755474, 0.52092775,
        0.48970779, 0.48126955, 0.4908217 , 0.51125353, 0.51749447,
        0.50835431, 0.47421988, 0.42923629, 0.38538186, 0.34389258,
        0.2983444 , 0.26189059, 0.22009613, 0.19946593, 0.18638895],
       [0.07899596, 0.08276493, 0.09018082, 0.09967193, 0.11074998,
        0.12715343, 0.15956359, 0.20015259, 0.28473335, 0.36325628,
        0.42769512, 0.49156939, 0.48667124, 0.49150835, 0.48023194,
        0.45180438, 0.46904707, 0.47782101, 0.5020676 , 0.51091783,
        0.47985046, 0.44885939, 0.40596628, 0.3699855 , 0.33055619,
        0.28464179, 0.24441901, 0.20639353, 0.18974594, 0.1747921 ],
       [0.08130007, 0.0888838 , 0.10243381, 0.11218433, 0.12213321,
        0.12898451, 0.1445182 , 0.18014801, 0.24138247, 0.30704204,
        0.36232547, 0.40610361, 0.41890593, 0.4293431 , 0.42537575,
        0.40428779, 0.40729381, 0.42523842, 0.44701305, 0.45525292,
        0.44818799, 0.41069657, 0.35251392, 0.33475242, 0.30151827,
        0.26228733, 0.21596094, 0.18770123, 0.18266575, 0.15748836],
       [0.08314641, 0.08966201, 0.1038529 , 0.1107805 , 0.12031739,
        0.13528649, 0.14305333, 0.1719234 , 0.21672389, 0.27698177,
        0.31801328, 0.35962463, 0.38422217, 0.40181582, 0.39139391,
        0.38448157, 0.39578851, 0.41803616, 0.44101625, 0.45517662,
        0.45197223, 0.41373312, 0.3738613 , 0.33105974, 0.28685435,
        0.24032959, 0.21120012, 0.19064622, 0.16681163, 0.15185779],
       [0.08151369, 0.08697642, 0.09784085, 0.10983444, 0.11778439,
        0.13374533, 0.15046921, 0.16771191, 0.19443046, 0.24798962,
        0.28975357, 0.34166476, 0.37555505, 0.40199893, 0.41164263,
        0.39714656, 0.41269551, 0.4250248 , 0.44327459, 0.46759747,
        0.44626535, 0.42078279, 0.3816434 , 0.32231632, 0.28938735,
        0.24045167, 0.22255283, 0.1972076 , 0.17541772, 0.15562676],
       [0.0780499 , 0.08300908, 0.09153887, 0.09974823, 0.10818647,
        0.1238117 , 0.14135958, 0.16148623, 0.1837644 , 0.22835126,
        0.27309071, 0.31596857, 0.34168002, 0.36893263, 0.38204013,
        0.38478676, 0.38353552, 0.39942016, 0.41300069, 0.42658122,
        0.42684062, 0.39345388, 0.33817044, 0.28982986, 0.26004425,
        0.22218662, 0.20010681, 0.18861677, 0.171664  , 0.15370413]]

from skimage.feature import peak_local_max
import numpy as np
import matplotlib.pyplot as plt
from skimage import img_as_float

data = img_as_float(img)
coords = peak_local_max(data, min_distance=10,threshold_rel=0.1)

plt.figure(1)
plt.clf()
plt.imshow(data)
plt.plot(coords[:, 1],coords[:, 0],'rx')

@rfezzani
Copy link
Member

rfezzani commented Jan 4, 2022

Which version of skimage are you using @jonaszeuner? The behavior you observed was fixed 😉! Here is what I obtain with current version
Figure_1

@rfezzani
Copy link
Member

rfezzani commented Jan 4, 2022

BTW, If you don't mind, I will convert this issue to a Discussion.

@scikit-image scikit-image locked and limited conversation to collaborators Jan 4, 2022
@rfezzani rfezzani converted this issue into a discussion Jan 4, 2022
@scikit-image scikit-image unlocked this conversation Apr 8, 2022
@grlee77
Copy link
Contributor

grlee77 commented Apr 8, 2022

comment moved back to issues from Discussion

@jonaszeuner on Jan 4
Great thanks! Indeed I was running 0.16 and updating to 0.19.1 fixed the issue.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🫂 Support User help and QA ❓ Needs info Needs more information or investigation
Projects
None yet
Development

No branches or pull requests

5 participants