/** * If null the dst is declared, otherwise it checks to see if the 'dst' as the same shape as * 'src'. * * <p>The returned image will be 8-bit RGB */ private static BufferedImage checkInputs(ImageBase src, BufferedImage dst) { if (dst != null) { if (dst.getWidth() != src.getWidth() || dst.getHeight() != src.getHeight()) { throw new IllegalArgumentException("image dimension are different"); } } else { dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB); } return dst; }
public static void checkEquals( BufferedImage imgA, ImageBase imgB, boolean boofcvBandOrder, double tol) { if (ImageUInt8.class == imgB.getClass()) { checkEquals(imgA, (ImageUInt8) imgB); } else if (ImageInt16.class.isAssignableFrom(imgB.getClass())) { checkEquals(imgA, (ImageInt16) imgB); } else if (ImageFloat32.class == imgB.getClass()) { checkEquals(imgA, (ImageFloat32) imgB, (float) tol); } else if (InterleavedU8.class == imgB.getClass()) { checkEquals(imgA, (InterleavedU8) imgB); } else if (MultiSpectral.class == imgB.getClass()) { checkEquals(imgA, (MultiSpectral) imgB, boofcvBandOrder, (float) tol); } else { throw new IllegalArgumentException("Unknown"); } }
// TODO make sure pixels outside are not modified of sub-matrix // todo have the submatrices be from different shaped inputs @SuppressWarnings({"unchecked"}) public static void checkSubImage( Object testClass, String function, boolean checkEquals, Object... inputParam) { try { ImageBase[] larger = new ImageBase[inputParam.length]; ImageBase[] subImg = new ImageBase[inputParam.length]; Class<?> paramDesc[] = new Class<?>[inputParam.length]; Object[] inputModified = new Object[inputParam.length]; for (int i = 0; i < inputParam.length; i++) { if (ImageBase.class.isAssignableFrom(inputParam[i].getClass())) { ImageBase<?> img = (ImageBase<?>) inputParam[i]; // copy the original image inside of a larger image larger[i] = img._createNew(img.getWidth() + 10, img.getHeight() + 12); // extract a sub-image and make it equivalent to the original image. subImg[i] = larger[i].subimage(5, 6, 5 + img.getWidth(), 6 + img.getHeight(), null); subImg[i].setTo(img); } // the first time it is called use the original inputs inputModified[i] = inputParam[i]; paramDesc[i] = inputParam[i].getClass(); } // first try it with the original image Method m = findMethod(testClass.getClass(), function, paramDesc); m.invoke(testClass, inputModified); // now try it with the sub-image for (int i = 0; i < inputModified.length; i++) { if (subImg[i] != null) inputModified[i] = subImg[i]; } m.invoke(testClass, inputModified); // the result should be the identical if (checkEquals) { for (int i = 0; i < inputParam.length; i++) { if (subImg[i] == null) continue; assertEquals((ImageBase) inputModified[i], subImg[i], 0); } } } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
@Override protected Object[][] createInputParam(Method candidate, Method validation) { Class<?> param[] = validation.getParameterTypes(); String name = candidate.getName(); ImageBase inputA = null; ImageBase inputSquare = null; for (int i = 0; i < param.length; i++) { if (ImageBase.class.isAssignableFrom(param[i])) { if (ImageGray.class.isAssignableFrom(param[i])) { inputA = GeneralizedImageOps.createSingleBand((Class) param[i], width, height); inputSquare = GeneralizedImageOps.createSingleBand((Class) param[i], width, width); } else { inputA = GeneralizedImageOps.createInterleaved((Class) param[i], width, height, numBands); inputSquare = GeneralizedImageOps.createInterleaved((Class) param[i], width, width, numBands); } } } if (inputA == null) throw new RuntimeException("Invalid funciton"); Object[][] ret = new Object[1][param.length]; if (name.equals("copy")) { ImageBase inputB = inputA.createNew(width, height); GImageMiscOps.fillUniform(inputA, rand, 0, 10); GImageMiscOps.fillUniform(inputB, rand, 0, 10); ret[0][0] = 10; ret[0][1] = 15; ret[0][2] = 12; ret[0][3] = 8; ret[0][4] = 5; ret[0][5] = 6; ret[0][6] = inputA; ret[0][7] = inputB; } else if (name.equals("fillBand")) { ret[0][0] = inputA; ret[0][1] = 1; ret[0][2] = 3; } else if (name.equals("fill")) { if (param[1].isArray()) { Object array = Array.newInstance(param[1].getComponentType(), numBands); for (int i = 0; i < numBands; i++) { Array.set(array, i, 2 * i + 1); } ret[0][0] = inputA; ret[0][1] = array; } else { ret[0][0] = inputA; ret[0][1] = 3; } } else if (name.equals("insertBand")) { ImageBase input = GeneralizedImageOps.createSingleBand((Class) param[0], width, height); ImageBase output = GeneralizedImageOps.createInterleaved((Class) param[2], width, height, numBands); GImageMiscOps.fillUniform(input, rand, 0, 10); GImageMiscOps.fillUniform(output, rand, 0, 10); ret[0][0] = input; ret[0][1] = 1; ret[0][2] = output; } else if (name.equals("fillBorder")) { ret[0][0] = inputA; ret[0][1] = 3; ret[0][2] = 2; } else if (name.equals("fillRectangle")) { ret[0][0] = inputA; ret[0][1] = 3; ret[0][2] = 5; ret[0][3] = 6; ret[0][4] = 3; ret[0][5] = 4; } else if (name.equals("fillGaussian")) { ret[0][0] = inputA; ret[0][1] = new Random(randomSeed); ret[0][2] = 5; ret[0][3] = 3; ret[0][4] = 1; ret[0][5] = 12; } else if (name.equals("fillUniform")) { ret[0][0] = inputA; ret[0][1] = new Random(randomSeed); ret[0][2] = 5; ret[0][3] = 30; } else if (name.equals("addGaussian")) { ret[0][0] = inputA; ret[0][1] = new Random(randomSeed); ret[0][2] = 5; ret[0][3] = 1; ret[0][4] = 10; } else if (name.equals("addUniform")) { ret[0][0] = inputA; ret[0][1] = new Random(randomSeed); ret[0][2] = 1; ret[0][3] = 10; } else if (name.equals("flipVertical")) { ret[0][0] = inputA; } else if (name.equals("flipHorizontal")) { ret[0][0] = inputA; } else if (name.equals("rotateCW")) { if (param.length == 1) { ret[0][0] = inputSquare; } else { ret[0][0] = inputA; ret[0][1] = inputA.createNew(height, width); } } else if (name.equals("rotateCCW")) { if (param.length == 1) { ret[0][0] = inputSquare; } else { ret[0][0] = inputA; ret[0][1] = inputA.createNew(height, width); } } else { throw new RuntimeException("Unknown function: " + name); } fillRandom(inputA); return ret; }