|
|
|
@ -6,6 +6,7 @@ import java.util.Set;
|
|
|
|
|
import java.util.Vector;
|
|
|
|
|
|
|
|
|
|
import org.bytedeco.javacpp.BytePointer;
|
|
|
|
|
import org.bytedeco.javacpp.IntPointer;
|
|
|
|
|
import org.bytedeco.javacpp.opencv_core;
|
|
|
|
|
import org.bytedeco.javacpp.opencv_core.Mat;
|
|
|
|
|
import org.bytedeco.javacpp.opencv_core.MatVector;
|
|
|
|
@ -79,7 +80,7 @@ public class ImageUtil {
|
|
|
|
|
|
|
|
|
|
String tempPath = DEFAULT_BASE_TEST_PATH + "test/";
|
|
|
|
|
String filename = tempPath + "/100_yuantu.jpg";
|
|
|
|
|
// filename = tempPath + "/100_yuantu1.jpg";
|
|
|
|
|
filename = tempPath + "/100_yuantu1.jpg";
|
|
|
|
|
|
|
|
|
|
Mat src = opencv_imgcodecs.imread(filename);
|
|
|
|
|
|
|
|
|
@ -93,9 +94,9 @@ public class ImageUtil {
|
|
|
|
|
|
|
|
|
|
// Mat sobel = ImageUtil.scharr(grey, debug, tempPath);
|
|
|
|
|
|
|
|
|
|
Mat threshold = ImageUtil.threshold(sobel, debug, tempPath);
|
|
|
|
|
// Mat threshold = ImageUtil.threshold(sobel, debug, tempPath);
|
|
|
|
|
|
|
|
|
|
Mat morphology = ImageUtil.morphology(threshold, debug, tempPath);
|
|
|
|
|
Mat morphology = ImageUtil.morphology(ImageUtil.threshold(sobel, debug, tempPath), debug, tempPath);
|
|
|
|
|
|
|
|
|
|
MatVector contours = ImageUtil.contours(src, morphology, debug, tempPath);
|
|
|
|
|
|
|
|
|
@ -139,6 +140,7 @@ public class ImageUtil {
|
|
|
|
|
if (debug) {
|
|
|
|
|
opencv_imgcodecs.imwrite(tempPath + (debugMap.get("gray") + 100) + "_gray.jpg", dst);
|
|
|
|
|
}
|
|
|
|
|
inMat.release();
|
|
|
|
|
return dst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -230,18 +232,10 @@ public class ImageUtil {
|
|
|
|
|
public static Mat threshold(Mat inMat, Boolean debug, String tempPath) {
|
|
|
|
|
Mat dst = new Mat();
|
|
|
|
|
opencv_imgproc.threshold(inMat, dst, 0, 255, opencv_imgproc.CV_THRESH_OTSU + opencv_imgproc.CV_THRESH_BINARY);
|
|
|
|
|
|
|
|
|
|
/*for (int i = 0; i < dst.rows(); i++) {
|
|
|
|
|
for (int j = 0; j < dst.cols(); j++) {
|
|
|
|
|
if(dst.ptr(i, j).getInt() !=0 ) {
|
|
|
|
|
System.err.println(i + "\t" + j + "\t" +dst.ptr(i, j).getInt());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
if (debug) {
|
|
|
|
|
opencv_imgcodecs.imwrite(tempPath + (debugMap.get("threshold") + 100) + "_threshold.jpg", dst);
|
|
|
|
|
}
|
|
|
|
|
inMat.release();
|
|
|
|
|
return dst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -258,9 +252,8 @@ public class ImageUtil {
|
|
|
|
|
public static final int DEFAULT_MORPH_SIZE_WIDTH = 9;
|
|
|
|
|
public static final int DEFAULT_MORPH_SIZE_HEIGHT = 3;
|
|
|
|
|
public static Mat morphology(Mat inMat, Boolean debug, String tempPath) {
|
|
|
|
|
Mat dst = new Mat();
|
|
|
|
|
Mat dst = new Mat(inMat.size(), opencv_core.CV_8UC1);
|
|
|
|
|
Size size = new Size(DEFAULT_MORPH_SIZE_WIDTH, DEFAULT_MORPH_SIZE_HEIGHT);
|
|
|
|
|
|
|
|
|
|
Mat element = opencv_imgproc.getStructuringElement(opencv_imgproc.MORPH_RECT, size);
|
|
|
|
|
opencv_imgproc.morphologyEx(inMat, dst, opencv_imgproc.MORPH_CLOSE, element);
|
|
|
|
|
|
|
|
|
@ -269,14 +262,11 @@ public class ImageUtil {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 去除小连通区域
|
|
|
|
|
removeSmallRegion(dst, dst, 100, 1, 1, debug, tempPath);
|
|
|
|
|
// removeSmallRegion(dst, dst, 100, 1, 1, debug, tempPath);
|
|
|
|
|
// 去除孔洞
|
|
|
|
|
removeSmallRegion(dst, dst, 100, 0, 0, debug, tempPath);
|
|
|
|
|
|
|
|
|
|
if (debug) {
|
|
|
|
|
opencv_imgcodecs.imwrite(tempPath + (debugMap.get("morphology") + 100) + "_morphology1.jpg", dst);
|
|
|
|
|
}
|
|
|
|
|
// removeSmallRegion(dst, dst, 100, 0, 0, debug, tempPath);
|
|
|
|
|
|
|
|
|
|
clearHole(dst, 136/10, 36/10, debug, tempPath);
|
|
|
|
|
return dst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -417,6 +407,7 @@ public class ImageUtil {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return min <= area && area <= max && rmin <= r && r <= rmax;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -691,4 +682,79 @@ public class ImageUtil {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 清除二值图像的黑洞
|
|
|
|
|
* 按矩形清理
|
|
|
|
|
* @param inMat 二值图像
|
|
|
|
|
* @param rowLimit 宽度半径限制
|
|
|
|
|
* @param colsLimit 高度半径限制
|
|
|
|
|
* @param debug
|
|
|
|
|
* @param tempPath
|
|
|
|
|
*/
|
|
|
|
|
public static void clearHole(Mat inMat, int rowLimit, int colsLimit, Boolean debug, String tempPath) {
|
|
|
|
|
int uncheck = 0, checking = 1, black = 2, white = 3;
|
|
|
|
|
|
|
|
|
|
Mat dst = new Mat(inMat.size(), opencv_core.CV_8UC1);
|
|
|
|
|
inMat.copyTo(dst);
|
|
|
|
|
rowLimit = 2;
|
|
|
|
|
colsLimit = 2;
|
|
|
|
|
|
|
|
|
|
// 初始化的图像全部为0,未检查; 全黑图像
|
|
|
|
|
Mat label = new Mat(inMat.size(), opencv_core.CV_8UC1);
|
|
|
|
|
|
|
|
|
|
// 标记所有的白色区域
|
|
|
|
|
for (int i = 0; i < inMat.rows(); i++) {
|
|
|
|
|
for (int j = 0; j < inMat.cols(); j++) {
|
|
|
|
|
if (inMat.ptr(i, j).getInt() != 0) { // 对于二值图,0代表黑色,255代表白色
|
|
|
|
|
label.ptr(i, j).putInt(white);
|
|
|
|
|
|
|
|
|
|
int x1 = i - rowLimit < 0 ? 0 : i - rowLimit;
|
|
|
|
|
int x2 = i + rowLimit > inMat.rows() ? inMat.rows() : i + rowLimit;
|
|
|
|
|
int y1 = j - colsLimit < 0 ? 0 : j - colsLimit ;
|
|
|
|
|
int y2 = j + colsLimit > inMat.cols() ? inMat.cols() : j + colsLimit ;
|
|
|
|
|
|
|
|
|
|
/*IntPointer p1 = new IntPointer(x1, y1); // 左上角
|
|
|
|
|
IntPointer p2 = new IntPointer(x1, y2); // 左下角
|
|
|
|
|
IntPointer p3 = new IntPointer(x2, y1); // 右上角
|
|
|
|
|
IntPointer p4 = new IntPointer(x2, y2); // 右下角
|
|
|
|
|
*/
|
|
|
|
|
// System.out.println(x1 + "," + x2 + "\t" + y1 + "," + y2 + "\n");
|
|
|
|
|
// System.out.println(inMat.ptr(x1, y1).getInt() + "," + inMat.ptr(x1, y2).getInt() + "," + inMat.ptr(x2, y1).getInt() + "," + inMat.ptr(x2, y2).getInt());
|
|
|
|
|
|
|
|
|
|
//if(inMat.ptr(x1, y1).getInt() > 10 && inMat.ptr(x1, y2).getInt() > 10 && inMat.ptr(x2, y1).getInt() > 10 && inMat.ptr(x2, y2).getInt() > 10 ) {
|
|
|
|
|
for (int n = x1; n < x2; n++) {
|
|
|
|
|
for (int m = y1; m < y2; m++) {
|
|
|
|
|
//System.err.println(n + "," + m);
|
|
|
|
|
if (/*inMat.ptr(n, m).getInt() < 10 && */label.ptr(n, m).getInt() == uncheck) {
|
|
|
|
|
// System.err.println(n + "," + m);
|
|
|
|
|
label.ptr(n, m).putInt(black);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 1184 //1550
|
|
|
|
|
int count = 0;
|
|
|
|
|
// 替换颜色
|
|
|
|
|
for (int i = 0; i < inMat.rows(); i++) {
|
|
|
|
|
for (int j = 0; j < inMat.cols(); j++) {
|
|
|
|
|
if(label.ptr(i, j).getInt() == black) {
|
|
|
|
|
dst.ptr(i, j).putInt(255);
|
|
|
|
|
count ++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
System.err.println(count);
|
|
|
|
|
|
|
|
|
|
if (debug) {
|
|
|
|
|
opencv_imgcodecs.imwrite(tempPath + (debugMap.get("morphology") + 100) + "_morphology1.jpg", dst);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|