当前位置:网站首页>Image processing in opencv -- Introduction to contour + contour features
Image processing in opencv -- Introduction to contour + contour features
2022-04-23 09:08:00 【Hua Weiyun】
OpenCV Image processing in —— Introduction to outline + Contour feature
1. OpenCV The outline of
1.1 Outline overview
All changes are inseparable from their religion. They are learning OpenCV Before the outline in , Let's first understand what outline is , The contour can be simply interpreted as connecting all consecutive points of the same color or intensity ( Along the border ) The curve of , Contour is a useful tool for shape analysis and object detection and recognition
- For greater accuracy , We'll use binary images , So before looking for the contour, we apply a threshold or Canny edge detection
- stay OpenCV in , Looking for the outline is to find the white object from the black background , Therefore, the object we are looking for should be white , The background should be black ( After the threshold is applied or Canny edge detection )
Image to find binary image , We need to use a function :cv.findContours(), This function includes three parameters , The first is the original image , The second is the contour retrieval mode , The third is the contour approximation method , This function outputs contours and hierarchies , Outline refers to the outline of all images in the image Python list , Each individual profile is a (x,y) Coordinate Numpy Array of boundary point objects
import numpy as npimport cv2 as cvim = cv.imread('test.jpg')imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)ret, thresh = cv.threshold(imgray, 127, 255, 0)contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)
The value of the second parameter contour retrieval mode can be :
-
RETR_EXTERNAL: Retrieve only the outermost outline
-
RETR_LIST: Retrieve all the contours , And save it to a linked list
-
RETR_CCOMP: Retrieve all the contours , And organize them into two layers : The top layer is the outer boundary of the parts , The second layer is the boundary of the void
-
RETR_TREE: Commonly used , Retrieve all the contours , And reconstruct the entire hierarchy of nested profiles . Save all the contours
The value of the third parameter contour approximation method can be :
-
CHAIN_APPROX_NONE: With Freeman Chain code output outline , All other methods output polygons ( The sequence of vertices ). Draw all contours normally
-
CHAIN_APPROX_SIMPLE: Compressed horizontal 、 Vertical 、 The oblique part , That is, functions retain only their end coordinates . Compression results in more streamlined results , For example, a rectangular outline only needs 4 Points to save profile information
References come from articles :【opencv】(6) Image contour processing
1.2 Outline drawing
adopt cv.findContours() After the function finds the contour , We usually pass cv.drawContours() Function to draw the outline , As long as there are boundary points , It can also be used to draw any shape you want , The first parameter of this function is the image resource , The second parameter should be as Python List passed outline , The third parameter is the index of the contour ( Useful when drawing a single profile , If necessary, draw all contours , Pass in the parameter -1 that will do ), The other parameters are common parameters such as color thickness
import cv2 as cvimport numpy as npfrom matplotlib import pyplot as pltimg = cv.imread('E:/image/test09.png')# When looking for the contour, you need to pass in a single channel image gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)ret, threshold = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)contours, hierarchy = cv.findContours(threshold, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)# Draw all contours img1 = cv.drawContours(img, contours, -1, (0, 255, 0), 3)# Draw a single outline img2 = cv.drawContours(img, contours, 3, (0, 255, 0), 3)# Common methods of drawing contour cnt = contours[3]img3 = cv.drawContours(img, [cnt], 0, (0, 255, 0), 3)plt.subplot(1, 3, 1), plt.imshow(img, cmap='gray')plt.title('Original'), plt.xticks([]), plt.yticks([])plt.subplot(1, 3, 2), plt.imshow(img, cmap='gray')plt.title('ALL_IMG'), plt.xticks([]), plt.yticks([])plt.subplot(1, 3, 3), plt.imshow(img, cmap='gray')plt.title('Usually'), plt.xticks([]), plt.yticks([])plt.show()
1.3 Contour approximation
What we said earlier cv.findContours() The function has three arguments passed , The third parameter is the contour approximation method , That is, the contour approximation method we want to talk about here . A contour is the boundary of a shape with the same intensity , It stores shape boundaries (x,y) coordinate , But it doesn't store all the coordinates
And does it store all the coordinates , It depends on the use of contour approximation method , If we bring in cv.CHAIN_APPROX_NONE Parameters , Then the contour will store all the coordinates , But in reality , Do we need to store all the coordinates ?
For example, we found a rectangular outline , Storing all the coordinates will waste a lot of space , We only store the coordinates of four vertices , This operation is a parameter cv.CHAIN_APPROX_SIMPLE, This contour approximation method can meet our needs
2. Contour feature
2.1 Characteristic moment
Before we learn the characteristic moment, we need to understand its concept , Firstly, the characteristic moment represents a contour 、 Global features of an image , Moment information contains different types of geometric features of corresponding objects , There are three kinds of characteristic moments : Space moment 、 Central moment and normalized central moment
The central moment : For higher-order images , The characteristic moment will change with the change of position , In order to solve this problem, the central moment came into being , It obtains the invariance of translation by subtracting the mean value , Therefore, it can compare whether two objects in different positions are consistent , That is, the central moment has the characteristics of translation invariance
Normalized central moment : In addition to translation , For some images, we will encounter scaling , That is, its characteristics can be judged after zooming , The normalized central moment obtains scaling invariance by dividing the total size of the object
Back to us OpenCV Characteristic moment of , It can help us calculate some characteristics , For example, the center of mass 、 Area etc. , An important function cv.moments() A dictionary of all calculated moment values is provided
import numpy as npimport cv2 as cvimg = cv.imread('star.jpg',0)ret,thresh = cv.threshold(img,127,255,0)contours,hierarchy = cv.findContours(thresh, 1, 2)cnt = contours[0] M = cv.moments(cnt)print( M )
2.2 Contour area + Perimeter
The contour area is determined by the function cv.contourArea() Or from the moment M[‘m00’] Give in
Perimeter is also called arc length , have access to cv.arcLength() Function to find it . The second parameter specifies that the shape is a closed profile ( True ) Or the curve
area = cv.contourArea(cnt)perimeter = cv.arcLength(cnt,True)
2.3 The outline is approximate
Contour approximation is based on our specified intensive reading , Through Douglas - Puke algorithm , Approximate the contour shape to another shape with a small number of vertices
Speaking of Douglas - Puke algorithm , Then we must look at its essence : When digitizing an image , Sample the curve , That is, take a finite number of points on the curve , Turn the connection into a polyline , And keep the original shape to a certain extent
import cv2import numpy as npfrom matplotlib import pyplot as pltsrc = cv2.imread('E:/image/test10.png')gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)contours, hierarchy = cv2.findContours(~thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)cnt = contours[0]img1 = src.copy()epslion1 = 0.05 * cv2.arcLength(cnt, True)approx1 = cv2.approxPolyDP(cnt, epslion1, True)img1 = cv2.drawContours(img1, [approx1], 0, (0, 0, 255), 2)img2 = src.copy()epslion2 = 0.01 * cv2.arcLength(cnt, True)approx2 = cv2.approxPolyDP(cnt, epslion2, True)img2 = cv2.drawContours(img2, [approx2], 0, (0, 255, 0), 2)plt.subplot(1, 2, 1), plt.imshow(img1, cmap='gray')plt.title('JinSi1'), plt.xticks([]), plt.yticks([])plt.subplot(1, 2, 2), plt.imshow(img2, cmap='gray')plt.title('JInSi2'), plt.xticks([]), plt.yticks([])plt.show()
As a review of the previous knowledge , We analyze the whole process of drawing two approximate contours with different accuracy , First, load some third-party libraries we need , It is convenient for us to use its methods in the code, etc , Then in the first 5 Line of code, we pass cv,imread() The function reads an image , And then used cv.cvtColor() Function to convert it into a grayscale image , Why convert to grayscale image ? Because we're going to use image threshold ! In the global threshold function cv.threshold() In the function, we need to pass in four parameters , The first parameter is our source image , It's usually a grayscale image , That's why img Turn into gray, The second parameter is the threshold we set , It is used to classify the pixels in the image , The threshold value of all pixels is set to be greater than the maximum value of , The third parameter is the maximum value we set , The fourth parameter is a flag representing different types
The code has reached the 8 That's ok , From here on, we will cover what we learned in this part , The first is to find the outline , We used cv.findContours() function , The three parameters of this function are image 、 Contour retrieval mode and contour approximation method
here we are 11 That's ok , A strange thing appeared , It is one of the core codes of contour approximation ,epslion Is a parameter closely related to the accuracy of approximation , It represents the maximum distance from the contour to the approximate contour , How to calculate the maximum distance , Then you need a function :cv.arcLength(), Is it familiar , It is the function of the contour perimeter we mentioned above , We need to choose the right epsilon To get the right output
If we put 11 Line code 0.05 Change to 0.1, We will find that we can't get the output we want , This is the parameter epsilon It's too rough , The smaller this parameter is , The more approximate the contour we get
2.4 A convex outline
Contour convex hull and contour approximation look like , In fact, it doesn't matter at all , We also have an important function about contour convex hull cv.convexHull() function , It is used to check whether the curve has convex and concave defects and correct them , And calibration or not , This is the parameter of this function has the final say.
Generally speaking, convex curves are generally convex or flat curves , If it protrudes inside ( It's concave ) We call it convexity defect , Convexity defects will be discussed in detail later
About functions cv.convexHull() Let's spread out the grammar of , It was so long :
hull = cv.convexHull(points[, hull[, clockwise[, returnPoints]]
point It's the outline we pass ( Why use a little (point) Well ? Forget that the essence of the outline is a curve connected by all continuous points with the same color or intensity ),hull( bulge ) It's output , We usually ignore it ,clockwise It's a direction marker , If you pass in True It's clockwise , On the contrary, it is counterclockwise ,returnPoint By default True, It returns the coordinates of the convex hull , If False, Returns the index of the contour point corresponding to the convex hull point
Then give an example !( Continuous practice can grow !)
import cv2import numpy as npfrom matplotlib import pyplot as pltsrc = cv2.imread('E:/image/test11.png')gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)# Look for the outline contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)for cnt in contours: hull = cv2.convexHull(cnt) length = len(hull) # If the number of points in the convex hull point set is greater than 5 if length > 5: # Draw the outline of the convex hull of the image for i in range(length): cv2.line(src, tuple(hull[i][0]), tuple(hull[(i + 1) % length][0]), (0, 0, 255), 2)cv2.imshow('line', src)cv2.waitKey()
Article reference source :OpenCV Getting started is to find the convex hull of the image (convex hull)
2.5 Border rectangle ( Right angle rectangle + Rotate rectangle )
2.5.1 Right angle rectangle
Right angled rectangles do not take into account the rotation of objects , So the area of a rectangular rectangle with a right angle boundary is not the smallest , Finding this rectangle involves a function :cv.boundingRect()
# (x,y) Is the upper-left coordinate ,(w,h) For width and height x,y,w,h = cv.boundingRect(cnt)cv.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
2.5.2 Rotate rectangle
The rotation boundary rectangle is drawn with the minimum area , So it takes into account the rotation of the object , The function involved in finding the rotation boundary rectangle is cv.minAreaRect(), What it returns is a Box2D structure , It contains coordinates 、 Width, height and rotation angle , But it's still troublesome for us to get this rectangle , We need the coordinates of the four vertices
rect = cv.minAreaRect(cnt)box = cv.boxPoints(rect)box = np.int0(box)cv.drawContours(img,[box],0,(0,0,255),2)
2.8 The smallest closed circle + Fit ellipse
2.8.1 The smallest closed circle
Finding the smallest closed circle of an object requires the function :cv.minEnclosingCircle(), It's a circle that completely covers the object with the smallest area
(x,y),radius = cv.minEnclosingCircle(cnt)center = (int(x),int(y))radius = int(radius)cv.circle(img,center,radius,(0,255,0),2)
2.8.2 Fit ellipse
Fitting the ellipse of an object uses the function :cv.fitEllipse()
ellipse = cv.fitEllipse(cnt)cv.ellipse(img,ellipse,(0,255,0),2)
Let's look at the application code :
import cv2import numpy as npfrom matplotlib import pyplot as pltimg = cv2.imread('E:/image/test13.png')gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# Set the threshold ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)cnt = contours[0]x, y, w, h = cv2.boundingRect(cnt)img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)rect = cv2.minAreaRect(cnt)box = cv2.boxPoints(rect)box = np.int0(box)img = cv2.drawContours(img, [box], 0, (0, 0, 255), 2)cv2.imshow('img', img)cv2.waitKey(0)cv2.destroyWindow()
( notes : Article content reference OpenCV4.1 Official documents in Chinese )
If the article helps you , Remember to support three times with one click
版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230904508863.html
边栏推荐
- Emuelec compilation summary
- Notes on xctf questions
- Project upload part
- L2-023 graph coloring problem (25 points) (graph traversal)
- Failed to download esp32 program, prompting timeout
- 考研线性代数常见概念、问题总结
- Arbre de dépendance de l'emballage des ressources
- Pctp test experience sharing
- [SQL Server fast track] view and cursor of database
- MATLAB入门资料
猜你喜欢
The crawler returns null when parsing with XPath. The reason why the crawler cannot get the corresponding element and the solution
Multi view depth estimation by fusing single view depth probability with multi view geometry
What is augmented reality technology? Where can it be used?
npm报错 :operation not permitted, mkdir ‘C: \Program Files \node js \node_ cache _ cacache’
Pctp test experience sharing
LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands
[58] length of the last word [leetcode]
Production practice elk
Flink SQL realizes the integration of stream and batch
653. 两数之和 IV - 输入 BST
随机推荐
单片机数码管秒表
Solidity 问题汇总
Applet error: should have URL attribute when using navigateto, redirectto or switchtab
valgrind和kcachegrind使用運行分析
Illegal character in scheme name at index 0:
Download and install bashdb
501. 二叉搜索树中的众数
共享办公室,提升入驻体验
Play with binary tree (25 points)
Colorui solves the problem of blocking content in bottom navigation
node安装
Node installation
Restore binary tree (25 points)
ONEFLOW learning notes: from functor to opexprinter
Redis Desktop Manager for Mac
Go language self-study series | golang structure as function parameter
Valgrind et kcachegrind utilisent l'analyse d'exécution
Data visualization: use Excel to make radar chart
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
【SQL server速成之路】数据库的视图和游标