嗨,今天我有一个关于使用java在opencv中计算汽车的问题。我完全可以通过视频检测汽车,但我的车载计数器并不算完美,例如,如果我们有10辆汽车,它有时只计算8辆9.这是我的最后一年项目请帮帮我。这是关于汽车检测的代码
while (running) {
Mat previusFrame = new Mat();
Mat currentFrame = new Mat();
Mat grayImage1 = new Mat();
Mat grayImage2 = new Mat();
Mat differenceImage = new Mat();
Mat thresholdImage = new Mat();
camera.read(previusFrame);
Imgproc.resize(previusFrame, previusFrame, new Size(WIDTH, HEIGHT));
Imgproc.cvtColor(previusFrame, grayImage1, Imgproc.COLOR_BGR2GRAY);
while (running) {
boolean frameSucces = camera.read(currentFrame);
if (frameSucces) {
Imgproc.resize(currentFrame, currentFrame, new Size(WIDTH, HEIGHT));
Imgproc.cvtColor(currentFrame, grayImage2, Imgproc.COLOR_BGR2GRAY);
} else {
break;
}
//abbs differnce
Core.absdiff(grayImage1, grayImage2, differenceImage);
Imgproc.threshold(differenceImage, thresholdImage, SENSITIVE, 255, Imgproc.THRESH_BINARY);
Imgproc.blur(thresholdImage, thresholdImage, new Size(BLUR, BLUR));
Imgproc.threshold(thresholdImage, thresholdImage, SENSITIVE, 255, Imgproc.THRESH_BINARY);
//finding Movement
Mat heirarchy = new Mat();
Rect boundingRect = new Rect(0, 0, 0, 0);
List<MatOfPoint> contour = new ArrayList<MatOfPoint>();
Imgproc.findContours(thresholdImage, contour, heirarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contour.size(); i++) {
boundingRect = Imgproc.boundingRect(contour.get(i));
if (boundingRect.area() > 500) {
Imgproc.rectangle(currentFrame, boundingRect.tl(), boundingRect.br(), new Scalar(0, 0, 255), 2);
}
}
Imgproc.line(currentFrame, new Point(10, 200), new Point(currentFrame.width() - 10, 200), new Scalar(0, 255, 0), 3);
Moments moment = Imgproc.moments(thresholdImage);
double m10 = moment.get_m10();
double m01 = moment.get_m01();
double m_area = moment.get_m00();
int posx = (int) (m10 / m_area);
int posy = (int) (m01 / m_area);
if (posy > 202 && posy <= 245) {
i++;
}
label.setText("cars Number" + i);
MatOfByte matof = new MatOfByte();
Imgcodecs.imencode(".bmp", currentFrame, matof);
byte[] data = matof.toArray();
try {
image = ImageIO.read(new ByteArrayInputStream(data));
} catch (IOException ex) {
Logger.getLogger(MotionDetecter.class.getName()).log(Level.SEVERE, null, ex);
}
Graphics g = getGraphics();
g.drawImage(image, 0, 20, null);
grayImage2.copyTo(grayImage1);
}
camera.release();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(MotionDetecter.class.getName()).log(Level.SEVERE, null, ex);
}
}
}