当前位置:网站首页>3h精通OpenCV(八)-形状检测
3h精通OpenCV(八)-形状检测
2022-06-29 17:46:00 【华为云】
import cv2import numpy as np
我们还要知道在OpenCV中,坐标轴的方向是x轴向右,y轴向下,坐标原点在左上角,比如下面这张长为640像素,宽为480像素的图片。OK,下面开始本节的学习吧。
| 1、实现源码 |
|---|
先将原图像转为灰度图像,再对它进行高斯平滑,去除噪声,得到光滑的背景图像,然后进行边缘检测,再对提取的形状进行判别,分别框起来并且打上标签。
def stackImages(scale,imgArray): rows = len(imgArray) cols = len(imgArray[0]) rowsAvailable = isinstance(imgArray[0], list) width = imgArray[0][0].shape[1] height = imgArray[0][0].shape[0] if rowsAvailable: for x in range ( 0, rows): for y in range(0, cols): if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]: imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale) else: imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale) if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR) imageBlank = np.zeros((height, width, 3), np.uint8) hor = [imageBlank]*rows hor_con = [imageBlank]*rows for x in range(0, rows): hor[x] = np.hstack(imgArray[x]) ver = np.vstack(hor) else: for x in range(0, rows): if imgArray[x].shape[:2] == imgArray[0].shape[:2]: imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale) else: imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale) if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR) hor= np.hstack(imgArray) ver = hor return verdef getContours(img): contours,hierarchy=cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) for cnt in contours: area=cv2.contourArea(cnt) print(area) cv2.drawContours(imgContour,cnt,-1,(255,0,0),3) if area>500: cv2.drawContours(imgContour,cnt,-1,(255,0,0),3) peri=cv2.arcLength(cnt,True) #print(peri) approx=cv2.approxPolyDP(cnt,0.02*peri,True) print(len(approx)) objCor=len(approx) x,y,w,h=cv2.boundingRect(approx) if objCor==3:objectType="Tri" elif objCor==4: aspRatio=w/float(h) if aspRatio>0.98 and aspRatio<1.03: objectType="Square" else:objectType="Rectangle" elif objCor>4: objectType="Circle" else:objectType="None" cv2.rectangle(imgContour,(x,y),(x+w,y+h),(0,255,0),2) cv2.putText(imgContour,objectType,(x+(w//2)-10,y+(h//2)-10),cv2.FONT_HERSHEY_SIMPLEX,0.7, (0,255,255),2)path='Resources/shapes.png'img=cv2.imread(path)img=cv2.resize(img,(400,400))imgContour=img.copy()imgGray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)imgBlur=cv2.GaussianBlur(imgGray,(7,7),1)imgCanny=cv2.Canny(imgBlur,50,50)getContours(imgCanny)imgBlank=np.zeros_like(img)imgStack=stackImages(0.8,([img,imgGray,imgBlur],[imgCanny,imgContour,imgBlank]))cv2.imshow("imgStack",imgStack)cv2.waitKey(0)| 2、运行效果 |
|---|

边栏推荐
- Scenario solution for two modules calling each other in go
- Fill in the next right node pointer of each node [make good use of each point - > reduce the space-time complexity as much as possible]
- Digital twin energy system, creating a "perspective" in the low-carbon era
- Visual Studio插件CodeRush正式发布v22.1——优化调试可视化工具
- Timer interrupt experiment based on stm32f103zet6 library function
- How to solve MySQL 1045 error in Linux
- 小程序容器是什么技术?能助力物联网企业红海突围?
- VB.Net读写NFC Ntag标签源码
- Parental delegation mechanism
- 迈动互联中标大家保险集团
猜你喜欢

Walk with love, educate and run poor families, and promote public welfare undertakings

关于日期相加减问题

基于STM32F103ZET6库函数串口实验

力扣每日一题 06.29 两数相加

What is the MySQL query view command

Two controller layer interface authentication methods

Force deduction daily question 06.29 add two numbers

小程序容器是什么技术?能助力物联网企业红海突围?

Maidong Internet won the bid of Dajia Insurance Group

What technology is an applet container? Can it help Internet of things enterprises break through the red sea?
随机推荐
Bloom filter:
What are the usage scenarios for locks in MySQL
How to solve MySQL 1045 error in Linux
一次采集JSON解析错误的修复
Bags of Binary Words for Fast Place Recognition in Image Sequenc
最受欢迎的30款开源软件
Opencv+YOLO-V3实现目标跟踪
Let's start with a bug that was cheated by the app store
Bottom level internal skill cultivation
Detailed introduction and Simulation of bitmap
2022春夏系列 KOREANO ESSENTIAL重塑时装生命力
What is the MySQL query view command
底层内功修养
MaxCompute字符串替换函数-replace
Multi mode concurrent implementation of tortoise and rabbit race in go language
Web Scraping with Beautiful Soup for Data Scientist
Teach you how to install the latest version of mysql8.0 database on windows, nanny level teaching
基于注解和拦截器防止表单重复提交
跨境独立站语言unicode转希伯来语
ISO 32000-2 international standard 7.7