当前位置:网站首页>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 .
边栏推荐
- Default risk early warning preliminary competition scheme of bond issuing enterprises [AI competition]
- Li Kou, niuke.com - > linked list related topics (Article 1) (C language)
- DevOps随笔
- Movie recommendation system
- Opencv project practice - credit card recognition
- Kubernetes: (I) basic concepts
- 2021-06-03pip error valueerror: unable to find resource t64.exe in package pip_ vendor.distlib
- Using bidirectional linked list to realize stack (c)
- MySQL --- 子查询 - 标量子查询
- Deep analysis of data storage in memory
猜你喜欢

Starting from scratch C language intensive Part 3: Functions

Implement a queue with two stacks.

Kubernetes:(一)基本概念

Game three piece chess

2021-06-03pip error valueerror: unable to find resource t64.exe in package pip_ vendor.distlib

Selenium basic knowledge automatic search
![[cloud native] MySQL index analysis and query optimization](/img/ca/79783721637641cb8225bc26a8c4a9.png)
[cloud native] MySQL index analysis and query optimization

XSS vulnerability learning

【Pytorch】conv2d torchvision.transforms

MySQL 啥时候用表锁,啥时候用行锁?
随机推荐
Simple Gateway - intranet server safely obtains external network data
The growth path of software testing
HCIP第十天笔记
Automatic test and manual test
MySQL update uses case when to update the value of another field according to the value of one field
MySQL -- subquery scalar subquery
A simple mobile terminal todo
C language advanced part VII. Program compilation and preprocessing
OpenGL camera and periodic review
Image feature SIFT (scale invariant feature transform)
About how to set colored fonts on the terminal
About the solution of thinking that you download torch as a GPU version, but the result is really a CPU version
Project practice - document scanning OCR recognition
Thesis reading: geotransformer
Introduction to C language II. Functions
Hcip day 10 notes
MS SQL Server 2019 learning
Default risk early warning preliminary competition scheme of bond issuing enterprises [AI competition]
C language to achieve mine sweeping game
Case practice - panoramic image mosaic: feature matching method