Cv2.Sobel(binaryMat, sobelMat, MatType.CV_8U, 1, 1);sobelMat 保存的坐标吗
时间: 2024-04-15 19:26:56 浏览: 54
在你提供的代码中,Cv2.Sobel函数是OpenCV库中的一个函数,用于在图像上应用Sobel算子进行边缘检测。它的参数包括输入图像、输出结果、输出图像的数据类型、X方向的阶数、Y方向的阶数等。
在这种情况下,sobelMat是一个输出参数,用于保存边缘检测结果。它是一个与输入图像大小相同的矩阵,每个元素表示该位置的像素强度梯度。sobelMat并不保存坐标信息,而是存储了图像中每个像素点的梯度值。这个矩阵可以用于后续的图像处理和分析任务,比如边缘检测、特征提取等。
相关问题
public void FindEdgesOUTCir(Mat srcImage, double edgeThreshold, ref List<Point2f> zeroPList) { // 定义图像边界矩形 Rect boundary = new Rect(0, 0, srcImage.Width, srcImage.Height); // 计算图像中心处的y坐标 float cenY = (boundary.Y + boundary.Height) / 2; // 对输入图像进行竖直方向的投影 Mat projectMat = VerticalLineGrayParallel(srcImage); // 应用Sobel算子,得到x方向上的导数 Mat sobelMat = new Mat(); Cv2.Sobel(projectMat, sobelMat, MatType.CV_32FC1, 1, 0, 1, 1, 0); // 设置阈值 float thresholdFloat = (float)edgeThreshold; int width = sobelMat.Width; var points = new ConcurrentBag<Point2f>(); // thread-safe collection to hold edge points Parallel.For(1, width, i => { float sdx1 = sobelMat.At<float>(0, i - 1); float sdx2 = sobelMat.At<float>(0, i); if (sdx1 < thresholdFloat && sdx2 >= thresholdFloat) { float a = (sdx2 - sdx1) / 1f; float b = sdx1 - a * (i - 1); float x = (thresholdFloat - b) / a; Point2f point = new Point2f(x, cenY); points.Add(point); } }); zeroPList = points.ToList(); // convert thread-safe collection to list } 提升鲁棒性请优化,进一步优化,以适应边沿对比度比较低的图像
针对边沿对比度比较低的情况,可以考虑以下优化:
1. 调整阈值:可以根据实际情况调整阈值,使其适应较低的对比度。
2. 应用高斯滤波:可以在进行Sobel算子处理之前,应用高斯滤波对图像进行平滑处理,以减少噪声的干扰和提升边缘检测的准确性。
3. 应用Canny算子:Canny算子是一种常用的边缘检测算法,可以检测出较弱的边缘,并且可以自适应调整阈值。可以在投影处理后,应用Canny算子进行边缘检测。
4. 使用多尺度边缘检测:可以使用多个尺度对图像进行边缘检测,以检测出不同尺度下的边缘信息,并且可以提高对低对比度边缘的检测能力。
5. 学习使用深度学习模型:可以使用深度学习模型进行边缘检测,例如使用卷积神经网络模型进行图像分割和边缘检测。这种方法需要大量的数据和计算资源,但可以获得更好的检测效果。
def Process(img): # 高斯平滑 gaussian = cv2.GaussianBlur(img, (3, 3), 0, 0, cv2.BORDER_DEFAULT)#高斯模糊函数 median = cv2.medianBlur(gaussian, 5)#中值滤波 sobel = cv2.Sobel(median, cv2.CV_8U, 1, 0, ksize=3)#Sobel算子,梯度方向是X # 二值化 ret, binary = cv2.threshold(sobel,200, 255, cv2.THRESH_BINARY)#cv2简单阙值函数 # 核函数 element1 = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 1))#得到一个结构元素(卷积核)。主要用于后续的腐蚀、膨胀等运算。 element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 7)) dilation = cv2.dilate(binary, element2, iterations=1)#膨胀函数 # 腐蚀 erosion = cv2.erode(dilation, element1, iterations=1) # 膨胀 dilation2 = cv2.dilate(erosion, element2, iterations=3) return dilation2 def GetRegion(img): regions = [] # 查找轮廓 contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)#检测图像中物体轮廓 for contour in contours: area = cv2.contourArea(contour)#计算轮廓面积 if (area<2000): continue eps = 0.001* cv2.arcLength(contour, True)#计算封闭轮廓或者曲线的长度 approx = cv2.approxPolyDP(contour, eps, True)#轮廓多边形逼近 rect = cv2.minAreaRect(contour)#求最小面积矩形框 box = cv2.boxPoints(rect)#获取最小面积矩形框的四个顶点坐标 box = np.int0(box)#整型化 height = abs(box[0][1] - box[2][1]) width = abs(box[0][0] - box[2][0]) ratio =float(width) / float(height) if (ratio < 5 and ratio > 1.8): regions.append(box) return regions def detect(img): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)#图片灰度化 prc = Process(gray) regions = GetRegion(prc) print('[INFO]:Detect %d license plates' % len(regions)) for box in regions: cv2.drawContours(img, [box], 0, (0, 0,255), 2) cv2.imwrite(r'C:\Users\86182\Pictures\Saved Pictures\test.png', img) cv2.waitKey(0) cv2.destroyAllWindows()
这是一段基于OpenCV库的车牌检测代码。具体来说,它通过对车牌图像进行高斯平滑、中值滤波、Sobel算子、二值化、膨胀、腐蚀等操作,得到一幅处理后的图像。然后,它通过查找图像中的轮廓,计算轮廓面积、多边形逼近、最小面积矩形框等方式,识别出车牌并用红色框标记出来。该代码还可以输出识别到的车牌数量,并将标记后的图像保存在本地。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)