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

边栏推荐
- Visio标注、批注位置
- Redux源码分析之createStore
- Browser large screen capture
- Visual studio plug-in coderush officially released v22.1 -- visual tool for optimizing debugging
- What is the MySQL query view command
- Mongotemplate - distinct use
- mysql. What is the concept of sock
- mac安装php7.2
- Detailed introduction and Simulation of bitmap
- ISO 32000-2 国际标准7.7
猜你喜欢

Analyze the implementation principle of zero copy mechanism, applicable scenarios and code implementation
![分割回文串[dp + dfs组合]](/img/7b/221b000984977508f849e19802c2c2.png)
分割回文串[dp + dfs组合]

剑桥大学教授:经常吃早餐害处多,很危险 - 知乎

MySQL触发器如何创建与删除

How to use the chart control of the b/s development tool devextreme - customize the axis position?

Prevent form resubmission based on annotations and interceptors

What are the usage scenarios for locks in MySQL

第42期:MySQL 是否有必要多列分区

Does MySQL support foreign keys

OpenFeign使用步骤 轮询策略与权重 log4j使用 openFeign拦截器的配置
随机推荐
布隆过滤器:
Selenium file upload method
phpunit骚操作之静态类的部分mock
Have you grasped the most frequently asked question in the interview about massive data processing?
Issue 42: is it necessary for MySQL to have multiple column partitions
SSH协议学习笔记
R language uses user-defined functions to write deep learning linear activation functions and visualize linear activation functions
与爱同行,育润走进贫困家庭,助推公益事业
Uploading files using AutoIT
R language ggplot2 visualization: use the patchwork package (directly use the plus sign +) to horizontally combine the two ggplot2 visualization results, and then horizontally combine them with the th
What are the usage scenarios for locks in MySQL
两种Controller层接口鉴权方式
PCB frame drawing - ad19
面试中问最常问的海量数据处理你拿捏了没?
Bags of Binary Words for Fast Place Recognition in Image Sequenc
Visual studio plug-in coderush officially released v22.1 -- visual tool for optimizing debugging
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]
Segment tree and tree array template (copy and paste are really easy to use)
Web Scraping with Beautiful Soup for Data Scientist
基于STM32F103ZET6库函数定时器中断实验