diff --git a/littlecv/littlecv/__init__.py b/littlecv/littlecv/__init__.py index d9a8787..1de5b15 100644 --- a/littlecv/littlecv/__init__.py +++ b/littlecv/littlecv/__init__.py @@ -10,7 +10,7 @@ from littlecv.littlecv.plot_image import plot_image __all__ = [ - "analyze_size", + "size", "Outputs", "readimage", "rgb2gray_lab", diff --git a/littlecv/littlecv/plot_image.py b/littlecv/littlecv/plot_image.py index 54ff429..40e5f36 100644 --- a/littlecv/littlecv/plot_image.py +++ b/littlecv/littlecv/plot_image.py @@ -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() diff --git a/littlecv/littlecv/readimage.py b/littlecv/littlecv/readimage.py index 832e28c..b3da2c1 100644 --- a/littlecv/littlecv/readimage.py +++ b/littlecv/littlecv/readimage.py @@ -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 diff --git a/littlecv/littlecv/threshold_binary.py b/littlecv/littlecv/threshold_binary.py index c0f0db6..22a2b8e 100644 --- a/littlecv/littlecv/threshold_binary.py +++ b/littlecv/littlecv/threshold_binary.py @@ -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) diff --git a/readme.md b/readme.md index 23689e3..081390c 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/tests/test_littlecv.py b/tests/test_littlecv.py index 105729b..0ce7a99 100644 --- a/tests/test_littlecv.py +++ b/tests/test_littlecv.py @@ -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