在opencv Java中创建感兴趣区域(ROI)
我今天试图在Opencv中创建一个感兴趣的区域,但没有成功,我的项目是关于一个交通计数器应用程序,我根据车辆的斑点跟踪车辆,为了减少处理时间,我设置了最小和最大斑点,但这还不够,我想创建一个感兴趣的区域,Opencv将在其中进行所有处理,我尝试检测道路,但它没有给我想要的结果,下面是我实现的代码在opencv Java中创建感兴趣区域(ROI),java,c#,opencv,Java,C#,Opencv,我今天试图在Opencv中创建一个感兴趣的区域,但没有成功,我的项目是关于一个交通计数器应用程序,我根据车辆的斑点跟踪车辆,为了减少处理时间,我设置了最小和最大斑点,但这还不够,我想创建一个感兴趣的区域,Opencv将在其中进行所有处理,我尝试检测道路,但它没有给我想要的结果,下面是我实现的代码 private static void segmentation(Mat src) { Mat matGray = src.clone(); matEdges =
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;
}
private静态空白分割(Mat src){
Mat matGray=src.clone();
matEdges=新垫(matGray.height()、matGray.width()、CvType.CV_8UC1);
//Imgproc.cvt颜色(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.腐蚀(matEdges,matEdges,Imgproc.getStructuringElement(Imgproc.morp___RECT,新尺寸(2,2));
}
专用静态空隙探测线(Mat src){
分段(src);
//检测线矩阵
垫线=新垫();
LineLeftLane=新行(0,src.height(),src.width(),0);
Line rightLane=新行(src.width(),src.height(),0,0);
//直线检测
Imgproc.HoughLines(matEdges,lines,1,Math.PI/180,70);
double maxLaneY=src.height();
布尔值isLeftHorizontal=true;
布尔值isRightHorizontal=true;
double midLaneX=src.width()/2;
双minLaneX=0;
double maxLaneX=src.width();
双minLeftLaneY=3*src.height()/4;
double minRightLaneY=3*src.height()/4;
//获取每条直线的ρ和θ值,并将其转换为笛卡尔空间
对于(int j=0;j3*Math.PI/4)){
双topX=rho/Math.cos(θ);
双底x=(rho-src.width()*Math.sin(θ))/Math.cos(θ);
if(minLaneXminRightLaneY&&rightY0&&i.x0&&i.y
这是我的代码结果,但我希望它看起来像图2
如果我的想法行不通,至少你可以帮助我如何通过点击视频中的图像来选择mat点,然后从这些点创建一个矩形并将其用作ROI,我尝试过在线搜索,但不幸的是我没有成功,像
C#
这样的语言有mat.setRoi()这样的方法
但不是在Java中我已经解决了我的问题,JavaFX
(以及Swing)可以监听鼠标活动,所以下面是我要做的
我将鼠标事件侦听器附加到根视图并获取所有点,即全部4点
root.setOnKeyPressed(eventHandler)
然后我检查鼠标是否在我的窗口内
if (mousePosition.inside(window.getImageArea()))
{
//if true the draw
}
});
if (mousePosition.inside(window.getImageArea()))
{
//if true the draw
}
});