在opencv Java中创建感兴趣区域(ROI)

时间:2016-01-19 15:43:55

标签: java c# opencv

我今天试图在Opencv中创建一个感兴趣的区域但是根本没有成功,我的项目是关于交通计数器应用程序,我正在跟踪基于他们的Blob的车辆,以减少我设置的最小和最大的处理时间blob但这还不够,我想创建一个感兴趣的区域,Opencv将在那里进行所有处理,我尝试检测道路但是它没有给我想要的结果,这里是我实现的代码

<option value="&quot;asd">test</option>
<option value="&#34;asd">test</option>
<option value='"asd'>test</option>
<option value='&quot;asd'>test</option>
<option value='&#34;asd'>test</option>
<option value=&quot;asd>test</option>
<option value=&#34;asd>test</option>

以下是我的代码结果,但我希望它看起来像Image 2

image1

image2

如果我的想法至少无法工作,你可以通过点击视频中的图像来帮助我如何选择垫点,然后从这些点创建一个矩形并将其用作ROI,我尝试在线搜索但不幸的是我如果没有成功,像 private static void segmentation(Mat src) { Mat matGray = src.clone(); matEdges = new Mat(matGray.height(), matGray.width(), CvType.CV_8UC1); // Imgproc.cvtColor(matGray, matGray, Imgproc.COLOR_GRAY2RGB, 0); matGray.submat(0, matGray.height(), (2 * matGray.width()) / 3, matGray.width()).copyTo(matEdges.submat(0, matGray.height(), (2 * matGray.width()) / 3, matGray.width())); Imgproc.adaptiveThreshold(matEdges, matEdges, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 3, -1.5); Imgproc.erode(matEdges, matEdges, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2))); } private static void detectLane(Mat src) { segmentation(src); //Matrix of detected lines Mat lines = new Mat(); Line leftLane = new Line(0, src.height(), src.width(), 0); Line rightLane = new Line(src.width(), src.height(), 0, 0); //Straight line detection Imgproc.HoughLines(matEdges, lines, 1, Math.PI / 180, 70); double maxLaneY = src.height(); boolean isLeftHorizontal = true; boolean isRightHorizontal = true; double midLaneX = src.width() / 2; double minLaneX = 0; double maxLaneX = src.width(); double minLeftLaneY = 3 * src.height() / 4; double minRightLaneY = 3 * src.height() / 4; //Get rho and theta values for every line and convert it to cartesian space for (int j = 0; j < lines.cols(); j++) { double[] vec = lines.get(0, j); double rho = vec[0], theta = vec[1]; //Vertical-ish lines conversion if ((theta < Math.PI / 4 || theta > 3 * Math.PI / 4)) { double topX = rho / Math.cos(theta); double bottomX = (rho - src.width() * Math.sin(theta)) / Math.cos(theta); if (minLaneX < bottomX && bottomX <= midLaneX) { minLaneX = bottomX; leftLane.setLine(bottomX, topX, src.width(), 0); isLeftHorizontal = false; } if (midLaneX < bottomX && bottomX < maxLaneX) { maxLaneX = bottomX; rightLane.setLine(bottomX, topX, src.width(), 0); isRightHorizontal = false; } } //Horizontal-ish lines conversion else { double leftY = rho / Math.sin(theta); double rightY = (rho - src.width() * Math.cos(theta)) / Math.sin(theta); if (leftY > minLeftLaneY && leftY < maxLaneY && isLeftHorizontal) { minLeftLaneY = leftY; leftLane.setLine(0, src.width(), leftY, rightY); } if (rightY > minRightLaneY && rightY < maxLaneY && isRightHorizontal) { minRightLaneY = rightY; rightLane.setLine(src.width(), 0, rightY, leftY); } } } //Get the horizon Point i = Line.getIntersectionPoint(leftLane, rightLane); if (i != null && i.x > 0 && i.x < src.width() && i.y > 0 && i.y < src.height()) { //Draw the lines with horizon Imgproc.line(src, leftLane.getStart(), i, new Scalar(255, 0, 0), 3); Imgproc.line(src, rightLane.getStart(), i, new Scalar(255, 0, 0), 3); } else { //Draw the lines without horizon Imgproc.line(src, leftLane.getStart(), leftLane.getEnd(), new Scalar(255, 0, 0), 3); Imgproc.line(src, rightLane.getStart(), rightLane.getEnd(), new Scalar(255, 0, 0), 3); } //Draw segmentation borders //drawBordersToMRgba(); //Cleanup System.err.println("lines:" + lines.cols()); lines.release(); lines = null; } 这样的语言会有像C#这样的方法而不是Java

1 个答案:

答案 0 :(得分:0)

我已修复了我的问题,JavaFX(以及Swing)可以收听鼠标活动,所以这就是我的工作

我将鼠标事件监听器附加到根视图并获取所有点,即全部4个点             root.setOnKeyPressed(事件处理);

    imageView.setOnMousePressed(event -> {

        mousePosition.x = event.getX();
        mousePosition.y = event.getY();

然后我检查鼠标是否在我的窗口内

        if (mousePosition.inside(window.getImageArea()))
        {
            //if true the draw
        }

    });
相关问题