Java Code Examples for org.opencv.core.Core#line()

The following examples show how to use org.opencv.core.Core#line() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
private void blankRotatedRect(Mat mat, final RotatedRect rect) {
	final Mat tempMat = Mat.zeros(mat.size(), CvType.CV_8UC1);

	final Point points[] = new Point[4];
	rect.points(points);
	for (int i = 0; i < 4; ++i) {
		Core.line(tempMat, points[i], points[(i + 1) % 4], new Scalar(255, 255, 255));
	}

	final Mat tempMask = Mat.zeros((mat.rows() + 2), (mat.cols() + 2), CvType.CV_8UC1);
	Imgproc.floodFill(tempMat, tempMask, rect.center, new Scalar(255, 255, 255), null, new Scalar(0, 0, 0),
			new Scalar(254, 254, 254), 4);

	if (logger.isTraceEnabled()) {
		String filename = String.format("poly.png");
		final File file = new File(filename);
		filename = file.toString();
		Highgui.imwrite(filename, tempMat);
	}

	mat.setTo(new Scalar(0, 0, 0), tempMat);
}
 
Example 2
public static void main (String[] args) {
	CVLoader.load();
	
	// load the image
	Mat img = Highgui.imread("data/topdown-6.jpg");
	
	// generate gray scale and blur
	Mat gray = new Mat();
	Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
	Imgproc.blur(gray, gray, new Size(3, 3));
	
	// detect the edges
	Mat edges = new Mat();
	int lowThreshold = 50;
	int ratio = 3;
	Imgproc.Canny(gray, edges, lowThreshold, lowThreshold * ratio);
	
	Mat lines = new Mat();
	Imgproc.HoughLinesP(edges, lines, 1, Math.PI / 180, 50, 50, 10);
	
	for(int i = 0; i < lines.cols(); i++) {
		double[] val = lines.get(0, i);
		Core.line(img, new Point(val[0], val[1]), new Point(val[2], val[3]), new Scalar(0, 0, 255), 2);
	}
	
	ImgWindow.newWindow(edges);
	ImgWindow.newWindow(gray);
	ImgWindow.newWindow(img);
}
 
Example 3
private Optional<MatOfPoint2f> estimatePatternRect(Mat traceMat, MatOfPoint2f boardRect) {

		// We use this to calculate the angle
		final RotatedRect boardBox = Imgproc.minAreaRect(boardRect);
		final double boardBoxAngle = boardBox.size.height > boardBox.size.width ? 90.0 + boardBox.angle
				: boardBox.angle;

		// This is the board corners with the angle eliminated
		final Mat unRotMat = getRotationMatrix(massCenterMatOfPoint2f(boardRect), boardBoxAngle);
		final MatOfPoint2f unRotatedRect = rotateRect(unRotMat, boardRect);

		// This is the estimated projection area that has minimum angle (Not
		// rotated)
		final MatOfPoint2f estimatedPatternSizeRect = estimateFullPatternSize(unRotatedRect);

		// This is what we'll use as the transformation target and bounds given
		// back to the cameramanager
		boundsRect = Imgproc.minAreaRect(estimatedPatternSizeRect);

		
		if (boundsRect.boundingRect().x < 0 || boundsRect.boundingRect().y < 0 ||
			boundsRect.boundingRect().y+boundsRect.boundingRect().height >= this.camera.getViewSize().getHeight() ||
			boundsRect.boundingRect().x+boundsRect.boundingRect().width >= this.camera.getViewSize().getWidth())
		{
			logger.debug("Pattern found but autocalibration failed--Cannot dedistort within camera bounds, make sure the projector area is a perfect rectangle and try again");
			return Optional.empty();
		}

		// We now rotate the estimation back to the original angle to use for
		// transformation source
		final Mat rotMat = getRotationMatrix(massCenterMatOfPoint2f(estimatedPatternSizeRect), -boardBoxAngle);

		final MatOfPoint2f rotatedPatternSizeRect = rotateRect(rotMat, estimatedPatternSizeRect);

		if (logger.isTraceEnabled()) {
			logger.trace("center {} angle {} width {} height {}", boardBox.center, boardBoxAngle, boardBox.size.width,
					boardBox.size.height);

			logger.debug("boundsRect {} {} {} {}", boundsRect.boundingRect().x, boundsRect.boundingRect().y,
					boundsRect.boundingRect().x + boundsRect.boundingRect().width,
					boundsRect.boundingRect().y + boundsRect.boundingRect().height);

			Core.circle(traceMat, new Point(boardRect.get(0, 0)[0], boardRect.get(0, 0)[1]), 1, new Scalar(255, 0, 0),
					-1);
			Core.circle(traceMat, new Point(boardRect.get(1, 0)[0], boardRect.get(1, 0)[1]), 1, new Scalar(255, 0, 0),
					-1);
			Core.circle(traceMat, new Point(boardRect.get(2, 0)[0], boardRect.get(2, 0)[1]), 1, new Scalar(255, 0, 0),
					-1);
			Core.circle(traceMat, new Point(boardRect.get(3, 0)[0], boardRect.get(3, 0)[1]), 1, new Scalar(255, 0, 0),
					-1);

			Core.line(traceMat, new Point(unRotatedRect.get(0, 0)[0], unRotatedRect.get(0, 0)[1]),
					new Point(unRotatedRect.get(1, 0)[0], unRotatedRect.get(1, 0)[1]), new Scalar(0, 255, 0));
			Core.line(traceMat, new Point(unRotatedRect.get(1, 0)[0], unRotatedRect.get(1, 0)[1]),
					new Point(unRotatedRect.get(2, 0)[0], unRotatedRect.get(2, 0)[1]), new Scalar(0, 255, 0));
			Core.line(traceMat, new Point(unRotatedRect.get(3, 0)[0], unRotatedRect.get(3, 0)[1]),
					new Point(unRotatedRect.get(2, 0)[0], unRotatedRect.get(2, 0)[1]), new Scalar(0, 255, 0));
			Core.line(traceMat, new Point(unRotatedRect.get(3, 0)[0], unRotatedRect.get(3, 0)[1]),
					new Point(unRotatedRect.get(0, 0)[0], unRotatedRect.get(0, 0)[1]), new Scalar(0, 255, 0));

			Core.line(traceMat, new Point(estimatedPatternSizeRect.get(0, 0)[0], estimatedPatternSizeRect.get(0, 0)[1]),
					new Point(estimatedPatternSizeRect.get(1, 0)[0], estimatedPatternSizeRect.get(1, 0)[1]),
					new Scalar(255, 255, 0));
			Core.line(traceMat, new Point(estimatedPatternSizeRect.get(1, 0)[0], estimatedPatternSizeRect.get(1, 0)[1]),
					new Point(estimatedPatternSizeRect.get(2, 0)[0], estimatedPatternSizeRect.get(2, 0)[1]),
					new Scalar(255, 255, 0));
			Core.line(traceMat, new Point(estimatedPatternSizeRect.get(3, 0)[0], estimatedPatternSizeRect.get(3, 0)[1]),
					new Point(estimatedPatternSizeRect.get(2, 0)[0], estimatedPatternSizeRect.get(2, 0)[1]),
					new Scalar(255, 255, 0));
			Core.line(traceMat, new Point(estimatedPatternSizeRect.get(3, 0)[0], estimatedPatternSizeRect.get(3, 0)[1]),
					new Point(estimatedPatternSizeRect.get(0, 0)[0], estimatedPatternSizeRect.get(0, 0)[1]),
					new Scalar(255, 255, 0));

			Core.line(traceMat, new Point(rotatedPatternSizeRect.get(0, 0)[0], rotatedPatternSizeRect.get(0, 0)[1]),
					new Point(rotatedPatternSizeRect.get(1, 0)[0], rotatedPatternSizeRect.get(1, 0)[1]),
					new Scalar(255, 255, 0));
			Core.line(traceMat, new Point(rotatedPatternSizeRect.get(1, 0)[0], rotatedPatternSizeRect.get(1, 0)[1]),
					new Point(rotatedPatternSizeRect.get(2, 0)[0], rotatedPatternSizeRect.get(2, 0)[1]),
					new Scalar(255, 255, 0));
			Core.line(traceMat, new Point(rotatedPatternSizeRect.get(3, 0)[0], rotatedPatternSizeRect.get(3, 0)[1]),
					new Point(rotatedPatternSizeRect.get(2, 0)[0], rotatedPatternSizeRect.get(2, 0)[1]),
					new Scalar(255, 255, 0));
			Core.line(traceMat, new Point(rotatedPatternSizeRect.get(3, 0)[0], rotatedPatternSizeRect.get(3, 0)[1]),
					new Point(rotatedPatternSizeRect.get(0, 0)[0], rotatedPatternSizeRect.get(0, 0)[1]),
					new Scalar(255, 255, 0));
		}

		return Optional.of(rotatedPatternSizeRect);
	}
 
Example 4
Source Project: ResistorScanner   File: ResistorImageProcessor.java    License: MIT License 4 votes vote down vote up
public Mat processFrame(CvCameraViewFrame frame)
{
    Mat imageMat = frame.rgba();
    int cols = imageMat.cols();
    int rows = imageMat.rows();

    Mat subMat = imageMat.submat(rows/2, rows/2+30, cols/2 - 50, cols/2 + 50);
    Mat filteredMat = new Mat();
    Imgproc.cvtColor(subMat, subMat, Imgproc.COLOR_RGBA2BGR);
    Imgproc.bilateralFilter(subMat, filteredMat, 5, 80, 80);
    Imgproc.cvtColor(filteredMat, filteredMat, Imgproc.COLOR_BGR2HSV);

    findLocations(filteredMat);

    if(_locationValues.size() >= 3)
    {
        // recover the resistor value by iterating through the centroid locations
        // in an ascending manner and using their associated colour values
        int k_tens = _locationValues.keyAt(0);
        int k_units = _locationValues.keyAt(1);
        int k_power = _locationValues.keyAt(2);

        int value = 10*_locationValues.get(k_tens) + _locationValues.get(k_units);
        value *= Math.pow(10, _locationValues.get(k_power));

        String valueStr;
        if(value >= 1e3 && value < 1e6)
            valueStr = String.valueOf(value/1e3) + " KOhm";
        else if(value >= 1e6)
            valueStr = String.valueOf(value/1e6) + " MOhm";
        else
            valueStr = String.valueOf(value) + " Ohm";

        if(value <= 1e9)
            Core.putText(imageMat, valueStr, new Point(10, 100), Core.FONT_HERSHEY_COMPLEX,
                         2, new Scalar(255, 0, 0, 255), 3);
    }

    Scalar color = new Scalar(255, 0, 0, 255);
    Core.line(imageMat, new Point(cols/2 - 50, rows/2), new Point(cols/2 + 50, rows/2 ), color, 2);
    return imageMat;
}