其他分享
首页 > 其他分享> > opencv 尝试定位车牌号

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

 效果如下:

 

 

 

 

 

 

标签:尝试,Imgproc,Mat,s1,车牌号,opencv,import,new
来源: https://www.cnblogs.com/duanzheng/p/16124112.html