提取车牌轮廓

public class Test {

    public static void main(String[] argv) throws InterruptedException {

        //gray

        Mat image = ImageUtil.readImage("E:/DataExamples/car2.jpg");

        Mat filter = new Mat();

        Mat kernel = new Mat(0, -1 ,0, -1, 5, -1, 0, -1, 0);
        filter2D(image, filter,CV_8UC3,kernel);

        Mat grayImage = new Mat();

       // ImageUtil.showImage(image,"car");
        Mat blur = new Mat();
        GaussianBlur(image,blur,new Size(3,3),2, 4, BORDER_DEFAULT);
        cvtColor(image,grayImage,COLOR_BGR2GRAY);
        //erode(grayImage, grayImage, new Mat(0, -1 ,0, -1, 5, -1, 0, -1, 0));
        // dilate canny output to remove potential
        // holes between edge segments
      //  dilate(grayImage, grayImage, new Mat(0, -1 ,0, -1, 5, -1, 0, -1, 0));

        Mat grad_x = new Mat();
        Mat grad_y = new Mat();
        Mat abs_grad_x = new Mat();
        Mat abs_grad_y = new Mat();
        Sobel( grayImage, grad_x, CMP_GE, 1, 0, 3, 1, 0, BORDER_DEFAULT );
        convertScaleAbs( grad_x, abs_grad_x );

        Sobel( grayImage, grad_y, CMP_GE, 0, 1, 3, 1, 0, BORDER_DEFAULT );

        convertScaleAbs( grad_y, abs_grad_y );
        Mat grad = new Mat();
        addWeighted( abs_grad_x, 0, abs_grad_y, 1, 0, grad );

        Mat img_threshold = new Mat();
        threshold(grad, img_threshold, 0, 255,CV_THRESH_BINARY_INV+ CV_THRESH_BINARY);

       // threshold(grad, img_threshold, 35, 255,CV_THRESH_BINARY);
        Mat element = getStructuringElement(MORPH_RECT, new Size(3, 3) );

        morphologyEx(img_threshold, img_threshold, MORPH_CLOSE, element);

        ImageUtil.showImage(img_threshold,"car");
        MatVector contours = new MatVector();
        //MatVector<RotatedRect> rects;
        findContours(img_threshold,
            contours, // a vector of contours
            CV_RETR_EXTERNAL, // 提取外部轮廓
            CV_CHAIN_APPROX_NONE); // all pixels of each contours
        Mat result = new Mat();
        image.copyTo(result);
        drawContours(result, contours,
            -1, // draw all contours
            new Scalar(0, 0, 255,1)); // with a thickness of 1
       // ImageUtil.showImage(result,"car");
        ImageUtil.imageWrite(img_threshold,"E:/DataExamples/result.jpg");
       // cvSmooth(iplImage,iplImage,CV_GAUSSIAN,3);
     //   ImageUtil.showImage(img_threshold,"car");
        int t = 0;
        while (t< contours.size())
        {
            //Create bounding rect of object

            RotatedRect mr = minAreaRect(contours.get(t));
            Rect safeBoundRect1 = new Rect((int)mr.boundingRect().x(), (int)mr.boundingRect().y(),
                (int)mr.boundingRect().width(), (int)mr.boundingRect().height());
            rectangle(blur, safeBoundRect1,
                new Scalar(0, 230, 255,1)); // with a thickness of 1
           // ImageUtil.showImage(blur,"blur");
            // boudRect的左上的x和y有可能小于0
            float tl_x = mr.boundingRect().x() > 0 ? mr.boundingRect().x() : 0;
            float tl_y = mr.boundingRect().y() > 0 ? mr.boundingRect().y() : 0;
            // boudRect的右下的x和y有可能大于src的范围
//            float br_x = mr.boundingRect().x() + mr.boundingRect().width() < img_threshold.cols() ?
//                (mr.boundingRect().x() + mr.boundingRect().width() - 1):(img_threshold.cols() - 1);
            float br_x = 0f;

            if(mr.boundingRect().x() + mr.boundingRect().width() < img_threshold.cols()){

                br_x = mr.boundingRect().x() + mr.boundingRect().width() - 1;
                //System.out.println("mr.boundingRect().width():"+mr.boundingRect().width());
                //System.out.println("mr.boundingRect().x():"+mr.boundingRect().x());
            } else {

                br_x = img_threshold.cols() - 1;
               // System.out.println("2:"+br_x);
            }
//            float br_y = mr.boundingRect().y() + mr.boundingRect().height() < img_threshold.rows() ?
//                (mr.boundingRect().y() + mr.boundingRect().height() - 1):(img_threshold.rows() - 1);
            float br_y = 0f;
            if(mr.boundingRect().y() + mr.boundingRect().height() < img_threshold.rows()){

                br_y = mr.boundingRect().y() + mr.boundingRect().height() - 1;
                //System.out.println("mr.boundingRect().height():"+mr.boundingRect().height());
               // System.out.println("mr.boundingRect().y():"+mr.boundingRect().y());
            } else {

                br_y = img_threshold.rows()  - 1;
               // System.out.println("3:"+br_y);
            }
            float roi_width = br_x - tl_x;
            float roi_height = br_y - tl_y;
//            System.out.println("br_x"+br_x);
//            System.out.println("tl_x"+tl_x);
         //   mr.boundingRect()
           // Thread.sleep(5000);
            if (roi_width <= 0 || roi_height <= 0){

                t++;
                continue;
            }
            // 新建一个mat,确保地址不越界,以防mat定位roi时抛异常
            Rect safeBoundRect = new Rect((int)tl_x, (int)tl_y, (int)roi_width, (int)roi_height);
            Mat cut = new Mat(img_threshold,safeBoundRect);
           // ImageUtil.showImage(cut,"car");
            Thread.sleep(1000);
            t++;

        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值