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

Make color card chip size a metadata term & Enhance debug pcv.transform.detect_color_card #1609

Merged
merged 21 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4829c88
update debug image in detect color card
HaleySchuhl Oct 17, 2024
d4b6203
replace docs image with updated debug
HaleySchuhl Oct 17, 2024
91abb94
oops, forgot to remove return for docs update
HaleySchuhl Oct 17, 2024
adc8574
Update transform_detect_color_card.md
HaleySchuhl Oct 17, 2024
d7dbefd
Update std_color_matrix.md
HaleySchuhl Oct 17, 2024
e23d145
add new function that detects & corrects in one step
HaleySchuhl Oct 17, 2024
055aea6
whitespace and docstring updates
HaleySchuhl Oct 17, 2024
f478199
Update __init__.py
HaleySchuhl Oct 18, 2024
4fee79e
rename var and update indentation
HaleySchuhl Oct 18, 2024
f898665
white space line return
HaleySchuhl Oct 18, 2024
7a115fb
update chip size data to be metadata
HaleySchuhl Oct 21, 2024
4243a7b
move new combined function to new branch
HaleySchuhl Oct 21, 2024
150dc0d
Merge branch 'main' into enhance_debug_detect_color_card
HaleySchuhl Oct 21, 2024
1eabdef
whitespace
HaleySchuhl Oct 21, 2024
17ff19e
Merge branch 'enhance_debug_detect_color_card' of https://github.com/…
HaleySchuhl Oct 21, 2024
38a5c8a
update debug text params
HaleySchuhl Oct 22, 2024
ba98e72
oops, remove debug return
HaleySchuhl Oct 24, 2024
5627f33
Merge branch 'main' into enhance_debug_detect_color_card
HaleySchuhl Oct 24, 2024
2b1cb52
Merge branch 'main' into enhance_debug_detect_color_card
HaleySchuhl Nov 4, 2024
39bd122
Merge branch 'main' into enhance_debug_detect_color_card
k034b363 Nov 7, 2024
5e60003
change "observations" to "metadata" in docs for detect color card
k034b363 Nov 8, 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions docs/std_color_matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Source: [https://en.wikipedia.org/wiki/ColorChecker](https://en.wikipedia.org/wi
- pos = 2: top-right corner
- pos = 3: top-left corner

- **Context**
- A standard matrix can be used most readily while doing [affine](transform_affine_color_correction.md) color correction.
Where possible it's recommended to use [`pcv.transform.detect_color_card`](transform_detect_color_card.md) which orders the color card mask as if the white chip were in the top-left corner, or `pos = 3`.

- **Returns**
- color_matrix - a *n* x 4 matrix containing the standard red, green, and blue
values for each color chip
Expand Down
18 changes: 14 additions & 4 deletions docs/transform_detect_color_card.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ Automatically detects a color card and creates a labeled mask.
- 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**
- labeled_mask - Labeled color card mask (useful downstream of this step in `pcv.transform.get_color_matrix` and `pcv.transform.correct_color`)
- labeled_mask - Labeled color card mask (useful downstream of this step in [`pcv.transform.get_color_matrix`](get_color_matrix.md) and [`pcv.transform.correct_color`](transform_correct_color.md) and [`pcv.transform.affine_color_correction`](transform_affine_color_correction.md)).

- **Context**
- This mask output will be consistent in chip order regardless of orientation, where the white chip is detected and labeled first with index=0. In the case of `affine_color_correction` one will make a target color matrix.
- **Example use:**
- [Color Correction Tutorial](tutorials/transform_color_correction_tutorial.md)

Expand All @@ -33,9 +36,16 @@ from plantcv import plantcv as pcv
rgb_img, path, filename = pcv.readimage("target_img.png")
cc_mask = pcv.transform.detect_color_card(rgb_img=rgb_img)

avg_chip_size = pcv.outputs.observations['default']['median_color_chip_size']['value']
avg_chip_w = pcv.outputs.observations['default']['median_color_chip_width']['value']
avg_chip_h = pcv.outputs.observations['default']['median_color_chip_height']['value']
avg_chip_size = pcv.outputs.metadata['median_color_chip_size']['value'][0]
avg_chip_w = pcv.outputs.metadata['median_color_chip_width']['value'][0]
avg_chip_h = pcv.outputs.metadata['median_color_chip_height']['value'][0]

# When using detect_color_card, you will always set pos=3
tgt_matrix = pcv.transform.std_color_matrix(pos=3)
headers, card_matrix = pcv.transform.get_color_matrix(rgb_img=rgb_img, mask=cc_mask)
corrected_img = pcv.transform.affine_color_correction(rgb_img=rgb_img,
source_matrix=card_matrix,
target_matrix=tgt_matrix)

```

Expand Down
17 changes: 7 additions & 10 deletions plantcv/plantcv/transform/detect_color_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ def detect_color_card(rgb_img, label=None, **kwargs):
if len(filtered_contours) == 0:
fatal_error('No color card found')

# Draw filtered contours on debug img
debug_img = np.copy(rgb_img)
cv2.drawContours(debug_img, filtered_contours, -1, color=(255, 50, 250), thickness=params.line_thickness)
# Initialize chip shape lists
marea, mwidth, mheight = _get_contour_sizes(filtered_contours)

Expand Down Expand Up @@ -172,18 +175,12 @@ def detect_color_card(rgb_img, label=None, **kwargs):
new_centers = cv2.transform(np.array([centers]), m_transform)[0][:, 0:2]

# Create labeled mask and debug image of color chips
labeled_mask, debug_img = _draw_color_chips(rgb_img, new_centers, radius)
labeled_mask, debug_img = _draw_color_chips(debug_img, new_centers, radius)

# Save out chip size for pixel to cm standardization
outputs.add_observation(sample=label, variable='median_color_chip_size', trait='size of color card chips identified',
method='plantcv.plantcv.transform.detect_color_card', scale='square pixels',
datatype=float, value=chip_size, label="median")
outputs.add_observation(sample=label, variable='median_color_chip_width', trait='width of color card chips identified',
method='plantcv.plantcv.transform.detect_color_card', scale='pixels',
datatype=float, value=chip_width, label="width")
outputs.add_observation(sample=label, variable='median_color_chip_height', trait='height of color card chips identified',
method='plantcv.plantcv.transform.detect_color_card', scale='pixels',
datatype=float, value=chip_height, label="height")
outputs.add_metadata(term="median_color_chip_size", datatype=float, value=chip_size)
outputs.add_metadata(term="median_color_chip_width", datatype=float, value=chip_width)
outputs.add_metadata(term="median_color_chip_height", datatype=float, value=chip_height)

# Debugging
_debug(visual=debug_img, filename=os.path.join(params.debug_outdir, f'{params.device}_color_card.png'))
Expand Down