-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1625 from danforthcenter/auto_color_correct_wrapper
Auto color correct wrapper
- Loading branch information
Showing
6 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
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 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/detect_color_card.png) | ||
|
||
|
||
**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
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,12 @@ | ||
"""Tests for auto_correct_color.""" | ||
import cv2 | ||
import numpy as np | ||
from plantcv.plantcv.transform import auto_correct_color | ||
|
||
|
||
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) |