2数据集的建立
根据图片的分割写了可以批量处理的程序建立数据集,现在只是第一步,之后还要进行人工分类
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 chuli0 {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
for(int ii=1;ii<=195;ii++) {
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:\\atom\\atom\\baidu_download\\4\\"+ii+".jpg");
if(mat.width()*mat.height()<120000)
continue;
Size size=new Size(400,300);
Imgproc.resize(mat, mat, size);
Mat mat0=mat.clone();
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.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)
contours.remove(i);
else
{
if(r.tl().x>10&&r.tl().y>10&&r.br().x+10<mat0.width()&&r.br().y+10<mat0.height()) {
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);
}
}
}
if(!out.empty())
Imgcodecs.imwrite("D:\\atom\\atom\\baidu_download\\newche\\"+ii+".jpg", out);
}
}
}
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 chuli1 {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
for(int iii=1;iii<=73;iii++) {
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:\\atom\\atom\\baidu_download\\newche\\ ("+iii+").jpg");
Size size=new Size(320,100);
Imgproc.resize(src, src, size);
Rect r0=new Rect(10,10,300,80);
mat=src.clone();
mat0=src.clone();
Imgproc.cvtColor(mat,mat,Imgproc.COLOR_BGR2GRAY);
Imgproc.Canny(mat, mat, 60, 80);
Imgproc.morphologyEx(mat, mat, Imgproc.MORPH_CLOSE, k0);
Imgproc.morphologyEx(mat, mat, Imgproc.MORPH_CLOSE, k1);
List<MatOfPoint> contours=new ArrayList<MatOfPoint>();
Mat out=new Mat();
Scalar color=new Scalar(255,255,255);
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.area()<800||r.area()>8000)
contours.remove(i);
else {
Imgproc.rectangle(mat0, r, color);
Mat mm=mat0.submat(r);
xxx.add(mm);
point.add(r.br());
}
}
for(int i=0;i<xxx.size();i++) {
Imgcodecs.imwrite("D:\\atom\\atom\\baidu_download\\danyitupian0\\("+iii+""+i+").jpg", xxx.get(i));
}
}}
}