1图片的分割
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 che0 {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat mat=new Mat();
Mat k = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2,3));
Mat k0 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(12,8));
mat=Imgcodecs.imread("D:\\test\\che02.jpg");
Size size=new Size(400,300);
Imgproc.resize(mat, mat, size);
Mat mat0=mat.clone();
HighGui.imshow("原图",mat );
Imgproc.cvtColor(mat,mat,Imgproc.COLOR_BGR2GRAY);
Mat mat1=mat.clone();
Imgproc.GaussianBlur(mat,mat, new Size(9,9), 0, 0, Core.BORDER_DEFAULT);
Mat mat2=new Mat();
Core.subtract(mat,mat1,mat2);
Imgproc.threshold(mat2,mat2,0,255,Imgproc.THRESH_BINARY|Imgproc.THRESH_OTSU);
Imgproc.morphologyEx(mat2, mat2, Imgproc.MORPH_OPEN, k);
// Imgproc.morphologyEx(mat2, mat2, Imgproc.MORPH_OPEN, k);
// Imgproc.morphologyEx(mat2, mat2, Imgproc.MORPH_OPEN, k);
Imgproc.Canny(mat2, mat2, 0,0);
Imgproc.morphologyEx(mat2, mat2, Imgproc.MORPH_CLOSE, k0);
Imgproc.morphologyEx(mat2, mat2, Imgproc.MORPH_CLOSE, k0);
List<MatOfPoint> contours=new ArrayList<MatOfPoint>();
Mat out=new Mat();
Scalar color=new Scalar(0,0,0);
Imgproc.findContours(mat2, 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.height*2>r.width||r.area()<1500||r.area()>4000)
contours.remove(i);
else
{
Point p0=new Point(r.tl().x-10,r.tl().y-10);
Point p1=new Point(r.br().x+10,r.br().y+10);
Rect r0=new Rect(p0,p1);
out=mat0.submat(r0);
}
}
Imgcodecs.imwrite("D:\\test\\aaa00.jpg", out);
HighGui.imshow("原图0",mat2 );
HighGui.imshow("车牌",out );
HighGui.imshow("原图00",mat0 );
HighGui.waitKey(0);
HighGui.destroyAllWindows();
}
}
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 chepai {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat src=new Mat();
Mat mat = new Mat();
Mat mat0=new Mat();
Mat k = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,2));
Mat k0 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2,3));
Mat k1 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,3));
src = Imgcodecs.imread("D:\\test\\chepai02.jpg");
Size size=new Size(320,100);
Imgproc.resize(src, src, size);
Rect r0=new Rect(10,10,300,80);
mat=src.submat(r0);
mat0=mat.clone();
Imgproc.cvtColor(mat,mat,Imgproc.COLOR_BGR2GRAY);
// Imgproc.boxFilter(mat,mat, -1,new Size(5,5));
Imgproc.Canny(mat, mat, 60, 80);
// Imgproc.boxFilter(mat,mat, -1,new Size(5,5));
// Mat element0 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,1));
// Imgproc.dilate(mat, mat, element0,new Point(-1, -1),1);
Imgproc.morphologyEx(mat, mat, Imgproc.MORPH_CLOSE, k0);
Imgproc.morphologyEx(mat, mat, Imgproc.MORPH_CLOSE, k1);
// Imgproc.morphologyEx(mat, mat, Imgproc.MORPH_OPEN, k);
List<MatOfPoint> contours=new ArrayList<MatOfPoint>();
Mat out=new Mat();
Scalar color=new Scalar(0,255,0);
// Imgproc.morphologyEx(mat, mat, Imgproc.MORPH_CLOSE, k);
Imgproc.findContours(mat, contours,out, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
ArrayList<Mat> xxx=new ArrayList<Mat>();
ArrayList<Point> point =new ArrayList<Point>();
for(int i=contours.size()-1;i>=0;i--) {
Rect r=Imgproc.boundingRect(contours.get(i));
int flag=0;
for(Point p:point) {
if(Math.sqrt(Math.pow(r.br().x-p.x,2)+Math.pow(r.br().y-p.y,2))<10)
{
flag=1;
break;
}
}
if(flag==1||r.width*1.1>r.height||r.area()<800)
contours.remove(i);
else {
Imgproc.rectangle(mat0, r, color);
Mat mm=mat0.submat(r);
xxx.add(mm);
point.add(r.br());
}
// Imgproc.rectangle(mat0, r, color);
// Imgproc.drawContours(mat0, contours, -1, color,2);
// {
// Imgproc.rectangle(mat, r, color);
// out=mat.submat(r);
// }
}
for(int i=0;i<xxx.size();i++) {
HighGui.imshow(i+"",xxx.get(i) );
}
// HighGui.imshow("原图1",src );
// HighGui.imshow("原图",mat );
HighGui.imshow("原图0",mat0 );
HighGui.waitKey(0);
HighGui.destroyAllWindows();
}
}
其中主要使用了OPencv