Java 在android中使用opencv检测玩具车后面

Java 在android中使用opencv检测玩具车后面,java,android,c++,opencv,image-processing,Java,Android,C++,Opencv,Image Processing,我有一个在java中使用opencv跟踪follow toy car的项目,但我不知道如何检测并为后面的汽车创建一个矩形。下面是我所有的想法和方式,我做,但它仍然没有发现好 我的源图像: 我的目标是发现车后。像这样的东西(我用颜料画:)) 我的想法是: _将rgb图像转换为灰度图像。使用Canny检测找到边缘,使用GaussianBlur使图像更平滑 Imgproc.cvtColor(mImageRGBA, thresholded, Imgproc.COLOR_RGB2GRAY,3); Im

我有一个在java中使用opencv跟踪follow toy car的项目,但我不知道如何检测并为后面的汽车创建一个矩形。下面是我所有的想法和方式,我做,但它仍然没有发现好

我的源图像:

我的目标是发现车后。像这样的东西(我用颜料画:))

我的想法是:

_将rgb图像转换为灰度图像。使用Canny检测找到边缘,使用GaussianBlur使图像更平滑

Imgproc.cvtColor(mImageRGBA, thresholded, Imgproc.COLOR_RGB2GRAY,3);
Imgproc.Canny(thresholded, thresholded, 78, 212);
Imgproc.GaussianBlur(thresholded, thresholded, new Size(5, 5), 2.2, 2);
我的形象会是这样的

_然后,我将使用findcontour找到汽车的轮廓并绘制它

List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat mHierarchy = new Mat();

Imgproc.findContours(thresholded, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
for( int i = 0; i< contours.size(); i++ ) {
      circle_contour = contours.get(i).toArray();
      double contour_length = circle_contour.length;
      if (contour_length < 50) continue;
      Imgproc.drawContours(thresholded,contours,i,new Scalar(255,255,255),2);
      .
      .
      .
List contours=new ArrayList();
Mat mHierarchy=新Mat();
Imgproc.findContours(阈值、轮廓、层次结构、Imgproc.RETR\u外部、Imgproc.CHAIN\u近似简单);
对于(int i=0;i
我的形象是这样的:

最后,我不会画轮廓,我会用Minareac来找到大小合适的矩形,我会画这个矩形

//Imgproc.drawContours(thresholded,contours,i,new Scalar(255,255,255),2);// remove this line and continue to find rectangle for car.
     .
     .
     .

Car_Rect = Imgproc.minAreaRect(new MatOfPoint2f(contours.get(i).toArray()));
float rect_width = Car_Rect.boundingRect().width;
float rect_height = Car_Rect.boundingRect().height;
float rect_ratio = rect_width/rect_height;
Point points[] = new Point[4];
Car_Rect.points(points);
if((Car_Rect.angle<=value1)&&(rect_width>value7)&&(rect_width<value8)&&(rect_height>value9)&&(rect_height<value10))
{
      Imgproc.line(thresholded,points[0],points[1],new Scalar(255, 255, 255),2);
      Imgproc.line(thresholded,points[1],points[2],new Scalar(255,255,255),2);
      Imgproc.line(thresholded,points[2],points[3],new Scalar(255,255,255),2);
      Imgproc.line(thresholded,points[3],points[0],new Scalar(255,255,255),2);

 }
//Imgproc.drawContours(阈值化,轮廓,i,新标量(255255255),2);//删除此行并继续查找汽车的矩形。
.
.
.
Car_Rect=Imgproc.minareact(新的MatOfPoint2f(cours.get(i).toArray());
float rect_width=Car_rect.boundingRect().width;
float rect_height=Car_rect.boundingRect().height;
浮动矩形比=矩形宽度/矩形高度;
点点[]=新点[4];
汽车直线点(点);

if((轿厢直角值7)和&(轿厢宽度值9)&&(rect_height我也做过类似的事情。这需要一些努力,但对于您的需求来说非常有限。问题是您可以使用haar分类器或级联分类器来实现这一点。但要使用它进行检测,您需要一个XML文件,其中包含它应该识别的图像的数据。Opencv提供了一些默认的分类器XML我不喜欢检测人脸、眼睛、嘴巴、车牌等。但不幸的是,OpenCv没有提供和xml来检测汽车的后视图,因此一种方法是根据您的要求创建您自己的分类器xml,并执行与对象识别(如人脸识别)类似的编码。别忘了使用xml更改人脸分类器您自定义的xml文件

先读这个

检查此链接以了解分步流程


谢谢你的回答,因为我需要一个稳定的矩形作为对象,所以你的方法是检测对象并获得宽度和高度稳定的矩形???,还有一个问题是对象的阴影,有阴影的对象可以吗?不管怎样,非常感谢你的帮助阴影效果会自动产生注意。有一件事你必须注意,这些特征是用haar分类器发现的,但是你会给出围绕对象绘制的矩形的尺寸。所以你手动给出矩形的高度和宽度。好了,非常感谢你,也许我会按照你的方式,我对这种方法一无所知“Haar特征的级联分类器”。看起来像是模板匹配。但我会研究它,再一次,非常感谢:)