如何检测条带图像上的补丁?
How can I detect patch at strip image?
我正在检测带状图像上的补丁方块
我觉得界限不明确
我怎样才能清楚地得到这个补丁
请帮帮我
这是我的代码和图片
谢谢
代码
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.src2);
// bmp = changeBitmapContrastBrightness(bmp, (float)1.5, 0);
Mat src = new Mat();
Utils.bitmapToMat(bmp, src);
// Creating an empty matrix to store the result
Mat dst = new Mat();
// Creating kernel matrix
Mat kernel = Mat.ones(1,1, CvType.CV_32F);
for(int i = 0; i<kernel.rows(); i++) {
for(int j = 0; j<kernel.cols(); j++) {
double[] m = kernel.get(i, j);
for(int k = 1; k<m.length; k++) {
m[k] = m[k]/(2 * 2);
}
kernel.put(i,j, m);
}
}
Imgproc.filter2D(src, dst, -1, kernel);
Imgproc.cvtColor(dst, dst, Imgproc.COLOR_BGR2GRAY);
// Preparing the kernel matrix object
Mat kernel1 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT,
new Size((2*2) + 1, (2*2)+1));
Imgproc.dilate(dst, dst, kernel1);
Imgproc.threshold(dst, dst, 160, 255, Imgproc.THRESH_BINARY);
// Creating kernel matrix
Mat kernel2 = Mat.ones(5,5, CvType.CV_32F);
Imgproc.morphologyEx(dst, dst, Imgproc.MORPH_OPEN, kernel2);
}
private static List<MatOfPoint> contourFind(Mat img){
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(img, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
List<MatOfPoint> squares = new ArrayList<>();
for(MatOfPoint cnt: contours){
MatOfPoint2f curve = new MatOfPoint2f(cnt.toArray());
MatOfPoint2f approxCurve = new MatOfPoint2f();
Imgproc.approxPolyDP(curve, approxCurve, 0.02 * Imgproc.arcLength(curve, true), true);
int numberVertices = (int) approxCurve.total();
double contourArea = Imgproc.contourArea(cnt);
if (Math.abs(contourArea) < img.size().area() / 10){
squares.add(cnt);
}
}
return squares;
}
原图
enter image description here
处理图像后
enter image description here
您可能想要了解一些分割算法并根据每个类别的样本图像训练您的模型。有一些算法,例如用于经典机器学习的 Watershed 算法。或者如果可以使用深度学习和神经网络的话,看看语义分割。
我正在检测带状图像上的补丁方块
我觉得界限不明确
我怎样才能清楚地得到这个补丁
请帮帮我
这是我的代码和图片
谢谢
代码
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.src2);
// bmp = changeBitmapContrastBrightness(bmp, (float)1.5, 0);
Mat src = new Mat();
Utils.bitmapToMat(bmp, src);
// Creating an empty matrix to store the result
Mat dst = new Mat();
// Creating kernel matrix
Mat kernel = Mat.ones(1,1, CvType.CV_32F);
for(int i = 0; i<kernel.rows(); i++) {
for(int j = 0; j<kernel.cols(); j++) {
double[] m = kernel.get(i, j);
for(int k = 1; k<m.length; k++) {
m[k] = m[k]/(2 * 2);
}
kernel.put(i,j, m);
}
}
Imgproc.filter2D(src, dst, -1, kernel);
Imgproc.cvtColor(dst, dst, Imgproc.COLOR_BGR2GRAY);
// Preparing the kernel matrix object
Mat kernel1 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT,
new Size((2*2) + 1, (2*2)+1));
Imgproc.dilate(dst, dst, kernel1);
Imgproc.threshold(dst, dst, 160, 255, Imgproc.THRESH_BINARY);
// Creating kernel matrix
Mat kernel2 = Mat.ones(5,5, CvType.CV_32F);
Imgproc.morphologyEx(dst, dst, Imgproc.MORPH_OPEN, kernel2);
}
private static List<MatOfPoint> contourFind(Mat img){
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(img, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
List<MatOfPoint> squares = new ArrayList<>();
for(MatOfPoint cnt: contours){
MatOfPoint2f curve = new MatOfPoint2f(cnt.toArray());
MatOfPoint2f approxCurve = new MatOfPoint2f();
Imgproc.approxPolyDP(curve, approxCurve, 0.02 * Imgproc.arcLength(curve, true), true);
int numberVertices = (int) approxCurve.total();
double contourArea = Imgproc.contourArea(cnt);
if (Math.abs(contourArea) < img.size().area() / 10){
squares.add(cnt);
}
}
return squares;
}
原图
enter image description here
处理图像后
enter image description here
您可能想要了解一些分割算法并根据每个类别的样本图像训练您的模型。有一些算法,例如用于经典机器学习的 Watershed 算法。或者如果可以使用深度学习和神经网络的话,看看语义分割。