|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * SciJava Ops OpenCV: OpenCV configuration for ops |
| 4 | + * %% |
| 5 | + * Copyright (C) 2023 - 2024 SciJava developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | +package org.scijava.ops.opencv; |
| 30 | + |
| 31 | +import net.imagej.opencv.MatToImgConverter; |
| 32 | +import org.bytedeco.opencv.opencv_core.Mat; |
| 33 | +import org.bytedeco.opencv.opencv_core.Size; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | +import org.scijava.ops.api.OpBuilder; |
| 36 | +import org.scijava.ops.api.OpEnvironment; |
| 37 | + |
| 38 | +import java.io.File; |
| 39 | + |
| 40 | +import static org.bytedeco.opencv.global.opencv_imgcodecs.imread; |
| 41 | +import static org.bytedeco.opencv.global.opencv_imgproc.GaussianBlur; |
| 42 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 44 | + |
| 45 | +public class TestOpenCV { |
| 46 | + |
| 47 | + private static final String TEST_OP = "filter.gauss"; |
| 48 | + private static final String EXPECTED_HELP = // |
| 49 | + "filter.gauss:\n" // |
| 50 | + + "\t- (in, @CONTAINER container1, size, StDev) -> None"; |
| 51 | + |
| 52 | + private static final String EXPECTED_HELP_VERBOSE = // |
| 53 | + "filter.gauss:\n" // |
| 54 | + + "\t- org.bytedeco.opencv.global.opencv_imgproc.GaussianBlur(org.bytedeco.opencv.opencv_core.Mat,org.bytedeco.opencv.opencv_core.Mat,org.bytedeco.opencv.opencv_core.Size,double)\n" // |
| 55 | + + "\t\t> in : org.bytedeco.opencv.opencv_core.Mat\n" // |
| 56 | + + "\t\t\tthe input {@link Mat} to blur\n" + "\t\t> container1 : @CONTAINER org.bytedeco.opencv.opencv_core.Mat\n" // |
| 57 | + + "\t\t\ta preallocated {@link Mat} to populate\n" // |
| 58 | + + "\t\t> size : org.bytedeco.opencv.opencv_core.Size\n" // |
| 59 | + + "\t\t\tdesired kernel size\n" // |
| 60 | + + "\t\t> StDev : java.lang.Double\n" // |
| 61 | + + "\t\t\tdesired standard deviation/blur intensity"; |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testDiscovery() { |
| 65 | + final OpEnvironment ops = OpEnvironment.build(); |
| 66 | + assertEquals(EXPECTED_HELP, ops.help(TEST_OP)); |
| 67 | + assertEquals(EXPECTED_HELP_VERBOSE, ops.helpVerbose(TEST_OP)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testUsage() { |
| 72 | + final OpEnvironment ops = OpEnvironment.build(); |
| 73 | + Mat src = openFish(); |
| 74 | + Mat opsFish = new Mat(src.rows(), src.cols(), src.type()); |
| 75 | + Mat opencvFish = new Mat(src.rows(), src.cols(), src.type()); |
| 76 | + |
| 77 | + int stDev = 100; |
| 78 | + Size size = new Size(5, 5); |
| 79 | + |
| 80 | + OpBuilder.Arity3_IV_OV<Mat, Size, Integer, Mat> builder = |
| 81 | + ops.ternary(TEST_OP).input(src, size, stDev).output(opsFish); |
| 82 | + |
| 83 | + // Check help strings for the builder |
| 84 | + assertEquals(EXPECTED_HELP, builder.help()); |
| 85 | + assertEquals(EXPECTED_HELP_VERBOSE, builder.helpVerbose()); |
| 86 | + |
| 87 | + // Blur with ops |
| 88 | + builder.compute(); |
| 89 | + |
| 90 | + // Blur directly with JavaCV |
| 91 | + GaussianBlur(src, opencvFish, size, stDev); |
| 92 | + |
| 93 | + // Verify dimensions |
| 94 | + assertEquals(opencvFish.rows(), opsFish.rows()); |
| 95 | + assertEquals(opencvFish.cols(), opsFish.cols()); |
| 96 | + |
| 97 | + // Verify data |
| 98 | + byte[] opencvBytes = MatToImgConverter.toByteArray(opencvFish); |
| 99 | + byte[] opsFishBytes = MatToImgConverter.toByteArray(opsFish); |
| 100 | + |
| 101 | + assertArrayEquals(opencvBytes, opsFishBytes); |
| 102 | + } |
| 103 | + |
| 104 | + private Mat openFish() { |
| 105 | + return imread(new File( |
| 106 | + getClass().getResource("/HappyFish.jpg").getFile()).getAbsolutePath()); |
| 107 | + } |
| 108 | + |
| 109 | + //TODO try opening with scifio and converting with imagej-opencv |
| 110 | +} |
0 commit comments