@@ -235,10 +235,19 @@ def plot_cross_section(
235235 zero_crossings = np .where (np .diff (np .signbit (cross_section + plot_db_drop )))[0 ]
236236 if len (zero_crossings ) >= 2 :
237237 zero_crossings -= np .argmax (cross_section )
238- width = zero_crossings [- 1 ] - zero_crossings [0 ]
239- ax .set_title (f"-{ plot_db_drop } dB width = { width } " )
240- ax .axvline (x = zero_crossings [0 ], c = "k" , linestyle = "--" )
241- ax .axvline (x = zero_crossings [- 1 ], c = "k" , linestyle = "--" )
238+
239+ # width = zero_crossings[-1] - zero_crossings[0]
240+ # ax.axvline(x=zero_crossings[0], c="k", linestyle="--")
241+ # ax.axvline(x=zero_crossings[-1], c="k", linestyle="--")
242+
243+ first_crossing = np .abs (zero_crossings [np .argmin (np .abs (zero_crossings ))])
244+ width = 2 * np .abs (first_crossing )
245+ ax .axvline (x = - first_crossing , c = "k" , linestyle = "--" )
246+ ax .axvline (x = + first_crossing , c = "k" , linestyle = "--" )
247+
248+ ax .set_title ("Cross-section" )
249+ ax .set_xlabel (f"-{ plot_db_drop } dB width = { width } " )
250+
242251 else :
243252 warnings .warn (
244253 "Width could not be determined. Did not detect two -{} points : {}" .format (
@@ -290,7 +299,7 @@ def plot_autocorr2d(vals, pad_mode="reflect", ax=None):
290299 return ax , autocorr
291300
292301
293- def plot_autocorr_rgb (img , width = 3 , figsize = None ):
302+ def plot_autocorr_rgb (img , width = 3 , figsize = None , plot_psf = False , psf_gamma = 2.2 ):
294303 """
295304 Plot autocorrelation of each channel of an image.
296305
@@ -306,15 +315,36 @@ def plot_autocorr_rgb(img, width=3, figsize=None):
306315 assert len (img .shape ) == 3 , "Image must be 3D"
307316 assert img .shape [2 ] == 3 , "Image must have 3 color channels"
308317
309- _ , ax_auto = plt .subplots (ncols = 3 , nrows = 2 , num = "Autocorrelations" , figsize = figsize )
310-
311- for i , c in enumerate (["r" , "g" , "b" ]):
312- _ , autocorr_c = plot_autocorr2d (img [:, :, i ], ax = ax_auto [0 ][i ])
318+ if plot_psf :
319+ _ , ax_auto = plt .subplots (ncols = 3 , nrows = 3 , num = "Autocorrelations" , figsize = figsize )
320+ else :
321+ _ , ax_auto = plt .subplots (ncols = 3 , nrows = 2 , num = "Autocorrelations" , figsize = figsize )
322+
323+ for i , c in enumerate (["Red" , "Green" , "Blue" ]):
324+ if plot_psf :
325+ plot_image (
326+ img [:, :, i ],
327+ ax = ax_auto [0 ][i ],
328+ gamma = psf_gamma ,
329+ normalize = True ,
330+ )
331+ # ax_auto[0][i].imshow(img[:, :, i], cmap="gray")
332+ ax_auto [0 ][i ].axis ("off" )
333+ ax_auto [0 ][i ].set_title (f"{ c } PSF" )
334+
335+ # plot autocorrelation
336+ _ , autocorr_c = plot_autocorr2d (img [:, :, i ], ax = ax_auto [1 if plot_psf else 0 ][i ])
337+ ax_auto [1 ][i ].set_title ("Autocorrelation" )
338+ # # -- horizontal cross-section
339+ max_idx = np .unravel_index (np .argmax (autocorr_c , axis = None ), autocorr_c .shape )
340+ idx = max_idx [0 ]
341+ # ax_auto[1][i].axhline(y=idx, c=c, linestyle="--")
313342
314343 ax , _ = plot_cross_section (
315344 autocorr_c ,
345+ idx = idx ,
316346 color = c ,
317- ax = ax_auto [1 ][i ],
347+ ax = ax_auto [2 if plot_psf else 1 ][i ],
318348 plot_db_drop = width ,
319349 )
320350 if i != 0 :
0 commit comments