use of boofcv.struct.convolve.Kernel2D in project BoofCV by lessthanoptimal.
the class TestSteerableKernel_I32 method checkCombining.
/**
* Checks to see if the basis kernels are correctly combined together.
*/
@Test
public void checkCombining() {
double[] c = new double[] { 0.1, 0.2, 0.8 };
DummySteerableCoefficients coef = new DummySteerableCoefficients(c);
Kernel2D[] basis = new Kernel2D[3];
basis[0] = FactoryKernel.random2D_I32(width, width / 2, 0, 30, rand);
basis[1] = FactoryKernel.random2D_I32(width, width / 2, 0, 30, rand);
basis[2] = FactoryKernel.random2D_I32(width, width / 2, 0, 30, rand);
Kernel2D_S32 expected = new Kernel2D_S32(width);
for (int y = 0; y < width; y++) {
for (int x = 0; x < width; x++) {
int total = 0;
for (int i = 0; i < c.length; i++) {
total += c[i] * ((Kernel2D_S32) basis[i]).get(x, y);
}
expected.set(x, y, total);
}
}
SteerableKernel_I32 alg = new SteerableKernel_I32();
alg.setBasis(coef, basis);
Kernel2D_S32 found = alg.compute(60.0);
assertTrue(KernelMath.isEquals(expected.data, found.data, width * width));
}
use of boofcv.struct.convolve.Kernel2D in project BoofCV by lessthanoptimal.
the class FactorySteerable method gaussian.
/**
* Steerable filter for 2D Gaussian derivatives. The basis is composed of a set of rotated kernels.
*
* @param kernelType Specifies which type of 2D kernel should be generated.
* @param orderX Order of the derivative in the x-axis.
* @param orderY Order of the derivative in the y-axis.
*
* @param sigma
*@param radius Radius of the kernel. @return Steerable kernel generator for the specified gaussian derivative.
*/
public static <K extends Kernel2D> SteerableKernel<K> gaussian(Class<K> kernelType, int orderX, int orderY, double sigma, int radius) {
if (orderX < 0 || orderX > 4)
throw new IllegalArgumentException("derivX must be from 0 to 4 inclusive.");
if (orderY < 0 || orderY > 4)
throw new IllegalArgumentException("derivT must be from 0 to 4 inclusive.");
int order = orderX + orderY;
if (order > 4) {
throw new IllegalArgumentException("The total order of x and y can't be greater than 4");
}
int maxOrder = Math.max(orderX, orderY);
if (sigma <= 0)
sigma = (float) FactoryKernelGaussian.sigmaForRadius(radius, maxOrder);
else if (radius <= 0)
radius = FactoryKernelGaussian.radiusForSigma(sigma, maxOrder);
Class kernel1DType = FactoryKernel.get1DType(kernelType);
Kernel1D kerX = FactoryKernelGaussian.derivativeK(kernel1DType, orderX, sigma, radius);
Kernel1D kerY = FactoryKernelGaussian.derivativeK(kernel1DType, orderY, sigma, radius);
Kernel2D kernel = GKernelMath.convolve(kerY, kerX);
Kernel2D[] basis = new Kernel2D[order + 1];
// convert it into an image which can be rotated
ImageGray image = GKernelMath.convertToImage(kernel);
ImageGray imageRotated = (ImageGray) image.createNew(image.width, image.height);
basis[0] = kernel;
// form the basis by created rotated versions of the kernel
double angleStep = Math.PI / basis.length;
for (int index = 1; index <= order; index++) {
float angle = (float) (angleStep * index);
GImageMiscOps.fill(imageRotated, 0);
new FDistort(image, imageRotated).rotate(angle).apply();
basis[index] = GKernelMath.convertToKernel(imageRotated);
}
SteerableKernel<K> ret;
if (kernelType == Kernel2D_F32.class)
ret = (SteerableKernel<K>) new SteerableKernel_F32();
else
ret = (SteerableKernel<K>) new SteerableKernel_I32();
ret.setBasis(FactorySteerCoefficients.polynomial(order), basis);
return ret;
}
use of boofcv.struct.convolve.Kernel2D in project BoofCV by lessthanoptimal.
the class DisplayGaussianKernelApp method setActiveAlgorithm.
@Override
public void setActiveAlgorithm(String name, Object cookie) {
DerivType type = (DerivType) cookie;
panel.reset();
for (int radius = 1; radius <= 40; radius += 2) {
int maxOrder = Math.max(type.orderX, type.orderY);
double sigma = FactoryKernelGaussian.sigmaForRadius(radius, maxOrder);
Class typeKer1 = FactoryKernel.getKernelType(imageType, 1);
Kernel1D kerX = FactoryKernelGaussian.derivativeK(typeKer1, type.orderX, sigma, radius);
Kernel1D kerY = FactoryKernelGaussian.derivativeK(typeKer1, type.orderY, sigma, radius);
Kernel2D kernel = GKernelMath.convolve(kerY, kerX);
T smallImg = GKernelMath.convertToImage(kernel);
new FDistort(smallImg, largeImg).interpNN().scaleExt().apply();
double maxValue = GImageStatistics.maxAbs(largeImg);
BufferedImage out = VisualizeImageData.colorizeSign(largeImg, null, maxValue);
panel.addImage(out, String.format("%5d", radius));
}
}
use of boofcv.struct.convolve.Kernel2D in project BoofCV by lessthanoptimal.
the class TestConvolveImageStandard_SB method convolve.
/**
* Unit test for 2D convolution.
*/
public void convolve(ImageGray img, ImageGray dest) {
Kernel2D ker = FactoryKernel.createKernelForImage(kernelWidth, kernelRadius, 2, img.getClass());
// standard symmetric odd kernel shape
ker = FactoryKernel.random(ker.getClass(), kernelWidth, kernelRadius, 0, maxKernelValue, rand);
convolve(ker, img, dest);
// odd non-symmetric kernel shape
ker = FactoryKernel.random(ker.getClass(), kernelWidth, 0, 0, maxKernelValue, rand);
convolve(ker, img, dest);
// even non-symmetric kernel shape
ker = FactoryKernel.random(ker.getClass(), 2, 1, 0, maxKernelValue, rand);
convolve(ker, img, dest);
}
use of boofcv.struct.convolve.Kernel2D in project BoofCV by lessthanoptimal.
the class TestConvolveImageStandard_SB method convolveDiv.
/**
* Unit test for 2D convolution with division.
*/
public void convolveDiv(ImageGray img, ImageGray dest) {
Kernel2D ker = FactoryKernel.createKernelForImage(kernelWidth, kernelRadius, 2, img.getClass());
// standard symmetric odd kernel shape
ker = FactoryKernel.random(ker.getClass(), kernelWidth, kernelRadius, 0, maxKernelValue, rand);
convolveDiv(ker, img, dest);
// odd non-symmetric kernel shape
ker = FactoryKernel.random(ker.getClass(), kernelWidth, 0, 0, maxKernelValue, rand);
convolveDiv(ker, img, dest);
// even non-symmetric kernel shape
ker = FactoryKernel.random(ker.getClass(), 2, 1, 0, maxKernelValue, rand);
convolveDiv(ker, img, dest);
}
Aggregations