opencv 尝试定位车牌号

2022/4/9 23:50:00

本文主要是介绍opencv 尝试定位车牌号,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

通过opencv Java尝试定位车牌号位置

代码如下

package opencv;

import java.util.ArrayList;
import java.util.List;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class che {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat mat = new Mat();
Mat mat0 = new Mat();
Mat mat1 = mat0.clone();
Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,4));
Mat element0 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,1));
mat = Imgcodecs.imread("D:\\test\\che0.jpg");
Imgproc.cvtColor(mat,mat0,Imgproc.COLOR_BGR2GRAY);
Imgproc.boxFilter(mat0,mat1, -1,new Size(5,5));
Mat s1 = new Mat();
Imgproc.Sobel(mat1,s1, CvType.CV_32F,1,0);
Core.convertScaleAbs(s1,s1);
Imgproc.threshold(s1,s1,0,255,Imgproc.THRESH_BINARY|Imgproc.THRESH_OTSU);
Imgproc.dilate(s1, s1, element,new Point(-1, -1),2);
Imgproc.erode(s1, s1, element,new Point(-1, -1),4);
Imgproc.dilate(s1, s1, element,new Point(-1, -1),7);
Imgproc.erode(s1, s1, element0,new Point(-1, -1),1);
Imgproc.dilate(s1, s1, element0,new Point(-1, -1),7);
List<MatOfPoint> contours=new ArrayList<MatOfPoint>();
Mat out=new Mat();
Scalar color=new Scalar(255);
Imgproc.findContours(s1, contours,out, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
for(int i=contours.size()-1;i>=0;i--) {
Rect r=Imgproc.boundingRect(contours.get(i));
if(r.width<r.height*2.5||r.width>r.height*10||r.area()<6400)
contours.remove(i);
else
// Imgproc.drawContours(mat, contours, -1, color,2);
Imgproc.rectangle(mat, r, color);
}
Imgcodecs.imwrite("D:\\test\\newche0.jpg", s1);
HighGui.imshow("test", mat);
HighGui.waitKey(0);
HighGui.destroyAllWindows();
}
}

 效果如下:

 

 

 

 

 

 



这篇关于opencv 尝试定位车牌号的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程