gpt4 book ai didi

java - OpenCV检测钻孔

转载 作者:搜寻专家 更新时间:2023-11-01 08:20:49 26 4
gpt4 key购买 nike

我正在做一个项目,我必须检测表面上的钻孔。 (顶部的两个孔仅供定位)

Image: surface to scan sample 1 Image: surface to scan sample 2

检测到孔后,图案将判断孔的位置并给出结果。我已经创建了一个覆盖网格布局并将其放置在 camera2api 预览上,以便用户可以对齐孔并进行扫描(真正的测试不会像屏幕截图中显示的那样来自 LCD 的图片)

enter image description here

目前,我正在根据网格裁剪图像并将其调整为 1920x2560 以具有一致的模式判断框架,这使得单个网格大约为 300px。我无法检测到 Blob 有人可以建议我应该为这项工作选择什么样的过滤,如果有更好的方法来做这个而不是使用网格布局作为孔的位置关于方向孔很重要最终结果(x 和 y 轴)

enter image description here

这是我的代码:

Mat srcMat = resizeAndCropMatToGrid(mats[0]);
if (srcMat == null) {
exception = new Exception("Cropping Failed");
errorMessage = "Unable to crop image based on grid";
return null;
}
matProgressTask = srcMat;
Mat processedMat = new Mat();
Imgproc.cvtColor(srcMat, processedMat, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(processedMat, processedMat, new org.opencv.core.Size(5, 5), 5);
Imgproc.threshold(processedMat, processedMat, 115, 255, Imgproc.THRESH_BINARY);


matProgressTask = processedMat;


FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.SIMPLEBLOB);
featureDetector.read(Environment.getExternalStorageDirectory() + "/Android/blob.xml");
MatOfKeyPoint matOfKeyPoint = new MatOfKeyPoint();
featureDetector.detect(processedMat, matOfKeyPoint);
KeyPoint[] keyPointsArray = matOfKeyPoint.toArray();
Log.e("keypoints", "" + Arrays.toString(keyPointsArray));
if (keyPointsArray.length < 1) {
exception = new Exception("Blobs Missing");
errorMessage = "Error: Unable to filter blobs";
} else {
try {
MatOfKeyPoint matOfKeyPointFilteredBlobs = new MatOfKeyPoint(keyPointsArray);
Features2d.drawKeypoints(srcMat, matOfKeyPointFilteredBlobs, srcMat, new Scalar(255, 0, 0), Features2d.DRAW_OVER_OUTIMG);
} catch (Exception e) {
e.printStackTrace();
exception = e;
errorMessage = "Error: Unable to draw Blobs";
return null;
}
matProgressTask = srcMat;
onProgressUpdate();
patterData = pinpointBlobsToGetData(keyPointsArray);
if (patterData == null) {
exception = new Exception("Unable to establish pattern");
errorMessage = "Error: Key points array is null";
}
}

这是我正在使用的 blobby 文件配置:

<?xml version="1.0"?>
<opencv_storage>
<format>3</format>
<thresholdStep>10.</thresholdStep>
<minThreshold>50.</minThreshold>
<maxThreshold>120.</maxThreshold>
<minRepeatability>2</minRepeatability>
<minDistBetweenBlobs>20.</minDistBetweenBlobs>
<filterByColor>1</filterByColor>
<blobColor>0</blobColor>
<filterByArea>1</filterByArea>
<minArea>2300.</minArea>
<maxArea>4500.</maxArea>
<filterByCircularity>1</filterByCircularity>
<minCircularity>0.2</minCircularity>
<maxCircularity>1.0</maxCircularity>
<filterByInertia>1</filterByInertia>
<minInertiaRatio>0.2</minInertiaRatio>
<maxInertiaRatio>1.0</maxInertiaRatio>
<filterByConvexity>1</filterByConvexity>
<minConvexity>0.2</minConvexity>
<maxConvexity>1.0</maxConvexity>
</opencv_storage>

最佳答案

我正在使用 Python。

对于您提供的第二张图片,我成功检测到了漏洞...

res1

...使用此代码...

import cv2
import numpy as np

img = cv2.imread("C:\\Users\\Link\\Desktop\\2.jpg")
# cv2.imshow("original", img)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# cv2.imshow("gray", gray)

blur = cv2.medianBlur(gray, 31)
# cv2.imshow("blur", blur)

ret, thresh = cv2.threshold(blur, 127, 255, cv2.THRESH_OTSU)
# cv2.imshow("thresh", thresh)

canny = cv2.Canny(thresh, 75, 200)
# cv2.imshow('canny', canny)

im2, contours, hierarchy = cv2.findContours(canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

contour_list = []
for contour in contours:
approx = cv2.approxPolyDP(contour, 0.01 * cv2.arcLength(contour, True), True)
area = cv2.contourArea(contour)
if 5000 < area < 15000:
contour_list.append(contour)

msg = "Total holes: {}".format(len(approx)//2)
cv2.putText(img, msg, (20, 40), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2, cv2.LINE_AA)

cv2.drawContours(img, contour_list, -1, (0, 255, 0), 2)
cv2.imshow('Objects Detected', img)

cv2.imwrite("detected_holes.png", img)

cv2.waitKey(0)

现在,第一个有点不同。相同的代码将无法检测适量的孔。该程序还不断检测明显不是孔的地方(左下角的裂纹..),缺少一些主要孔。

这是我正在谈论的例子:

res2

不仅这种情况下的计数器是错误的,而且主要问题是无法检测到右下角的孔。

关于java - OpenCV检测钻孔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51456660/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com