首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenCV +安卓+车牌识别

OpenCV +安卓+车牌识别
EN

Stack Overflow用户
提问于 2014-09-11 01:16:42
回答 1查看 6.3K关注 0票数 0

我正在开发一个Android应用程序来检测车牌。我做了findContours级别的图像处理。现在我需要将以下C++代码转换为基于Opencv的Android java。

这是原始的image

这是在Otsu阈值image之后

这是我的andoid+opencv代码(100%工作)

代码语言:javascript
复制
ImageView imgView  = (ImageView) findViewById(R.id.imageView1);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),car);
//First convert Bitmap to Mat
Mat ImageMatin = new Mat ( bmp.getHeight(), bmp.getWidth(), CvType.CV_8U, new Scalar(4));    
Mat ImageMatout = new Mat ( bmp.getHeight(), bmp.getWidth(), CvType.CV_8U, new Scalar(4));
Mat ImageMatBk = new Mat ( bmp.getHeight(), bmp.getWidth(), CvType.CV_8U, new Scalar(4));
Mat ImageMatTopHat = new Mat ( bmp.getHeight(), bmp.getWidth(), CvType.CV_8U, new Scalar(4));
Mat temp = new Mat ( bmp.getHeight(), bmp.getWidth(), CvType.CV_8U, new Scalar(4));

Bitmap myBitmap32 = bmp.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(myBitmap32, ImageMatin);  


//Converting RGB to Gray.
Imgproc.cvtColor(ImageMatin, ImageMatBk, Imgproc.COLOR_RGB2GRAY,8);    

Imgproc.dilate(ImageMatBk, temp, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(9, 9)));
Imgproc.erode(temp, ImageMatTopHat, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(9,9)));  

//Core.absdiff(current, previous, difference);
Core.absdiff(ImageMatTopHat, ImageMatBk, ImageMatout);      

//Sobel operator in horizontal direction.    
Imgproc.Sobel(ImageMatout,ImageMatout,CvType.CV_8U,1,0,3,1,0.4,Imgproc.BORDER_DEFAULT);

//Converting GaussianBlur                   
Imgproc.GaussianBlur(ImageMatout, ImageMatout, new Size(5,5),2);    

Imgproc.dilate(ImageMatout, ImageMatout, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3)));

Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(17, 3));    
Imgproc.morphologyEx(ImageMatout, ImageMatout, Imgproc.MORPH_CLOSE, element);

//threshold image
Imgproc.threshold(ImageMatout, ImageMatout, 0, 255, Imgproc.THRESH_OTSU+Imgproc.THRESH_BINARY);

现在我需要提取车牌

请帮我把下面的C++代码转换成java+opencv:。

代码语言:javascript
复制
std::vector rects;  
std::vector<std::vector >::iterator itc = contours.begin();  
while (itc != contours.end())  
{  
    cv::RotatedRect mr = cv::minAreaRect(cv::Mat(*itc)); 
    float area = fabs(cv::contourArea(*itc));  
    float bbArea=mr.size.width * mr.size.height;  
    float ratio = area/bbArea;  
    if( (ratio < 0.45) || (bbArea < 400) ){  
        itc= contours.erase(itc);  
    }else{  
        ++itc;  
        rects.push_back(mr);  
    }  
} 
EN

回答 1

Stack Overflow用户

发布于 2014-09-11 09:32:29

特别关注http://docs.opencv.org/javathe documentation for findContours

而不是

代码语言:javascript
复制
std::vector<std::vector<cv::Point> > contours;

你将会有

代码语言:javascript
复制
java.util.ArrayList<MatOfPoint> contours;

您可以使用contours.listIterator()遍历列表。类似下面的代码(没有编译,更不用说运行了,很可能包含重大错误):

代码语言:javascript
复制
import java.util.*;
import org.opencv.imgproc.Imgproc;
import org.opencv.core.*;

/* ... */
ArrayList<RotatedRect> rects = new  ArrayList<RotatedRect>()
ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(image, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);

ListIterator<MatOfPoint> itc = contours.listIterator();
while(itc.hasNext())
{
     MatOfPoint2f mp2f = new MatOfPoint2f(itc.next().toArray());
     RotatedRect mr = Imgproc.minAreaRect(mp2f);
     double area = Math.abs(Imgproc.contourArea(mp2f));

     double bbArea= mr.size.area();  
     double ratio = area / bbArea;  
     if( (ratio < 0.45) || (bbArea < 400) )
     {  
         itc.remove();  // other than deliberately making the program slow,
                        // does erasing the contour have any purpose?
     }
     else
     {  
         rects.add(mr);  
     }  

}  
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25771332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档