OpenCV的+安卓+号牌识别(OpenCV + Android +水平矫正)

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);

Now I need to extract number Plate 

Please help me to convert following C++ code to java+opencv:.

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);  
    }  
} 
 
 
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);  
     }  

}  


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值