使用openCV进行边缘检测、二值化、轮廓、轮廓检测、BGR、灰度图、二值化,专栏:各种openCV实践的案例

专栏连接:openCV练习-各种openCV实践的案例


前言

使用OpenCV的轮廓检测,当我们加入对象的边界上的所有点时,我们得到一个轮廓。 通常,特定轮廓区域与边界像素有关,具有相似的颜色和强度。 每当强度或颜色变化很大,那么几乎总是从那里开始一个新的轮廓区域。

环境:ubuntu、anaconda、python、vscode


基本步骤

1.读取图像并将其转换为灰度格式

import cv2
image = cv2.imread('/data/file/img/image_1.jpg')
img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

2.应用二进制阈值

ret, thresh = cv2.threshold(img_gray, 150, 255, cv2.THRESH_BINARY)

这里的ret是阈值,小于这个阈值的像素点的值会被改变为0,大于这个阈值的像素点的值会被改变为255。(我这里的ret的值是150)

thresh是应用阈值处理之后的二值化图像。

让我们来看一下二值化后的图像

在这里插入图片描述

可以看出边缘是很清晰的。

3.使用chain_approx_none绘制轮廓

contours, hierarchy = cv2.findContours(image=thresh, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)

image_copy = image.copy()
cv2.drawContours(image=image_copy, contours=contours, contourIdx=-1, color=(0, 255, 0), thickness=2, lineType=cv2.LINE_AA)

cv2.imshow('None approximation', image_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()

注意,直接点击×掉显示的窗口不会使得程序停止,除非你再执行一个

cv2.destroyAllWindows()

正确关闭窗口的操作是点击图像后,按任意的键即可关闭图像。

在这里插入图片描述

这里在二值化后的图像找到边缘,然后将边缘画在原图上,就形成了上面的图片。

以上就是边缘检测的基本操作,下面的案例几乎都是和上面的操作类似。

4.使用单个通道:红色,绿色或蓝色

# 使用上面已经读取了的图像
# 将图像的B,G,R通道分开
blue, green, red = cv2.split(image)
contours1, hierarchy1 = cv2.findContours(image=blue, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)
image_contours_blue = image.copy()
cv2.drawContours(image=image_contours_blue, contours=contours1, contourIdx=-1, 
                                color=(0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
cv2.imshow('Contours detection using blue channels only', image_contours_blue)
cv2.waitKey(0)
cv2.destroyAllWindows()

# detect contours using green channel and without thresholding
contours2, hierarchy2 = cv2.findContours(image=green, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)
# draw contours on the original image
image_contour_green = image.copy()
cv2.drawContours(image=image_contour_green, contours=contours2, contourIdx=-1, color=(0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
# see the results
cv2.imshow('Contour detection using green channels only', image_contour_green)
cv2.waitKey(0)
cv2.destroyAllWindows()

# detect contours using red channel and without thresholding
contours3, hierarchy3 = cv2.findContours(image=red, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)
# draw contours on the original image
image_contour_red = image.copy()
cv2.drawContours(image=image_contour_red, contours=contours3, contourIdx=-1, color=(0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
# see the results
cv2.imshow('Contour detection using red channels only', image_contour_red)
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

可以看出使用单通道进行边缘检测都不是很准确。


总结

这个实验我们使用了openCV的轮廓和轮廓检测。

参考连接:https://learnopencv.com/contour-detection-using-opencv-python-c/#What-are-Contours

  • 8
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
使用C++语言实现工业相机的灰度、二边缘检测、膨胀、腐蚀等操作,可以使用OpenCV像处理库。以下是实现这些操作的基本步骤: 1. 获取原始像:使用OpenCV提供的函数或工业相机SDK获取原始像。 2. 灰度使用OpenCV提供的函数将原始像转换为灰度像,例如: ```c++ cv::Mat gray_image; cv::cvtColor(original_image, gray_image, cv::COLOR_BGR2GRAY); ``` 3. 二使用OpenCV提供的函数将灰度像转换为二像,例如: ```c++ cv::Mat binary_image; cv::threshold(gray_image, binary_image, 128, 255, cv::THRESH_BINARY); ``` 其中,128为阈,255为二后的像素。 4. 边缘检测使用OpenCV提供的函数对二进行边缘检测,例如: ```c++ cv::Mat edges; cv::Canny(binary_image, edges, 100, 200); ``` 其中,100和200为边缘检测的阈。 5. 膨胀和腐蚀:使用OpenCV提供的函数对二进行膨胀和腐蚀操作,例如: ```c++ cv::Mat dilated_image; cv::Mat eroded_image; cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3)); cv::dilate(binary_image, dilated_image, element); cv::erode(binary_image, eroded_image, element); ``` 其中,element为膨胀和腐蚀的结构元素,可以根据需要选择不同的形状和大小。 最后,可以使用OpenCV提供的函数显示处理后的像,例如: ```c++ cv::imshow("Processed Image", processed_image); cv::waitKey(0); ``` 其中,processed_image为处理后的像,可以是灰度像、二像、边缘像、膨胀像或腐蚀像。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dlage

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值