Java源码示例:boofcv.struct.image.Planar
示例1
/**
* The actual classification of the images by using the pre-trained model.
*/
private static Entry<String, Double> classifyWithModel(ImageClassifierVggCifar10 classifier, BufferedImage image) {
Planar<GrayF32> planar = new Planar<>(GrayF32.class, image.getWidth(), image.getHeight(), 3);
ConvertBufferedImage.convertFromPlanar(image, planar, true, GrayF32.class);
classifier.classify(planar);
return classifier.getAllResults().stream()
.map(score -> entry(classifier.getCategories().get(score.category), score.score))
.max(Comparator.comparing(Entry::getValue)).get();
}
示例2
/**
* Segments a colored image by turning all pixels that are close to the provided color to white.
*
* @param image The image that should be converted.
* @param colorRgb The colour that should be turned to white.
* @return Converted image where pixels close to the provided color are white and the others are black
*/
public static BufferedImage segmentImageByColour(BufferedImage image, float[] colorRgb) {
/* Phase 1): Convert average RGB color to HSV. */
final float[] avgHsvColor = new float[]{0.0f,0.0f,0.0f};
ColorHsv.rgbToHsv(colorRgb[0], colorRgb[1], colorRgb[2], avgHsvColor);
/* Phase 2a): Convert the input BufferedImage to a HSV image and extract hue and saturation bands, which are independent of intensity. */
final Planar<GrayF32> input = ConvertBufferedImage.convertFromPlanar(image,null, true, GrayF32.class);
final Planar<GrayF32> hsv = input.createSameShape();
ColorHsv.rgbToHsv_F32(input,hsv);
final GrayF32 H = hsv.getBand(0);
final GrayF32 S = hsv.getBand(1);
/* Phase 2b): Determine thresholds. */
float maxDist2 = 0.4f*0.4f;
float adjustUnits = (float)(Math.PI/2.0);
/* Phase 3): For each pixel in the image, determine distance to average color. If color is closed, turn pixel white. */
final BufferedImage output = new BufferedImage(input.width,input.height,BufferedImage.TYPE_INT_RGB);
for(int y = 0; y < hsv.height; y++) {
for(int x = 0; x < hsv.width; x++) {
// Hue is an angle in radians, so simple subtraction doesn't work
float dh = UtilAngle.dist(H.unsafe_get(x,y),avgHsvColor[0]);
float ds = (S.unsafe_get(x,y)-avgHsvColor[1])*adjustUnits;
// this distance measure is a bit naive, but good enough for to demonstrate the concept
float dist2 = dh*dh + ds*ds;
if( dist2 <= maxDist2 ) {
output.setRGB(x,y,Color.WHITE.getRGB());
}
}
}
return output;
}
示例3
public BufferedImage getTransformedImage(BufferedImage in, boolean flip)
{
try
{
Planar<GrayF32> input = ConvertBufferedImage.convertFromPlanar(in, null, true, GrayF32.class);
RemovePerspectiveDistortion<Planar<GrayF32>> removePerspective =
new RemovePerspectiveDistortion<>(300, 418, ImageType.pl(3, GrayF32.class));
int start = longEdge();
if(flip)
{
start = (start+2)%4;
}
if( !removePerspective.apply(input,
new Point2D_F64(corners[start].x,corners[start].y),
new Point2D_F64(corners[(start+1)%4].x,corners[(start+1)%4].y),
new Point2D_F64(corners[(start+2)%4].x,corners[(start+2)%4].y),
new Point2D_F64(corners[(start+3)%4].x,corners[(start+3)%4].y)
) ){
return null;
}
Planar<GrayF32> output = removePerspective.getOutput();
return ConvertBufferedImage.convertTo_F32(output,null,true);
}
catch(Exception e)
{
return null;
}
}
示例4
/**
* HSV stores color information in Hue and Saturation while intensity is in Value. This computes a 2D histogram
* from hue and saturation only, which makes it lighting independent.
*/
public double[] coupledHueSat(byte[] image) throws IOException {
Planar<GrayF32> rgb = new Planar<GrayF32>(GrayF32.class,1,1,3);
Planar<GrayF32> hsv = new Planar<GrayF32>(GrayF32.class,1,1,3);
BufferedImage buffered = ImageIO.read(new ByteArrayInputStream(image));
if (buffered == null) {
throw new RuntimeException("Can't load image!");
}
rgb.reshape(buffered.getWidth(), buffered.getHeight());
hsv.reshape(buffered.getWidth(), buffered.getHeight());
ConvertBufferedImage.convertFrom(buffered, rgb, true);
ColorHsv.rgbToHsv_F32(rgb, hsv);
Planar<GrayF32> hs = hsv.partialSpectrum(0,1);
// The number of bins is an important parameter. Try adjusting it
Histogram_F64 histogram = new Histogram_F64(12,12);
histogram.setRange(0, 0, 2.0 * Math.PI); // range of hue is from 0 to 2PI
histogram.setRange(1, 0, 1.0); // range of saturation is from 0 to 1
// Compute the histogram
GHistogramFeatureOps.histogram(hs,histogram);
UtilFeature.normalizeL2(histogram); // normalize so that image size doesn't matter
return histogram.value;
}