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

本文介绍了一种基于图像处理的价签矫正算法。该算法通过轮廓检测实现,首先将输入图片转换为灰度图像并进行阈值处理,然后寻找轮廓并筛选出符合条件的矩形区域,对选定区域进行最小外接矩形计算并根据角度进行旋转矫正,最终保存矫正后的图像。
/**
 * 价签矫正
 */
public void getContouresPic (Bitmap source) {
    Mat imageSobleOutThreshold = new Mat();

    Mat gray = new Mat();
    Utils.bitmapToMat(source, imageSobleOutThreshold);
    Imgproc.cvtColor(imageSobleOutThreshold, imageSobleOutThreshold, Imgproc.COLOR_BGR2GRAY);

    Imgproc.threshold(imageSobleOutThreshold, gray, 125, 225, Imgproc.THRESH_BINARY);//maxVal就是控制黑白反转的,0是黑

    //Utils.matToBitmap(gray, source);
    
    ArrayList<RotatedRect> rects = new  ArrayList<RotatedRect>();
    ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(gray, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);


    for(int i=0;i<contours.size();i++){
        Rect rect = Imgproc.boundingRect(contours.get(i));
        if (rect.width > 500 && rect.width/rect.height == 1) {
            MatOfPoint2f mp2f = new MatOfPoint2f(contours.get(i).toArray());
            RotatedRect mr = Imgproc.minAreaRect(mp2f);

            double area = Math.abs(Imgproc.contourArea(mp2f));

            double angle = mr.angle+90;

            Mat ratationedImg = new Mat(gray.rows(), gray.cols(), CvType.CV_8UC3);
            ratationedImg.setTo(new Scalar(0, 0, 0));



            Point center = mr.center;//中心点
            Mat m2 = Imgproc.getRotationMatrix2D(center, angle, 1);
            Imgproc.warpAffine(imageSobleOutThreshold, ratationedImg, m2, imageSobleOutThreshold.size(), 1, 0, new Scalar(0) );//仿射变换


            Utils.matToBitmap(ratationedImg, source);

            
            File file = new File(Environment.getExternalStorageDirectory()+"/AiLingGong/", "ll"+System.currentTimeMillis()+".jpg");
            try {
                FileOutputStream out = new FileOutputStream(file);
                source.compress(Bitmap.CompressFormat.JPEG, 100, out);
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Log.d("----------------", "旋转角度是:"+angle+"------"+"-----"+mr);

        }
        
        }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值