当前位置:网站首页>Hegong sky team vision training day4 - traditional vision, contour recognition
Hegong sky team vision training day4 - traditional vision, contour recognition
2022-07-24 07:53:00 【Electric power department of University of Technology】
Catalog
One 、findContours Function learning notes
Two 、 Look for the light bar on the armor plate
3. Screen out the light bar on the armor plate
Learning goals
- Contour recognition
Learning content
- findContours function
- Look for the light bar
- Find the center
Learning time
- 2022 year 7 month 21 Japan
Learning output
One 、findContours Function learning notes
Contour extraction uses findContours function , Its parameters are as follows :
void cv::findContours ( InputArray
image,
OutputArrayOfArrays
contours,
OutputArray
hierarchy,
int mode,
int method,
Point
offset = Point()
)
void cv::findContours ( InputArray
image,
OutputArrayOfArrays
contours,
int mode,
int method,
Point
offset = Point()
)
First is the understanding of function declaration and the analysis of team training materials :
image: The input image .8-bit Single channel binary image , Non zero pixels are treated as 1.
contours: Detected contours . It's a vector , Every element of a vector is an outline . therefore , Every element of this vector is still a vector . namely vector<vector<Point> > contours
hierarchy: The inheritance relationship of each outline .hierarchy It's also a vector , Length and contours equal , Each element and contours The elements of are corresponding to .hierarchy Each element of is a vector of four integers . namely : vector<Vec4i> hierarchy; //Vec4i is a vector contains four number of int
int Type mode, Define the retrieval mode of the contour :
Take one :CV_RETR_EXTERNAL Only the outermost contour is detected , The inner contour contained within the outer contour is ignored ;
Take two :CV_RETR_LIST Detect all contours , Including the inner circumference 、 The outer contour , However, the detected contour does not establish a hierarchical relationship ;
Take three :CV_RETR_CCOMP Detect all contours , But all contours establish only two hierarchical relationships , The periphery is the top floor , If the inner contour in the periphery also contains other contour information , Then all contours in the inner wall belong to the top layer ;
Take four :CV_RETR_TREE Detect all contours , All contours establish a hierarchical tree structure . The outer contour contains the inner contour , The inner contour can also continue to contain embedded contours .
Fifth parameter :int Type method, Approximate method of defining contour :
Take one :CV_CHAIN_APPROX_NONE Save all continuous contour points on the object boundary to contours Within vector
Take two :CV_CHAIN_APPROX_SIMPLE Save only the inflection point information of the contour , Save all points at the inflection point of the contour into contours Within vector , The information points on the straight line between the inflection point and the inflection point will not be retained
among
(1)hierarchy Represents the hierarchical relationship of the contour , For the first i ii Strip outline ,hierarchy[i][0] , hierarchy[i][1] , hierarchy[i][2] , hierarchy[i][3] Respectively represent the next contour 、 The previous outline 、( The first one at the same level ) Sub contour 、 Index of the parent contour ( If there is no corresponding index , It's negative ).
(2)mode parameter “ Contour retrieval mode (Contour retrieval mode)”, Contains RETR_LIST,RETR_EXTERNAL,RETR_CCOMP,RETR_TREE Four patterns .
(3)method Parameters represent contour representation , It is generally used CHAIN_APPROX_SIMPLE.
After learning, I have an understanding of functions , But it has not formed an intuitive impression , Need to learn more .
Two 、 Look for the light bar on the armor plate
1. call findContours Find function outline , The functions used inside are as follows findContours、drawContours、minAreaRect、contourArea It's all in csdn Checked on , I won't elaborate here .
2. Traverse all contours
After traversing all the contours, draw all the recognized contours in the original drawing , To avoid other contour interference , Use minAreaRect Functions and contourArea Function to get a series of information about the smallest circumscribed rectangle of all contours , And through the geometric properties of other minimum circumscribed rectangles , Screen out the light bar on the armor plate .
3. Screen out the light bar on the armor plate
Use minAreaRect Function to get the center coordinate of the smallest circumscribed rectangle 、 Long 、 wide 、 Rotation Angle , Get the area of the smallest circumscribed rectangle, and you can filter out the rectangle of the light bar .
The code is as follows :
import cv2
import numpy as np
img = cv2.imread('/ Picture path /')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
contours,Hierarchy = cv2.findContours(binary,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
print (type(contours))
print (type(contours[0]))
>>> <class 'list'>
<class 'numpy.ndarray'>
for cnt in range(contours[cnt]):
cv2.drawContours(frame, contours[cnt],-1,(255,0,0),3) # Draw the outline
rect = cv2.minAreaRect(cnt)
print(" Central coordinates :", rect[0])
print(" Width :", rect[1][0])
print(" length :", rect[1][1])
print(" Rotation Angle :", rect[2])
area = cv2.contourArea(contours[cnt])
print(" area :", rect[0])
But at present, I only deal with binary images , But there are so many contours of such binary pictures , It's a little difficult for later matching , Because our task is to give a picture , And there are not many influencing factors , The light bar can be identified .
3、 ... and 、 Find the center
It's really difficult for me to write this part by myself , I refer to some codes of Southeast University and my own understanding, but the code still has bug, The idea needs to be improved .
The general idea is to search for the contour after image binarization ( By calculating the area ), Then screen the outline from the length width ratio of the light bar and mark the center , Finally, select it and draw a circle in the center .
PS: The code is partly extracted from the open source project file of Southeast University , So it's just a reflection of my own thoughts , Actually, it should not work , It includes part of identifying contour and finding center point , Compared with my own recognition contour , Dongda's code is many times stronger than mine , I've also watched it for a long time to learn this whole set of ideas , Now we can only search after binarization , And draw a circle , Understanding may be biased , Now I'm immature , Still need follow-up learning to improve .
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
#include<vector>
#include <time.h>
using namespace cv;
using namespace std;
const int ANGLE_TO_UP = 1;
const int WIDTH_GREATER_THAN_HEIGHT = 0;
vector<RotatedRect> vc;
vector<RotatedRect> vRec;
vector<vector<Point>> Light_Contour;
findContours(element.clone(),Light_Contour, RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
for (int i = 0;i < Light_Contour.size();i++)
{
float Light_Contour_Area = contourArea(Light_Contour[i]);
if (Light_Contour_Area < 15 || Light_Contour[i].size() <= 10)
continue;
RotatedRect Light_Rec = fitEllipse(Light_Contour[i]);
Light_Rec = adjustRec(Light_Rec, ANGLE_TO_UP);
if (Light_Rec.angle > 10 )
if (Light_Rec.size.width / Light_Rec.size.height > 1.5
|| Light_Contour_Area / Light_Rec.size.area() < 0.5)
continue;
Light_Rec. size.height *= 1.1;
Light_Rec.size.width *= 1.1;
vc.push_back(Light_Rec);
}
for (size_t i = 0; i < vc.size(); i++)
{
for (size_t j = i + 1; (j < vc.size()); j++)
{
float Contour_angle = abs(vc[i].angle - vc[j].angle); // Angle difference
if (Contour_angle >= 7)
float Contour_Len1 = abs(vc[i].size.height - vc[j].size.height) / max(vc[i].size.height, vc[j].size.height);
float Contour_Len2 = abs(vc[i].size.width - vc[j].size.width) / max(vc[i].size.width, vc[j].size.width);
if (Contour_Len1 > 0.25 || Contour_Len2 > 0.25)
continue;
RotatedRect ARMOR;
ARMOR.center.x = (vc[i].center.x + vc[j].center.x) / 2.;
ARMOR.center.y = (vc[i].center.y + vc[j].center.y) / 2.;
ARMOR.angle = (vc[i].angle + vc[j].angle) / 2.;
float nh, nw, yDiff, xDiff;
nw = sqrt((vc[i].center.x - vc[j].center.x) * (vc[i].center.x - vc[j].center.x) + (vc[i].center.y - vc[j].center.y) * (vc[i].center.y - vc[j].center.y));
float ratio = nw / nh;
xDiff = abs(vc[i].center.x - vc[j].center.x) / nh;
yDiff = abs(vc[i].center.y - vc[j].center.y) / nh;
if (ratio < 1.0 || ratio > 5.0 || xDiff < 0.5 || yDiff > 2.0)
continue;
ARMOR.size.height = nh;
ARMOR.size.width = nw;
vRec.push_back(ARMOR);
Point2f point1;
Point2f point2;
point1.x=vc[i].center.x;point1.y=vc[i].center.y+20;
point2.x=vc[j].center.x;point2.y=vc[j].center.y-20;
int xmidnum = (point1.x+point2.x)/2;
int ymidnum = (point1.y+point2.y)/2;
ARMOR.center.x = filter(ARMOR.center.x,xmidnum, DELAT_MAX);
ARMOR.center.y = filter(ARMOR.center.y,ymidnum, DELAT_MAX);
Scalar color(rand() & 255, rand() & 255, rand() & 255);
rectangle(imgOriginal, point1,point2, color, 2);
circle(imgOriginal,ARMOR.center,10,color);
}
}The learning
I feel that I still have a lot of knowledge to learn , There are many problems in the compilation process bug, such as error C4430: Missing type specifier - Is assumed to be int. Be careful : C++ Default... Is not supported int wait , I don't have a thorough knowledge of many functions , In the third step, we refer to the code of Dongda and find that the whole set is not of the same order of magnitude , Now I'm just a cute new , The road to learning is heavy and long .
边栏推荐
- CentOS 7 install mysql5.6.37
- Error when using PIP: pip is configured with locations that requires tls/ssl
- Install librosa using Tsinghua image
- Solve the problem that Anaconda navigator cannot be opened
- 简易网闸-内网服务器安全获取外网数据
- Digital twin demonstration project -- Talking about simple pendulum (4) IOT exploration
- Math。 Round, numeric rounding, underlying code parsing
- Automatic test and manual test
- Arduino在不同主频下稳定支持的TTL串口速率
- The solution of unable to import custom library in pycharm
猜你喜欢

Perceptron and multilayer neural network, back propagation and computational graph

GBK code in idea is converted to UTF-8 format ctrl+c+v one second solution perfect solution for single code file escape

Li Kou, niuke.com - > linked list related topics (Article 1) (C language)

Requests crawler implements a simple web page collector

C language file operation

Decision tree - ID3, C4.5, cart

About how to set colored fonts on the terminal

rbm 对比散度

Deep analysis of data storage in memory

Selenium basic knowledge automatically login Baidu online disk
随机推荐
HCIP第十天笔记
Selenium use
When does MySQL use table locks and row locks?
Sword finger offer special assault edition day 8
Debug NO2 check for errors according to the process
Generate API documents using swagger2markup
Digital twin demonstration project -- Talking about simple pendulum (2) vision exploration and application scenarios
Robot operation continuous learning thesis (1) original text reading and Translation -- primitive generation strategy learning without catastrophic forgetting in robot operation
Math。 Round, numeric rounding, underlying code parsing
Zhouzhihua machine learning watermelon book chapter 2 model evaluation and selection - accuracy and model generalization evaluation method, self-help method and integrated learning
Thesis reading: geotransformer
Selenium basic knowledge automatic search
Selenium basic knowledge automatically login Baidu online disk
Mitre att & CK ultra detailed learning notes-02 (a large number of cases)
2021-06-03pip error valueerror: unable to find resource t64.exe in package pip_ vendor.distlib
Talk about compilers based on vscode
Perceptron and multilayer neural network, back propagation and computational graph
The growth path of software testing
The difference between session and cookie
Debug No3 multi texture overlay