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

New background correction option and improved junge calculation #314

Merged
merged 37 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
b1ffc58
Made changes on Silcam Folder, forked and committed
Arsimstn Sep 16, 2024
236b856
Update Silcam.py
arsalanmstn Sep 16, 2024
ba610df
Merge branch 'SINTEF:main' into main
arsalanmstn Sep 16, 2024
e79b2b1
bayer conversion and version changed
arsalanmstn Sep 16, 2024
ae5f699
Bayer added to format_load
arsalanmstn Sep 16, 2024
88577d1
Additional image intensity check
arsalanmstn Sep 16, 2024
f9b0c5a
RGB dimension check
arsalanmstn Sep 16, 2024
9baf1a2
Update silcam.py
arsalanmstn Sep 16, 2024
befb771
Update silcam.py
arsalanmstn Sep 16, 2024
ad63946
Merge branch 'SINTEF:main' into main
arsalanmstn Sep 17, 2024
a63cfdb
Merge branch 'SINTEF:main' into main
arsalanmstn Sep 20, 2024
f79cd54
Update cli.py
arsalanmstn Sep 20, 2024
ae9af99
update __init__
arsalanmstn Sep 20, 2024
55227bf
Merge branch 'SINTEF:main' into main
arsalanmstn Sep 23, 2024
fc0345c
Merge branch 'SINTEF:main' into main
arsalanmstn Sep 24, 2024
2e3c90b
Update statistics.py
arsalanmstn Sep 25, 2024
1b9c43b
Update __init__.py
arsalanmstn Sep 25, 2024
49fb661
Add new background correction function based on dividing the imraw by…
arsalanmstn Sep 25, 2024
a7bb309
Update background.py
arsalanmstn Sep 25, 2024
5698e5f
Update background.py
arsalanmstn Sep 25, 2024
907f70a
Update background.py
arsalanmstn Sep 25, 2024
e989356
Update background.py
arsalanmstn Sep 25, 2024
94118ae
Modified background
arsalanmstn Sep 26, 2024
c7501e7
modified background.py
arsalanmstn Sep 26, 2024
f763f01
Merge branch 'main' of https://github.com/arsalanmstn/pyopia
arsalanmstn Sep 26, 2024
220c5d8
Update background.py
arsalanmstn Sep 26, 2024
55a3350
Update __init__.py
arsalanmstn Sep 26, 2024
1c2db1c
Update background.py
arsalanmstn Sep 26, 2024
8be4aef
Update background.py
arsalanmstn Sep 26, 2024
81c5012
Merge branch 'main' into main
emlynjdavies Sep 27, 2024
de69c05
Merge branch 'SINTEF:main' into main
arsalanmstn Sep 30, 2024
e8b1abc
modified background
arsalanmstn Sep 30, 2024
6997d1a
Merge branch 'main' of https://github.com/arsalanmstn/pyopia
arsalanmstn Sep 30, 2024
208e733
background.ipynb updated
arsalanmstn Sep 30, 2024
6b4c0c4
Update background.py
arsalanmstn Sep 30, 2024
ae20aae
Update __init__.py
arsalanmstn Sep 30, 2024
5e470ae
Updated __init__.py
arsalanmstn Oct 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions docs/notebooks/background_correction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,72 @@
"np.abs(imbg_before - processing_pipeline.data['imbg']).sum()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Correct images by subtracting vs dividing average background\n",
"The CorrectBackgroundAccurate class have two modes for a subtracting background correction (divide_bg=False), \n",
"or dividing background correction (divide_bg=True) that provides the corrected image ('im_corrected') for further analysis.\n",
"For dividing background mode, the zero-value pixels of the average background image are initially rescaled to 1/255 to prevent division by zero.\n",
"For more information, refer to: https://doi.org/10.1016/j.marpolbul.2016.11.063).\n",
"You can select the subtracting/dividing correction modes used in the pipeline to process raw images, as illustrated below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Corrected image uisng subtracting method\n",
"im_corrected_subtract = processing_pipeline.data['im_corrected'].copy()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Update pipeline config and background step with dividing background correction (divide_bg=True)\n",
"\n",
"pipeline_config['steps']['correctbackground']['divide_bg'] = True\n",
"\n",
"# Run the first N images to creat the background\n",
"for filename in image_files[:NUM_IMAGES_FOR_BACKGROUND]:\n",
" processing_pipeline.run(filename)\n",
"\n",
"# Now process one of the raw images\n",
"processing_pipeline.run(image_files[NUM_IMAGES_FOR_BACKGROUND])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Corrected image uisng dividing mode\n",
"im_corrected_division = processing_pipeline.data['im_corrected'].copy()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Plot raw, im_corrected by subtracing and dividing averaged background image\n",
"fig, axes = plt.subplots(1, 3, figsize=(3*6, 5))\n",
"axes[0].imshow(processing_pipeline.data['imraw'])\n",
"axes[0].set_title(f'Raw image #{NUM_IMAGES_FOR_BACKGROUND}')\n",
"axes[1].imshow(im_corrected_subtract)\n",
"axes[1].set_title('Corrected image by background subtraction')\n",
"axes[2].imshow(im_corrected_division)\n",
"axes[2].set_title('Corrected image by background division')"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
2 changes: 1 addition & 1 deletion pyopia/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.6.2'
__version__ = '2.7.0'
34 changes: 25 additions & 9 deletions pyopia/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ def shift_bgstack_fast(bgstack, imbg, imnew):
return bgstack, imbg


def correct_im_accurate(imbg, imraw):
def correct_im_accurate(imbg, imraw, divide_bg=False):
'''
Corrects raw image by subtracting the background and scaling the output
Corrects raw image by subtracting or dividing the background and scaling the output

For dividing method see: https://doi.org/10.1016/j.marpolbul.2016.11.063)

There is a small chance of clipping of imc in both crushed blacks and blown
highlights if the background or raw images are very poorly obtained
Expand All @@ -108,17 +110,26 @@ def correct_im_accurate(imbg, imraw):
background averaged image
imraw : float64
raw image
divide_bg : (bool, optional)
If True, the correction will be performed by dividing the raw image by the background
Default to False

Returns
-------
im_corrected : float64
corrected image, same type as input
'''

im_corrected = imraw - imbg
im_corrected += (1 / 2 - np.percentile(im_corrected, 50))

im_corrected += 1 - im_corrected.max()
if divide_bg:
imbg = np.clip(imbg, a_min=1/255, a_max=None) # Clipping the zero_value pixels
im_corrected = imraw / imbg
im_corrected += (1 / 2 - np.percentile(im_corrected, 50))
im_corrected -= im_corrected.min() # Shift the negative values to zero
nepstad marked this conversation as resolved.
Show resolved Hide resolved
im_corrected = np.clip(im_corrected, a_min=0, a_max=1)
else:
im_corrected = imraw - imbg
im_corrected += (1 / 2 - np.percentile(im_corrected, 50))
im_corrected += 1 - im_corrected.max() # Shift the positive values exceeding unity to one

return im_corrected

Expand Down Expand Up @@ -207,7 +218,7 @@ class CorrectBackgroundAccurate():
----------
bgshift_function : (string, optional)
Function used to shift the background. Defaults to passing (i.e. static background)
Available options are 'accurate', 'fast', or 'pass' to apply a statick background correction:
Available options are 'accurate', 'fast', or 'pass' to apply a static background correction:

:func:`pyopia.background.shift_bgstack_accurate`

Expand All @@ -220,6 +231,10 @@ class CorrectBackgroundAccurate():
The key in Pipeline.data of the image to be background corrected.
Defaults to 'imraw'

divide_bg : (bool)
If True, it performs background correction by dividing the raw image by the background.
Default to False.

Returns
-------
data : :class:`pyopia.pipeline.Data`
Expand Down Expand Up @@ -256,10 +271,11 @@ class CorrectBackgroundAccurate():
Then you could use :class:`pyopia.pipeline.CorrectBackgroundNone` if you need to instead.
'''

def __init__(self, bgshift_function='pass', average_window=1, image_source='imraw'):
def __init__(self, bgshift_function='pass', average_window=1, image_source='imraw', divide_bg=False):
self.bgshift_function = bgshift_function
self.average_window = average_window
self.image_source = image_source
self.divide_bg = divide_bg

def _build_background_step(self, data):
'''Add one layer to the background stack from the raw image in data pipeline, and update the background image.'''
Expand All @@ -284,7 +300,7 @@ def __call__(self, data):
data['skip_next_steps'] = True
return data

data['im_corrected'] = correct_im_accurate(data['imbg'], data[self.image_source])
data['im_corrected'] = correct_im_accurate(data['imbg'], data[self.image_source], divide_bg=self.divide_bg)

match self.bgshift_function:
case 'pass':
Expand Down
2 changes: 1 addition & 1 deletion pyopia/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def get_j(dias, number_distribution):

# use polyfit to obtain the slope of the ditriubtion in log-space (which is
# assumed near-linear in most parts of the ocean)
p = np.polyfit(np.log(dias[ind]), np.log(number_distribution[ind]), 1)
p = np.polyfit(np.log(dias[ind]), np.log(number_distribution[ind], where=number_distribution[ind] > 0), 1)
junge_slope = p[0]
return junge_slope

Expand Down
Loading