opencv实现靶纸弹孔识别计数功能
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv

·
opencv实现靶纸弹孔识别计数功能
任务是对胸环靶进行弹孔识别与计数。靶纸图片是从网上找的。
具体过程主要是先对图片进行灰度化处理,接着进行阈值分割,将弹孔与背景分割开。再腐蚀并膨胀(形态学开运算),取得较好的分割效果。最后通过轮廓提取,对弹孔进行识别与计数。
1. 靶纸图片
靶纸如下图所示:
2. 灰度化处理
通过opencv的cv2.cvtColor()函数对图像进行灰度化。
函数cv2.cvtColor(input_image ,flag),flag是转换类型:
BGR和灰度图的转换使用 cv2.COLOR_BGR2GRAY
# src.jpg是图像的名称
img = cv2.imread('src.jpg')
# 灰度化函数,结果为GrayImage
GrayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('GrayImage', GrayImage)
cv2.waitKey(0)
灰度化结果如图所示:
3. 二值化处理
通过opencv的cv2.threshold()函数对得到的灰度图进行阈值分割,二值化处理。
函数参数参考参数描述
#
ret, thresh = cv2.threshold(GrayImage, 70,255,cv2.THRESH_BINARY_INV)
二值化结果图如图所示:
可以看出,阈值化处理的结果还有一些背景没有去除。
3. 形态学处理(开运算)
# 开运算处理,结果图为opening
kernel = np.ones((3, 3), np.uint8)
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
开运算结果如图所示:
4. 轮廓提取,画轮廓
通过opencv的cv2.findContours()函数对开运算处理后的二值化图像进行轮廓提取,cv2.rectangle()函数。
# 轮廓提取函数
contours, hierarchy = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for i in range(0,len(contours)):
x, y, w, h = cv2.boundingRect(contours[i])
cv2.rectangle(img, (x,y), (x+w,y+h), (0,0,255), 2)
结果如图所示:
5. 弹孔计数
num = len(contours)
print("弹孔数量", num)
输出结果为3
6. 结果分析
从图中的结果可以看出,原靶纸图片应该为4个弹孔,但实际上只识别到3个弹孔。
分析原因:主要是在二值化步骤中,10环中心区域是白色的背景,导致10环处的弹孔较亮,大于我们阈值处理选择的灰度值70,从而被当成背景而未被识别。
处理:在二值化之前,应该先对10环内的区域进行处理,分割出内部的弹孔或使其灰度值降低。
7. 未完待续…
之后有时间再进行改进吧…




OpenCV: 开源计算机视觉库
最近提交(Master分支:29 天前 )
b6447542
Fix invalid memory access in USAC #27865
### Pull Request Readiness Checklist
Fix #27863
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
22 小时前
19c41c29
Improved blur #27822
* Perform row and column filter operations in a single pass.
* Temporary storage of intermediate results are avoided.
* Impacts 32F and 64F inputs for ksize <=5.
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
1 天前
更多推荐
所有评论(0)