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

        }
        
        }
}

优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/595082

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的、适应高吞吐量的数据库方案。

Apache Tomcat7、8、9(Java Web服务器)

Apache Tomcat7、8、9(Java Web服务器)

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。

Eclipse(集成开发环境)

Eclipse(集成开发环境)

Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。

Java Development Kit(Java开发工具)

Java Development Kit(Java开发工具)

JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。