All Downloads are FREE. Search and download functionalities are using the official Maven repository.

boofcv.alg.fiducial.square.FiducialSquareGenerator Maven / Gradle / Ivy

Go to download

BoofCV is an open source Java library for real-time computer vision and robotics applications.

There is a newer version: 1.1.6
Show newest version
/*
 * Copyright (c) 2021, Peter Abeles. All Rights Reserved.
 *
 * This file is part of BoofCV (http://boofcv.org).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package boofcv.alg.fiducial.square;

import boofcv.abst.distort.FDistort;
import boofcv.alg.drawing.FiducialImageGenerator;
import boofcv.alg.drawing.FiducialRenderEngine;
import boofcv.alg.filter.binary.ThresholdImageOps;
import boofcv.alg.misc.PixelMath;
import boofcv.struct.image.GrayU8;
import lombok.Getter;
import lombok.Setter;

/**
 * Generates images of square fiducials
 *
 * @author Peter Abeles
 */
public class FiducialSquareGenerator extends FiducialImageGenerator {
	/** length of the white border surrounding the fiducial */
	@Getter @Setter double whiteBorderDoc = 0;
	/** length of the black border */
	@Getter @Setter double blackBorder = 0.25;

	public FiducialSquareGenerator( FiducialRenderEngine renderer ) {
		this.renderer = renderer;
	}

	public void generate( GrayU8 image ) {
		renderer.init();

		// make sure the image is square and divisible by 8
		int s = image.width - (image.width%8);
		if (image.width != s || image.height != s) {
			GrayU8 tmp = new GrayU8(s, s);
			new FDistort(image, tmp).scaleExt().apply();
			image = tmp;
		}

		GrayU8 binary = ThresholdImageOps.threshold(image, null, 125, false);

		PixelMath.multiply(binary, 255, binary);

		double whiteBorder = whiteBorderDoc/markerWidth;
		double X0 = whiteBorder + blackBorder;
		double Y0 = whiteBorder + blackBorder;

		drawBorder();

		// Draw the image inside
		draw(image, X0, Y0, 1.0 - X0, 1.0 - Y0);
	}

	void drawBorder() {
		double whiteBorder = whiteBorderDoc/markerWidth;
		double X0 = whiteBorder + blackBorder;
		double Y0 = whiteBorder + blackBorder;

		// top and bottom
		rectangle(whiteBorder, whiteBorder, 1.0 - whiteBorder, Y0);
		rectangle(whiteBorder, 1.0 - Y0, 1.0 - whiteBorder, 1.0 - whiteBorder);

		// left and right sides
		rectangle(whiteBorder, Y0, X0, 1.0 - Y0);
		rectangle(1.0 - X0, Y0, 1.0 - whiteBorder, 1.0 - Y0);
	}

	/**
	 * Renders a binary square fiducial
	 *
	 * @param value Value encoded in the fiducial
	 * @param gridWidth number of cells wide the grid is
	 */
	public void generate( long value, int gridWidth ) {
		renderer.init();

		drawBorder();

		double whiteBorder = whiteBorderDoc/markerWidth;
		double X0 = whiteBorder + blackBorder;
//		double Y0 = whiteBorder+blackBorder;

		double bw = (1.0 - 2*X0)/gridWidth;

		// Draw the black corner used to ID the orientation
		square(X0, 1.0 - whiteBorder - blackBorder - bw, bw);

		final int bitCount = gridWidth*gridWidth - 4;
		for (int j = 0; j < bitCount; j++) {
			if ((value & (1L << j)) != 0) {
				box(bw, j, gridWidth);
			}
		}

		//		int s2 = (int)Math.round(ret.width*borderFraction);
//		int s5 = s2+square*(gridWidth-1);
//
//		int N = gridWidth*gridWidth-4;
//		for (int i = 0; i < N; i++) {
//			if( (value& (1<




© 2015 - 2024 Weber Informatics LLC | Privacy Policy