Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion littlecv/littlecv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from littlecv.littlecv.plot_image import plot_image

__all__ = [
"analyze_size",
"size",
"Outputs",
"readimage",
"rgb2gray_lab",
Expand Down
10 changes: 0 additions & 10 deletions littlecv/littlecv/plot_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,3 @@ def plot_image(img, cmap=None, plot=True, **kwargs):
plt.figure()
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()

elif cmap is None and len(dimensions) == 2:
plt.figure()
plt.imshow(img, cmap="gray")
plt.show()

elif cmap is not None and len(dimensions) == 2:
plt.figure()
plt.imshow(img, cmap=cmap)
plt.show()
7 changes: 0 additions & 7 deletions littlecv/littlecv/readimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ def readimage(filename, plot=True):
"""

img = cv2.imread(filename, -1)
# Default to drop alpha channel if user doesn't specify 'rgba'
if len(np.shape(img)) == 3 and np.shape(img)[2] == 4:
img = cv2.imread(filename)

if img is None:
raise RuntimeError("Failed to open" + filename)

# Split path from filename
path, img_name = os.path.split(filename)
# we are always going to debug in this dummy version
Expand Down
16 changes: 2 additions & 14 deletions littlecv/littlecv/threshold_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@
from littlecv.littlecv.plot_image import plot_image


def binary(gray_img, threshold, object_type="light", plot=True):
def binary(gray_img, threshold, plot=True):
"""Creates a binary image from a grayscale image based on the threshold value.

Inputs:
gray_img = numpy.ndarray, Grayscale image data
threshold = int, Threshold value (0-255)
object_type = str, "light" or "dark" (default: "light")
- If object is lighter than the background then standard thresholding is done
- If object is darker than the background then inverse thresholding is done
plot = bool, lazy version of pcv.params.debug

Returns:
bin_img = numpy.ndarray, Thresholded binary image
"""
# Set the threshold method
threshold_method = ""
if object_type.upper() == "LIGHT":
threshold_method = cv2.THRESH_BINARY
elif object_type.upper() == "DARK":
threshold_method = cv2.THRESH_BINARY_INV
else:
RuntimeError('Object type ' + str(object_type) + ' is not "light" or "dark"!')

# Threshold the image
_, bin_img = cv2.threshold(gray_img, threshold, 255, threshold_method)
_, bin_img = cv2.threshold(gray_img, threshold, 255, cv2.THRESH_BINARY)

plot_image(bin_img, plot=plot)

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The final piece of the `workflow.ipynb` checks the area phenotype, when that che
#### Steps

* 1: Open a branch with your name (`git checkout -b first_last`).
* 2: Check current behavior either in jupyter (run `jupyter-lab` and use GUI) or with the tests (run `py.test --cov littlecv`)
* 2: Check current behavior either in jupyter (run `jupyter-lab` and use GUI) or with the tests (run `py.test --cov littlecv --cov-report="term-missing"`)
* 3: Edit `littlecv` code.
* 4: Restart Jupyter Kernel and run all cells again or rerun tests. Make edits and test them until tests pass.
* 5: Commit your changes (`git commit -m "your commit message here"`). This is like saving the files on your branch. How often you do this and how granular commits are is mostly up to you.
Expand Down
59 changes: 57 additions & 2 deletions tests/test_littlecv.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,69 @@
import os
import cv2
import matplotlib
import numpy as np
from littlecv import littlecv as lcv


# disable plotting
matplotlib.use("Template")


def test_readimage():
"""Test littlecv readimage"""
data = os.path.join(os.path.dirname(os.path.abspath(__file__)), "testdata/plant_cropped.png")
img, _, _ = lcv.readimage(filename=data, plot=False)
assert img.shape == (2000, 1600, 3)


def test_plot_image():
"""Test littlecv plot_image"""
data = os.path.join(os.path.dirname(os.path.abspath(__file__)), "testdata/plant_cropped.png")
img, _, _ = lcv.readimage(filename=data, plot=False)
lcv.plot_image(img)
assert True


def test_rgb2gray():
"""Test littlecv rgb to grayscale conversion"""
data = os.path.join(os.path.dirname(os.path.abspath(__file__)), "testdata/plant_cropped.png")
img, _, _ = lcv.readimage(filename=data, plot=False)
b_img = lcv.rgb2gray_lab(rgb_img=img, channel='b', plot=False)
assert b_img.shape == (2000, 1600)


def test_binary():
"""Test littlecv binary threshold"""
data = os.path.join(os.path.dirname(os.path.abspath(__file__)), "testdata/plant_cropped.png")
img, _, _ = lcv.readimage(filename=data, plot=False)
b_img = lcv.rgb2gray_lab(rgb_img=img, channel='b', plot=False)
thresh_mask = lcv.binary(gray_img=b_img, threshold=130, plot=False)
assert all(np.unique(thresh_mask) == [0, 255])


def test_fill():
"""Test littlecv fill"""
data = os.path.join(os.path.dirname(os.path.abspath(__file__)), "testdata/plant_cropped.png")
img, _, _ = lcv.readimage(filename=data, plot=False)
b_img = lcv.rgb2gray_lab(rgb_img=img, channel='b', plot=False)
thresh_mask = lcv.binary(gray_img=b_img, threshold=130, plot=False)
fill_mask = lcv.fill(thresh_mask, size=1000, plot=False)
assert np.sum(fill_mask) < np.sum(thresh_mask)


def test_area():
"""Test littlecv size"""
d = np.array([[0, 0, 1, 0, 0], [0, 1, 1, 1, 0], [0,1,1,1,0], [0, 0, 1, 0, 0]]).astype("uint8")
_, out = lcv.size(img=d, mask=d)
assert out.observations["littlecv_test"]["area"]["value"] == np.sum(d)


def test_littlecv_workflow():
"""Test for your edit"""
data = os.path.join(os.path.dirname(os.path.abspath(__file__)), "testdata/plant_cropped.png")
img, _, _ = lcv.readimage(filename=data, plot=False)
b_img = lcv.rgb2gray_lab(rgb_img=img, channel='b', plot=False)
thresh_mask = lcv.binary(gray_img=b_img, threshold=130, object_type='light', plot=False)
thresh_mask = lcv.binary(gray_img=b_img, threshold=130, plot=False)
fill_mask = lcv.fill(thresh_mask, size=1000, plot=False)
_, outputs = lcv.size(img=img, mask=fill_mask, plot=False)

assert outputs.observations["littlecv_test"]["area"]["value"] == 198568.0
Loading