当前位置:网站首页>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、运行效果 |
|---|

边栏推荐
- 双亲委派机制
- Test dble split function execution + import time-consuming shell script reference
- 育润多维发力慈善领域,勇抗企业公益大旗
- SRM supplier collaborative management system function introduction
- VB.Net读写NFC Ntag标签源码
- Openfeign use step polling strategy and weight log4j configuration of openfeign interceptor
- The soft youth under the blessing of devcloud makes education "smart" in the cloud
- ISO 32000-2 international standard 7.7
- mysql.sock的概念是什么
- 基于STM32F103ZET6库函数独立看门狗(IWDG)实验
猜你喜欢

Automatic vending machine

Top 30 open source software

Issue 42: is it necessary for MySQL to have multiple column partitions

DevCloud加持下的青软,让教育“智”上云端

2022 spring summer collection koreano essential reshapes the vitality of fashion

【WebDriver】使用AutoIt上传文件

Let's start with a bug that was cheated by the app store

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

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?
随机推荐
填充每个节点的下一个右侧节点指针[利用好每个点->尽可能降低时空复杂度]
Top 30 open source software
基于STM32F103ZET6库函数定时器中断实验
使用autoIt 上传文件
What is a SCM system? What are the advantages of a supply chain management system?
Use SSH to pull codes
Visio标注、批注位置
How to create a virtual image
Industry application of smart city based on GIS 3D visualization
Distributed | several steps of rapid read / write separation
基于STM32F103ZET6库函数独立看门狗(IWDG)实验
reflex
How to solve the 2003 error of MySQL in Linux
R语言使用MASS包的glm.nb函数建立负二项广义线性模型(negative binomial)、summary函数获取负二项广义线性模型模型汇总统计信息
迈动互联中标大家保险集团
SRM供应商协同管理系统功能介绍
MySQL触发器如何创建与删除
Redux源码分析之createStore
selenium上传文件
The R language uses the KAP function (kap.2.raters function) of epidisplay package to calculate the value of kappa statistics (total consistency, expected consistency), analyze the consistency of the