0
我試圖在Opencv中創建一個感興趣的區域,但一直都沒有成功,我的項目是關於一個交通計數器應用程序,我正在根據他們的Blob ,爲了減少處理時間,我設置了最小和最大blob,但那還不夠,我想創建一個感興趣的區域,Opencv將會處理所有的處理,我試着檢測這條路,但它沒有給我想要的結果,這裏是代碼我實現在opencv Java中創建感興趣區域(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;
}
這裏是我的代碼的結果,但我希望它看起來像圖片2
如果我的想法不能至少工作,你能幫助我如何,我可以通過點擊視頻中的圖像,然後從這些點創建一個矩形,並用它作爲ROI挑墊點,我已經嘗試網上搜索,但不幸的是我還沒有成功,像C#
語言有像Mat.setRoi()
的方法,但不是在Java中