当前位置:网站首页>3H proficient in opencv (VIII) - shape detection
3H proficient in opencv (VIII) - shape detection
2022-06-29 17:57:00 【Hua Weiyun】
import cv2import numpy as np
We also need to know where OpenCV in , The direction of the coordinate axis is x Shaft to the right ,y Axis down , The coordinate origin is in the upper left corner , For example, the following one is 640 Pixels , Wide for 480 Pixel image .OK, Let's begin with this section .
| 1、 Implementation source code |
|---|
First, convert the original image to a grayscale image , Gaussian smoothing is applied to it , Noise removal , Get a smooth background image , And then edge detection , Then the extracted shape is judged , Frame them separately and label them .
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、 Running effect |
|---|

边栏推荐
- js两个二维数组合并并去除相同项(整理)
- ABC253 D FizzBuzz Sum Hard(容斥定理)
- js两个一维数组合并并去除相同项(整理)
- Industry application of smart city based on GIS 3D visualization
- What is a SCM system? What are the advantages of a supply chain management system?
- MaxCompute Studio
- YoloV6+TensorRT+ONNX:基于WIN10+TensorRT8+YoloV6+ONNX的部署
- QQ如何开通在线客服
- 分布式 | 几步快速拥有读写分离
- JS merge two 2D arrays and remove the same items (collation)
猜你喜欢

力扣每日一题 06.29 两数相加

基于STM32F103ZET6库函数串口实验

与爱同行,育润走进贫困家庭,助推公益事业

Yurun multidimensional makes efforts in the charity field and bravely resists the corporate public welfare banner

Abc253 D fizzbuzz sum hard (tolerance exclusion theorem)

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

Matlab farthest point sampling (FPS)

ABC253 D FizzBuzz Sum Hard(容斥定理)

Distributed | several steps of rapid read / write separation

Maidong Internet won the bid of Dajia Insurance Group
随机推荐
Have you grasped the most frequently asked question in the interview about massive data processing?
阿里云不同账号新旧服务器镜像迁移数据迁移同步
Inherit Chinese virtues, pay attention to the health of the middle-aged and the elderly, and Yurun milk powder has strong respect for the elderly
Xiaobai yuesai 51 supplement e g f
Matlab farthest point sampling (FPS)
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]
Opencv+yolo-v3 for target tracking
DevCloud加持下的青软,让教育“智”上云端
最受欢迎的30款开源软件
[Oracle] basic knowledge interview questions
selenium 文件上传方法
SRM supplier collaborative management system function introduction
Selenium key combination operation
让 Google 搜索到自己的博客
牛客小Bai月赛52 D 环上食虫(尺取+st表)
Mysql database literacy, do you really know what a database is
分割回文串[dp + dfs组合]
Niuke small Bai monthly race 52 D ring insectivorous (feet +st table)
Spingmvc requests and responses
Visual studio plug-in coderush officially released v22.1 -- visual tool for optimizing debugging