boofcv.alg.filter.binary.Contour Java Examples

The following examples show how to use boofcv.alg.filter.binary.Contour. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ShapeCentroidDistance.java    From cineast with MIT License 6 votes vote down vote up
/**
 *
 * @param shot
 */
@Override
public void processSegment(SegmentContainer shot) {

    BufferedImage image = shot.getAvgImg().getBufferedImage();

    List<Contour> contours = ContourHelper.getContours(image);
    List<Point2D_I32> contour = contours.get(0).internal.get(0);

    if (image != null) {
        FastFourierTransformer transformer = new FastFourierTransformer(DftNormalization.STANDARD);
        double[] distancefunction = ContourHelper.centroidDistance(contour, true);
        Complex[] signature = transformer.transform(distancefunction, TransformType.FORWARD);
        float[] descriptors = new float[DESCRIPTOR_LENGTH];
        for (int i = 1;i<DESCRIPTOR_LENGTH;i++) {
            descriptors[i] = (float) (signature[i].abs() / signature[0].abs());
        }
        this.persist(shot.getId(), new FloatVectorImpl(descriptors));
    }
}
 
Example #2
Source File: ShapeCentroidDistance.java    From cineast with MIT License 6 votes vote down vote up
/**
 *
 * @param sc
 * @param qc
 * @return
 */
@Override
public List<ScoreElement> getSimilar(SegmentContainer sc, ReadableQueryConfig qc) {

    BufferedImage image = sc.getAvgImg().getBufferedImage();

    qc = setQueryConfig(qc);

    List<Contour> contours = ContourHelper.getContours(image);
    List<Point2D_I32> contour =  contours.get(0).internal.get(0);

    if (image != null) {
        FastFourierTransformer transformer = new FastFourierTransformer(DftNormalization.STANDARD);
        double[] distancefunction = ContourHelper.centroidDistance(contour, true);
        Complex[] signature = transformer.transform(distancefunction, TransformType.FORWARD);
        float[] descriptors = new float[DESCRIPTOR_LENGTH];
        for (int i = 1;i<DESCRIPTOR_LENGTH;i++) {
            descriptors[i] = (float) (signature[i].abs() / signature[0].abs());
        }
        return this.getSimilar(descriptors, qc);
    } else {
        return new ArrayList<>();
    }
}
 
Example #3
Source File: ContourHelper.java    From cineast with MIT License 6 votes vote down vote up
/**
 * Applies a contour-detection algorithm on the provided image and returns a list of detected contours. First, the image
 * is converted to a BinaryImage using a threshold algorithm (Otsu). Afterwards, blobs in the image are detected using
 * an 8-connect rule.
 *
 * This method provides the best results if the image is a black & white, i.e. factually binary, image!
 * See {@link ContourHelper#segmentImageByColour(BufferedImage,float[])} to convert a coloured image to a binary image.
 *
 * @param image BufferedImage in which contours should be detected.
 * @return List of contours.
 */
public static List<Contour> getContours(BufferedImage image) {
    /* Draw a black frame around to image so as to make sure that all detected contours are internal contours. */
    BufferedImage resized = new BufferedImage(image.getWidth() + 4, image.getHeight() + 4, image.getType());
    Graphics g = resized.getGraphics();
    g.setColor(Color.BLACK);
    g.fillRect(0,0,resized.getWidth(),resized.getHeight());
    g.drawImage(image, 2,2, image.getWidth(), image.getHeight(), null);

    /* Convert to BufferedImage to Gray-scale image and prepare Binary image. */
    GrayF32 input = ConvertBufferedImage.convertFromSingle(resized, null, GrayF32.class);
    GrayU8 binary = new GrayU8(input.width,input.height);
    GrayS32 label = new GrayS32(input.width,input.height);

    /* Select a global threshold using Otsu's method and apply that threshold. */
    double threshold = GThresholdImageOps.computeOtsu(input, 0, 255);
    ThresholdImageOps.threshold(input, binary,(float)threshold,true);

    /* Remove small blobs through erosion and dilation;  The null in the input indicates that it should internally
     * declare the work image it needs this is less efficient, but easier to code. */
    GrayU8 filtered = BinaryImageOps.erode8(binary, 1, null);
    filtered = BinaryImageOps.dilate8(filtered, 1, null);

    /* Detect blobs inside the image using an 8-connect rule. */
    return BinaryImageOps.contour(filtered, ConnectRule.EIGHT, label);
}
 
Example #4
Source File: LightfieldFourier.java    From cineast with MIT License 5 votes vote down vote up
/**
 * Extracts the Lightfield Fourier descriptors from a provided BufferedImage. The returned list contains
 * elements for each identified contour of adequate size.
 *
 * @param image Image for which to extract the Lightfield Fourier descriptors.
 * @param poseidx Poseidx of the extracted image.
 * @return List of descriptors for image.
 */
@Override
protected List<float[]> featureVectorsFromImage(BufferedImage image, int poseidx) {
    final List<Contour> contours = ContourHelper.getContours(image);
    final List<float[]> features = new ArrayList<>();

    /* Select the largest, inner contour from the list of available contours. */
    for (Contour contour : contours) {
        for (List<Point2D_I32> inner : contour.internal) {
            /* Check size of selected contour. */
            if (inner.size() < SIZE * 2) {
              continue;
            }

            /* Calculate the descriptor for the selected contour. */
            double[] cds = ContourHelper.centroidDistance(inner, true);
            Complex[] results = this.transformer.transform(cds, TransformType.FORWARD);
            double magnitude = results[0].abs();
            float[] feature = new float[SIZE];
            for (int i = 1; i < SIZE; i++) {
                feature[i] = (float) (results[i+1].abs() / magnitude);
            }
            feature = MathHelper.normalizeL2InPlace(feature);
            feature[0] = poseidx;
            features.add(feature);
        }
    }

    return features;
}
 
Example #5
Source File: MotionDetector.java    From Telephoto with Apache License 2.0 4 votes vote down vote up
public synchronized GrayU8 addImage(GrayU8 newImage, int[][] matrix) {
    int width = newImage.width;
    int height = newImage.height;

    // Изменилось разрешение детектора - пересоздаем всё
    if (oldWidth != width || oldHeight != height) {
        //oldGrey = null;
        oldWidth = width;
        oldHeight = height;
        segmented = new GrayU8(oldWidth, oldHeight);
        homeToWorld.a13 = width / 2;
        homeToWorld.a23 = height / 2;

        background.initialize(width * 2, height * 2, homeToWorld);
        labeledObjects = new GrayS32(width, height);
    }

    background.segment(firstToCurrent32, newImage, segmented);
    background.updateBackground(firstToCurrent32, newImage);

    BinaryImageOps.removePointNoise(segmented, segmented);
    List<Contour> contours = BinaryImageOps.contour(segmented, ConnectRule.EIGHT, labeledObjects);

    int bigObjects = 0;
    if (contours.size() > 0) {
        for (Contour c : contours) {
            int minx = Integer.MAX_VALUE;
            int miny = Integer.MAX_VALUE;
            int maxx = Integer.MIN_VALUE;
            int maxy = Integer.MIN_VALUE;
            for (Point2D_I32 point : c.external) {
                if (minx > point.getX()) {
                    minx = point.getX();
                }
                if (maxx < point.getX()) {
                    maxx = point.getX();
                }
                if (miny > point.getY()) {
                    miny = point.getY();
                }
                if (maxy < point.getY()) {
                    maxy = point.getY();
                }
            }
            int length = Math.min((maxx - minx), (maxy - miny));
            //System.out.println("Length: " + length);
            if (length > 5) {
                if (checkMatrix(width, height, matrix, minx, maxx, miny, maxy)) {
                    bigObjects++;
                }
            }
        }
        if (bigObjects > 0) {
            return segmented;
        }
    }
    return null;
}