-
Notifications
You must be signed in to change notification settings - Fork 265
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
Auto color correct wrapper #1625
Open
HaleySchuhl
wants to merge
14
commits into
main
Choose a base branch
from
auto_color_correct_wrapper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8e8793b
Create auto_correct_color.py
HaleySchuhl c4e007b
update var name and indentation in function
HaleySchuhl 9fa6c1b
add to transform init
HaleySchuhl 55f8bed
Create test_auto_correct_color.py
HaleySchuhl 85720dc
Update test_auto_correct_color.py
HaleySchuhl 1da8975
Update updating.md
HaleySchuhl 9403ada
Create transform_auto_correct_color.md
HaleySchuhl 9727b57
add example photo to docs
HaleySchuhl a6abc63
deepsource fixes
HaleySchuhl 96447f2
whitespace deepsource
HaleySchuhl 2a50dea
Merge branch 'main' into auto_color_correct_wrapper
HaleySchuhl 024b0d7
Merge branch 'auto_color_correct_wrapper' of https://github.com/danfo…
HaleySchuhl d9a4950
add auto correct color to mkdocs
HaleySchuhl ce94abd
Merge branch 'main' into auto_color_correct_wrapper
k034b363 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file added
BIN
+160 KB
docs/img/documentation_images/correct_color_imgs/auto_correct_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Automatically detect a color card and color correct in one step | ||
|
||
Corrects the color of the input image based on the target color matrix using an affine transformation | ||
in the RGB space after automatic detection of a color card within the image. A one-step wrapper of | ||
[plantcv.transform.detect_color_card](transform_detect_color_card.md), [plantcv.transform.std_color_matrix](std_color_matrix.md), | ||
[plantcv.transform.get_color_matrix](get_color_matrix.md), and [plantcv.transform.affine_color_correction](transform_affine_color_correction.md). | ||
|
||
**plantcv.transform.auto_correct_color**(*rgb_img, label=None, \*\*kwargs*) | ||
|
||
**returns** corrected_img | ||
|
||
- **Parameters** | ||
- rgb_img - Input RGB image data containing a color card. | ||
- label - Optional label parameter, modifies the variable name of observations recorded. (default = `pcv.params.sample_label`) | ||
- **kwargs - Other keyword arguments passed to `cv2.adaptiveThreshold` and `cv2.circle`. | ||
- adaptive_method - Adaptive threhold method. 0 (mean) or 1 (Gaussian) (default = 1). | ||
- block_size - Size of a pixel neighborhood that is used to calculate a threshold value (default = 51). We suggest using 127 if using `adaptive_method=0`. | ||
- radius - Radius of circle to make the color card labeled mask (default = 20). | ||
- min_size - Minimum chip size for filtering objects after edge detection (default = 1000) | ||
- **Returns** | ||
- corrected_img - Color corrected image | ||
|
||
- **Example Use** | ||
- Below | ||
|
||
```python | ||
|
||
from plantcv import plantcv as pcv | ||
|
||
rgb_img, imgpath, imgname = pcv.readimage(filename="top_view_plant.png") | ||
|
||
corrected_rgb = pcv.transform.auto_correct_color(rgb_img=old_card) | ||
``` | ||
|
||
**Debug Image: automatically detected and masked the color card** | ||
|
||
![Screenshot](img/documentation_images/correct_color_imgs/auto_correct_color.png) | ||
|
||
**Corrected Image:** | ||
|
||
![Screenshot](img/documentation_images/transform_affine_color_corr/tobacco_leaves_corrected.jpg) | ||
|
||
**Source Code:** [Here](https://github.com/danforthcenter/plantcv/blob/main/plantcv/plantcv/transform/auto_correct_color.py) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1210,6 +1210,11 @@ pages for more details on the input and output variable types. | |
* pre v3.0dev1: NA | ||
* post v3.0dev2: determinant, transformation_matrix = **plantcv.transform.calc_transformation_matrix**(*matrix_m, matrix_b*) | ||
|
||
#### plantcv.transform.auto_correct_color | ||
|
||
* pre v4.6: NA | ||
* post v4.6: corrected_img = **plantcv.transform.r**(*rgb_img, label=None, **kwargs*) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo in function name |
||
|
||
#### plantcv.transform.correct_color | ||
|
||
* pre v3.0dev1: NA | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Automatically detect a color card and color correct to standard chip values | ||
|
||
from plantcv.plantcv import params | ||
from plantcv.plantcv.transform.detect_color_card import detect_color_card | ||
from plantcv.plantcv.transform.color_correction import get_color_matrix, std_color_matrix, affine_color_correction | ||
|
||
|
||
def auto_correct_color(rgb_img, label=None, **kwargs): | ||
"""Automatically detect a color card. | ||
Parameters | ||
---------- | ||
rgb_img : numpy.ndarray | ||
Input RGB image data containing a color card. | ||
label : str, optional | ||
modifies the variable name of observations recorded (default = pcv.params.sample_label). | ||
**kwargs | ||
Other keyword arguments passed to cv2.adaptiveThreshold and cv2.circle. | ||
Valid keyword arguments: | ||
adaptive_method: 0 (mean) or 1 (Gaussian) (default = 1) | ||
block_size: int (default = 51) | ||
radius: int (default = 20) | ||
min_size: int (default = 1000) | ||
Returns | ||
------- | ||
numpy.ndarray | ||
Color corrected image | ||
""" | ||
# Set lable to params.sample_label if None | ||
if label is None: | ||
label = params.sample_label | ||
|
||
# Get keyword arguments and set defaults if not set | ||
labeled_mask = detect_color_card(rgb_img=rgb_img, min_size=kwargs.get("min_size", 1000), | ||
radius=kwargs.get("radius", 20), | ||
adaptive_method=kwargs.get("adaptive_method", 1), | ||
block_size=kwargs.get("block_size", 51)) | ||
_, card_matrix = get_color_matrix(rgb_img=rgb_img, mask=labeled_mask) | ||
std_matrix = std_color_matrix(pos=3) | ||
return affine_color_correction(rgb_img=rgb_img, source_matrix=card_matrix, | ||
target_matrix=std_matrix) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,11 @@ | ||||||||
"""Tests for auto_correct_color.""" | ||||||||
import cv2 | ||||||||
import numpy as np | ||||||||
from plantcv.plantcv.transform import auto_correct_color | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
def test_auto_correct_color(transform_test_data): | ||||||||
"""Test for PlantCV.""" | ||||||||
# Load rgb image | ||||||||
rgb_img = cv2.imread(transform_test_data.colorcard_img) | ||||||||
corrected_img = auto_correct_color(rgb_img=rgb_img) | ||||||||
assert np.shape(corrected_img) == np.shape(rgb_img) and np.sum(corrected_img) != np.sum(rgb_img) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the example images for before and after color correction are different samples.