0% found this document useful (0 votes)
2 views

visao computacional

This document provides a comprehensive guide on training a boosted cascade of weak classifiers using OpenCV, detailing the necessary stages such as data collection, preparation, and model training. It emphasizes the use of specific OpenCV applications like opencv_createsamples and opencv_traincascade, while also highlighting the importance of positive and negative sample preparation. Additionally, it outlines command line arguments for both sample creation and cascade training, ensuring users can effectively implement object detection models.

Uploaded by

ricardo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

visao computacional

This document provides a comprehensive guide on training a boosted cascade of weak classifiers using OpenCV, detailing the necessary stages such as data collection, preparation, and model training. It emphasizes the use of specific OpenCV applications like opencv_createsamples and opencv_traincascade, while also highlighting the importance of positive and negative sample preparation. Additionally, it outlines command line arguments for both sample creation and cascade training, ensuring users can effectively implement object detection models.

Uploaded by

ricardo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

https://cv.antonello.com.

br/visao-computacional/

Prev Tutorial: Cascade Classifier


Introduction
Working with a boosted cascade of weak classifiers
includes two major stages: the training and the
detection stage. The detection stage using either
HAAR or LBP based models, is described in
the object detection tutorial. This documentation
gives an overview of the functionality needed to train
your own boosted cascade of weak classifiers. The
current guide will walk through all the different
stages: collecting training data, preparation of the
training data and executing the actual model training.
To support this tutorial, several official OpenCV
applications will be
used: opencv_createsamples, opencv_annotation, o
pencv_traincascade and opencv_visualisation.
Note
Createsamples and traincascade are disabled
since OpenCV 4.0. Consider using these apps
for training from 3.4 branch for Cascade
Classifier. Model format is the same between 3.4
and 4.x.
Important notes
 If you come across any tutorial mentioning the
old opencv_haartraining tool (which is
deprecated and still using the OpenCV1.x
interface), then please ignore that tutorial and
stick to the opencv_traincascade tool. This tool
is a newer version, written in C++ in accordance
to the OpenCV 2.x and OpenCV 3.x API. The
opencv_traincascade supports both HAAR like
wavelet features [226] and LBP (Local Binary
Patterns) [133] features. LBP features yield
integer precision in contrast to HAAR features,
yielding floating point precision, so both training
and detection with LBP are several times faster
then with HAAR features. Regarding the LBP
and HAAR detection quality, it mainly depends
on the training data used and the training
parameters selected. It's possible to train a LBP-
based classifier that will provide almost the
same quality as HAAR-based one, within a
percentage of the training time.
 The newer cascade classifier detection interface
from OpenCV 2.x and OpenCV 3.x
(cv::CascadeClassifier) supports working with
both old and new model formats.
opencv_traincascade can even save (export) a
trained cascade in the older format if for some
reason you are stuck using the old interface. At
least training the model could then be done in
the most stable interface.
 The opencv_traincascade application can use
TBB for multi-threading. To use it in multicore
mode OpenCV must be built with TBB support
enabled.
Preparation of the training data
For training a boosted cascade of weak classifiers
we need a set of positive samples (containing actual
objects you want to detect) and a set of negative
images (containing everything you do not want to
detect). The set of negative samples must be
prepared manually, whereas set of positive samples
is created using the opencv_createsamples
application.
Negative Samples
Negative samples are taken from arbitrary images,
not containing objects you want to detect. These
negative images, from which the samples are
generated, should be listed in a special negative
image file containing one image path per line (can be
absolute or relative). Note that negative samples and
sample images are also called background samples
or background images, and are used interchangeably
in this document.
Described images may be of different sizes.
However, each image should be equal or larger than
the desired training window size (which corresponds
to the model dimensions, most of the times being the
average size of your object), because these images
are used to subsample a given negative image into
several image samples having this training window
size.
An example of such a negative description file:
Directory structure:
/img
img1.jpg
img2.jpg
bg.txt
File bg.txt:
img/img1.jpg
img/img2.jpg
Your set of negative window samples will be used to
tell the machine learning step, boosting in this case,
what not to look for, when trying to find your objects
of interest.
Positive Samples
Positive samples are created by the
opencv_createsamples application. They are used by
the boosting process to define what the model should
actually look for when trying to find your objects of
interest. The application supports two ways of
generating a positive sample dataset.
1. You can generate a bunch of positives from a
single positive object image.
2. You can supply all the positives yourself and
only use the tool to cut them out, resize them
and put them in the opencv needed binary
format.
While the first approach works decently for fixed
objects, like very rigid logo's, it tends to fail rather
soon for less rigid objects. In that case we do
suggest to use the second approach. Many tutorials
on the web even state that 100 real object images,
can lead to a better model than 1000 artificially
generated positives, by using the
opencv_createsamples application. If you however
do decide to take the first approach, keep some
things in mind:
 Please note that you need more than a single
positive samples before you give it to the
mentioned application, because it only applies
perspective transformation.
 If you want a robust model, take samples that
cover the wide range of varieties that can occur
within your object class. For example in the case
of faces you should consider different races and
age groups, emotions and perhaps beard styles.
This also applies when using the second
approach.
The first approach takes a single object image with
for example a company logo and creates a large set
of positive samples from the given object image by
randomly rotating the object, changing the image
intensity as well as placing the image on arbitrary
backgrounds. The amount and range of randomness
can be controlled by command line arguments of the
opencv_createsamples application.
Command line arguments:
 -vec <vec_file_name> : Name of the output
file containing the positive samples for training.
 -img <image_file_name> : Source object
image (e.g., a company logo).
 -bg <background_file_name> :
Background description file; contains a list of
images which are used as a background for
randomly distorted versions of the object.
 -num <number_of_samples> : Number of
positive samples to generate.
 -bgcolor <background_color> :
Background color (currently grayscale images
are assumed); the background color denotes the
transparent color. Since there might be
compression artifacts, the amount of color
tolerance can be specified by -bgthresh. All
pixels within bgcolor-bgthresh and
bgcolor+bgthresh range are interpreted as
transparent.
 -bgthresh
<background_color_threshold>
 -inv : If specified, colors will be inverted.
 -randinv : If specified, colors will be inverted
randomly.
 -maxidev <max_intensity_deviation> :
Maximal intensity deviation of pixels in
foreground samples.
 -maxxangle <max_x_rotation_angle> :
Maximal rotation angle towards x-axis, must be
given in radians.
 -maxyangle <max_y_rotation_angle> :
Maximal rotation angle towards y-axis, must be
given in radians.
 -maxzangle <max_z_rotation_angle> :
Maximal rotation angle towards z-axis, must be
given in radians.
 -show : Useful debugging option. If specified,
each sample will be shown. Pressing Esc will
continue the samples creation process without
showing each sample.
 -w <sample_width> : Width (in pixels) of the
output samples.
 -h <sample_height> : Height (in pixels) of
the output samples.
When running opencv_createsamples in this way,
the following procedure is used to create a sample
object instance: The given source image is rotated
randomly around all three axes. The chosen angle is
limited by -maxxangle, -maxyangle and -
maxzangle. Then pixels having the intensity from
the [bg_color-bg_color_threshold;
bg_color+bg_color_threshold] range are interpreted
as transparent. White noise is added to the
intensities of the foreground. If the -inv key is
specified then foreground pixel intensities are
inverted. If -randinv key is specified then algorithm
randomly selects whether inversion should be
applied to this sample. Finally, the obtained image is
placed onto an arbitrary background from the
background description file, resized to the desired
size specified by -w and -h and stored to the vec-
file, specified by the -vec command line option.
Positive samples also may be obtained from a
collection of previously marked up images, which is
the desired way when building robust object models.
This collection is described by a text file similar to the
background description file. Each line of this file
corresponds to an image. The first element of the line
is the filename, followed by the number of object
annotations, followed by numbers describing the
coordinates of the objects bounding rectangles (x, y,
width, height).
An example of description file:
Directory structure:
/img
img1.jpg
img2.jpg
info.dat
File info.dat:
img/img1.jpg 1 140 100 45 45
img/img2.jpg 2 100 200 50 50 50 30 25 25
Image img1.jpg contains single object instance with
the following coordinates of bounding rectangle:
(140, 100, 45, 45). Image img2.jpg contains two
object instances.
In order to create positive samples from such
collection, -info argument should be specified
instead of -img:
 -info <collection_file_name> :
Description file of marked up images collection.
Note that in this case, parameters like -bg, -
bgcolor, -bgthreshold, -inv, -randinv,
-maxxangle, -maxyangle, -maxzangle are
simply ignored and not used anymore. The scheme
of samples creation in this case is as follows. The
object instances are taken from the given images, by
cutting out the supplied bounding boxes from the
original images. Then they are resized to target
samples size (defined by -w and -h) and stored in
output vec-file, defined by the -vec parameter. No
distortion is applied, so the only affecting arguments
are -w, -h, -show and -num.
The manual process of creating the -info file can
also been done by using the opencv_annotation tool.
This is an open source tool for visually selecting the
regions of interest of your object instances in any
given images. The following subsection will discuss
in more detail on how to use this application.
Extra remarks
 opencv_createsamples utility may be used for
examining samples stored in any given positive
samples file. In order to do this only -vec, -
w and -h parameters should be specified.
 Example of vec-file is available
here opencv/data/vec_files/trainingfa
ces_24-24.vec. It can be used to train a face
detector with the following window size: -w 24
-h 24.
Using OpenCV's integrated annotation tool
Since OpenCV 3.x the community has been
supplying and maintaining a open source annotation
tool, used for generating the -info file. The tool can
be accessed by the command opencv_annotation if
the OpenCV applications where build.
Using the tool is quite straightforward. The tool
accepts several required and some optional
parameters:
 --annotations (required) : path to
annotations txt file, where you want to store your
annotations, which is then passed to the -
info parameter [example -
/data/annotations.txt]
 --images (required) : path to folder containing
the images with your objects [example -
/data/testimages/]
 --maxWindowHeight (optional) : if the input
image is larger in height then the given
resolution here, resize the image for easier
annotation, using --resizeFactor.
 --resizeFactor (optional) : factor used to
resize the input image when using the --
maxWindowHeight parameter.
Note that the optional parameters can only be used
together. An example of a command that could be
used can be seen below
opencv_annotation
--annotations=/path/to/annotations/file
.txt --images=/path/to/image/folder/
This command will fire up a window containing the
first image and your mouse cursor which will be used
for annotation. A video on how to use the annotation
tool can be found here. Basically there are several
keystrokes that trigger an action. The left mouse
button is used to select the first corner of your object,
then keeps drawing until you are fine, and stops
when a second left mouse button click is registered.
After each selection you have the following choices:
 Pressing c : confirm the annotation, turning the
annotation green and confirming it is stored
 Pressing d : delete the last annotation from the
list of annotations (easy for removing wrong
annotations)
 Pressing n : continue to the next image
 Pressing ESC : this will exit the annotation
software
Finally you will end up with a usable annotation file
that can be passed to the -info argument of
opencv_createsamples.
Cascade Training
The next step is the actual training of the boosted
cascade of weak classifiers, based on the positive
and negative dataset that was prepared beforehand.
Command line arguments of opencv_traincascade
application grouped by purposes:
 Common arguments:
o -data <cascade_dir_name> : Where

the trained classifier should be stored. This


folder should be created manually
beforehand.
o -vec <vec_file_name> : vec-file with

positive samples (created by


opencv_createsamples utility).
o -bg <background_file_name> :
Background description file. This is the file
containing the negative sample images.
o -numPos
<number_of_positive_samples> :
Number of positive samples used in training
for every classifier stage.
o -numNeg
<number_of_negative_samples> :
Number of negative samples used in
training for every classifier stage.
o -numStages <number_of_stages> :
Number of cascade stages to be trained.
o -precalcValBufSize
<precalculated_vals_buffer_size_
in_Mb> : Size of buffer for precalculated
feature values (in Mb). The more memory
you assign the faster the training process,
however keep in mind that -
precalcValBufSize and -
precalcIdxBufSize combined should
not exceed you available system memory.
o -precalcIdxBufSize
<precalculated_idxs_buffer_size_
in_Mb> : Size of buffer for precalculated
feature indices (in Mb). The more memory
you assign the faster the training process,
however keep in mind that -
precalcValBufSize and -
precalcIdxBufSize combined should
not exceed you available system memory.
o -baseFormatSave : This argument is
actual in case of Haar-like features. If it is
specified, the cascade will be saved in the
old format. This is only available for
backwards compatibility reasons and to
allow users stuck to the old deprecated
interface, to at least train models using the
newer interface.
o -numThreads

<max_number_of_threads> : Maximum
number of threads to use during training.
Notice that the actual number of used
threads may be lower, depending on your
machine and compilation options. By
default, the maximum available threads are
selected if you built OpenCV with TBB
support, which is needed for this
optimization.
o -acceptanceRatioBreakValue

<break_value> : This argument is used to


determine how precise your model should
keep learning and when to stop. A good
guideline is to train not further than 10e-5, to
ensure the model does not overtrain on your
training data. By default this value is set to -
1 to disable this feature.
 Cascade parameters:
o -stageType <BOOST(default)> : Type

of stages. Only boosted classifiers are


supported as a stage type at the moment.
o -featureType<{HAAR(default),

LBP}> : Type of features: HAAR - Haar-like


features, LBP - local binary patterns.
o -w <sampleWidth> : Width of training

samples (in pixels). Must have exactly the


same value as used during training samples
creation (opencv_createsamples utility).
o -h <sampleHeight> : Height of training

samples (in pixels). Must have exactly the


same value as used during training samples
creation (opencv_createsamples utility).
 Boosted classifier parameters:
o -bt <{DAB, RAB, LB,

GAB(default)}> : Type of boosted


classifiers: DAB - Discrete AdaBoost, RAB -
Real AdaBoost, LB - LogitBoost, GAB -
Gentle AdaBoost.
o -minHitRate <min_hit_rate> :

Minimal desired hit rate for each stage of


the classifier. Overall hit rate may be
estimated as (min_hit_rate ^
number_of_stages), [227] §4.1.
o -maxFalseAlarmRate

<max_false_alarm_rate> : Maximal
desired false alarm rate for each stage of
the classifier. Overall false alarm rate may
be estimated as (max_false_alarm_rate ^
number_of_stages), [227] §4.1.
o -weightTrimRate

<weight_trim_rate> : Specifies whether


trimming should be used and its weight. A
decent choice is 0.95.
o -maxDepth

<max_depth_of_weak_tree> : Maximal
depth of a weak tree. A decent choice is 1,
that is case of stumps.
o -maxWeakCount

<max_weak_tree_count> : Maximal
count of weak trees for every cascade
stage. The boosted classifier (stage) will
have so many weak trees
(<=maxWeakCount), as needed to achieve
the given -maxFalseAlarmRate.
 Haar-like feature parameters:
o -mode <BASIC (default) | CORE |

ALL> : Selects the type of Haar features set


used in training. BASIC use only upright
features, while ALL uses the full set of
upright and 45 degree rotated feature set.
See [135] for more details.
 Local Binary Patterns parameters: Local Binary
Patterns don't have parameters.
After the opencv_traincascade application has
finished its work, the trained cascade will be saved
in cascade.xml file in the -data folder. Other files
in this folder are created for the case of interrupted
training, so you may delete them after completion of
training.
Training is finished and you can test your cascade
classifier!
Visualising Cascade Classifiers
From time to time it can be useful to visualise the
trained cascade, to see which features it selected
and how complex its stages are. For this OpenCV
supplies a opencv_visualisation application. This
application has the following commands:
 --image (required) : path to a reference image
for your object model. This should be an
annotation with dimensions [-w,-h] as passed to
both opencv_createsamples and
opencv_traincascade application.
 --model (required) : path to the trained model,
which should be in the folder supplied to the -
data parameter of the opencv_traincascade
application.
 --data (optional) : if a data folder is supplied,
which has to be manually created beforehand,
stage output and a video of the features will be
stored.
An example command can be seen below
opencv_visualisation
--image=/data/object.png
--model=/data/model.xml
--data=/data/result/
Some limitations of the current visualisation tool
 Only handles cascade classifier models, trained
with the opencv_traincascade tool,
containing stumps as decision trees [default
settings].
 The image provided needs to be a sample
window with the original model dimensions,
passed to the --image parameter.
Example of the HAAR/LBP face model ran on a
given window of Angelina Jolie, which had the same
preprocessing as cascade classifier files –>24x24
pixel image, grayscale conversion and histogram
equalisation:
A video is made with for each stage each feature
visualised:

Each stage is stored as an image for future


validation of the features:

This work was created for OpenCV 3 Blueprints by


StevenPuttemans but Packt Publishing agreed
integration into OpenCV.
Aplicação de visão computacional com
OpenCV
13/01/2017
Leandro Passarelli

ÍNDICE DE CONTEÚDO
 Sobre o projeto

 Funcionamento do sistema

 OpenCV – Biblioteca aberta de visão

computacional da Intel
 O projeto

o Funcionamento algoritmo básico:

 Materiais utilizados no teste

 Observações

o Repositório do projeto para

download
o Vídeo
Visão computacional é a visão de máquinas,
é possível obter informações de imagens,
sejam elas astronômicas, microscópicas ou em
tamanho natural, podemos utilizar algoritmos
computacionais para descrever e analisar o
conteúdo de qualquer imagem digitalizada.
Essa prática é cada vez mais comum na
indústria no controle de qualidade de
processos e orientação de robôs, a visão
computacional é capaz de realizar análises
com precisão e velocidades que o olho
humano não poderia alcançar, traz um novo
leque de possibilidades como navegação de
veículos autônomos, descoberta de novos
planetas e análises biológicas em células.
-- publicidade --
Sobre o projeto
Um sistema de visão computacional para
identificar e analisar biscoitos em tempo real,
desenvolvido com software livre e neste caso
voltado para uso educacional.
Figura 1 – Inspeção – Biscoito aprovado
Funcionamento do sistema
O sistema de visão computacional utiliza uma
câmera digital, uma iluminação uniforme tipo
domo e um computador com software para
processar e analisar as imagens. O software
utiliza técnicas de processamento de imagens
que são como os filtros do photoshop, os
filtros têm o objetivo de tratar as imagens,
retirar o fundo (background) e deixar o objeto
com o melhor contraste possível e sem ruídos
que possam atrapalhar a análise geométrica.
Após filtrada a imagem, temos uma imagem
binária, uma imagem preto e branco somente
com a forma do objeto, chamamos essa
imagem binária de máscara e é nela que o
software irá realizar as análises de padrões
geométricos.

No dia 25 de Junho de 2024, ocorrerá o


“Seminário de Sistemas Embarcados e IoT
2024“, no Holiday Inn Anhembi — Parque
Anhembi, São Paulo–SP.
Garanta seu ingresso
Primeiro o software verifica a presença do
biscoito na imagem pela cor/tom com uma
função simples e rápida chamada trigger,
depois se houver a presença detectada do
objeto a imagem passa por filtros é retirada a
máscara e em seguida passa para análise do
padrão geométrico que vai identificar as peças
boas e ruins e ao final o resultado é mostrado
na tela em tempo real.
OpenCV – Biblioteca aberta de visão
computacional da Intel
OpenCV é a principal biblioteca de código
aberto para a visão computacional,
processamento de imagem e aprendizagem
de máquina, e agora apresenta a aceleração
de GPU para operação em tempo real.
OpenCV é liberado sob uma licença de BSD e
daqui é livre para o uso acadêmico e
comercial. Possui interfaces C++, C, Python e
Java e suporta Windows, Linux, Mac OS, iOS e
Android. OpenCV foi projetado para eficiência
computacional e com um forte foco em
aplicações em tempo real. Escrito em C/C++
otimizado, a biblioteca pode aproveitar o
processamento multi-core. Adotado em todo o
mundo, OpenCV tem mais de 47 mil pessoas
da comunidade de usuários e número
estimado de downloads superior a 6 milhões.
O uso varia de arte interativa, a inspeção de
minas, costura mapas na web ou através de
robótica avançada.
Documentação oficial do OpenCV.
Recomendo a leitura da documentação oficial
do OpenCV para aprendizado e consulta!
Considero que você possui conhecimentos em
programação, tem o OpenCV instalado e
testou alguns, ou todos os exemplos dele em
C++ e Python e fez um hello world com
CMAKE.
Esse tutorial é de nível intermediário, apesar
da simplicidade do código, é necessário algum
conhecimento mas qualquer um pode
aprender seguindo a documentação.
O projeto
Vamos apresentar a utilização do OpenCV em
um projeto realtime simples, completo e de
baixo custo.
Esse projeto poderá ser modificado para
muitos outros casos de inspeção visual de
objetos e sua arquitetura é fruto de anos de
pesquisa e desenvolvimento para obter o
melhor desempenho e manter a simplicidade
do código com menor custo possível.
Funcionamento algoritmo básico:
1.Captura
2.Detecção
3.Pre-processamento / filtro
4.Inspeção / análise
5.Resultado

Figura 2 – Funcionamento algoritmo básico

Parametrização, antes de capturar a


imagem é necessário configurar a câmera,
resolução, fps, tempo de exposição e ganho
do sensor manual.
C
1//comando do sistema (video1, tempo de
2exposição manual, ganho do sensor manual)
3
int status = system( "v4l2-ctl -d /dev/video1 -
c gain_automatic=0 -c gain=0 -c
auto_exposure=1 -c exposure=30 -c
power_line_frequency=1" );

1 – Captura – abrir conexão, primeiro


vamos abrir uma conexão com a câmera para
imagem ser capturada.
1.0 – Conexão da câmera, este passo ocorre
fora do loop principal, na inicialização do
sistema:
C
1 int main(int, char**)
2{
3 // video 1, exposure manual, gain manual
4 int status = system( "v4l2-ctl -d /dev/video1
5 -c gain_automatic=0 -c gain=0 -c
auto_exposure=1 -c exposure=30 -c
6 power_line_frequency=1" );
7
8
9 double start=0;
1 double end=0;
0
1 //iniciar captura camera 1
1 capture.open(1);
1
2
pthread_mutex_init(&frameLocker, NULL);
1
3 pthread_t UpdateThread;
1 pthread_create(&UpdateThread, NULL,
4 UpdateFrame, NULL);
1
5 //loop principal
1 for (;;)
6
{(...)}
1
7
1
8

1.1 Captura de frame, esta função é um


loop e roda dentro de uma thread
independente, aqui utilizei um mutex que tem
a função de impedir que a imagem seja
acessada simultaneamente por duas threads,
pois a frame é uma variável tipo global e é
utilizada por duas threads.
C
1 /capture
2 void *UpdateFrame(void *arg)
3{
4 for(;;)
5 {
6 try
7 {
8 Mat tempFrame = Mat::zeros(
9 frame.size(), CV_8UC3 );
1 capture >> tempFrame;
0
1 pthread_mutex_lock(&frameLocker);
1 if (!tempFrame.empty())
1 frame = tempFrame;
2
pthread_mutex_unlock(&frameLocke
1 r);
3
}
1
4 catch(Exception &e)
1 {
5 cout << e.msg << endl;
1 }
6
}
1
}
7
1
8
1
9
2
0
2
1

2 – Detecção, uma função simples e rápida


que verifica se o objeto existe na frame
capturada!
A detecção ou trigger é uma área retangular
no centro da imagem que testa a média da
cor/tom.
Se a comparação for verdadeira a frame
capturada contém o objeto e podemos
inspecioná-la.

Figura 3 – Trigger
C
1 //Trigger
2 float trigger(Mat &tframe)
3{
4 try
5 {
6 float resmean = 0;
7 int x=200,y=200,w=300,h=110;
8 Rect roi = Rect(x,y,w,h);
9 Mat mask0 = Mat::zeros( tframe.size(),
1 CV_8UC1 );
0 rectangle(mask0,roi,255,1);
1 Mat imRoi = frame(roi);
1 Scalar tempMean = mean(imRoi);
1 resmean = tempMean.val[0];
2
if (resmean < 123)
1
3 cout << "\n trigger mean: " <<
resmean << "\n";
1
4 return resmean;
1
5 }
1 catch (Exception &e)
6
{
1
cout << e.msg << endl;
7
return 0;
1
8 }
1}
9
2
0
2
1
2
2
2
3
2
4

3 – Pré-processamento, Esta etapa é muito


importante, é onde filtramos a imagem cinza e
transformamos em uma máscara, ou seja,
uma imagem binária preto e branco, onde o
objeto é branco e o fundo preto.
C
1 void processing(Mat &frame)
2{
3 try
4 {
5 vector<Mat> bgr_planes;
6 split( frame, bgr_planes );
7 //Mat b = bgr_planes[0];
8 //Mat g = bgr_planes[1];
9 Mat r = bgr_planes[2];
1 Mat mask = Mat::zeros( frame.size(),
0 CV_8UC1 );
1 Mat res = Mat::zeros( frame.size(),
1 CV_8UC1 );
1 Mat open = Mat::zeros( frame.size(),
2 CV_8UC1 );
1 Mat close = Mat::zeros( frame.size(),
3 CV_8UC1 );
1 Mat thresh = Mat::zeros( frame.size(),
4 CV_8UC1 );
1 resize( r, mask, Size(), 0.5, 0.5,
5 INTER_LINEAR );
1 threshold(mask,thresh, 120, 255,
6 THRESH_BINARY);
1 //cleanup
7 Mat kernel1 =
1 getStructuringElement(MORPH_ELLIPSE,
8 Size(3, 3));
1 morphologyEx(thresh,open,
9 MORPH_OPEN, kernel1);
2 morphologyEx(open,close,
0 MORPH_CLOSE, kernel1);
2 GaussianBlur(close,res, Size(3, 3), 3);
1 resize( res, res, Size(), 2, 2,
2 INTER_LINEAR );
2
2 inspect(res, frame);
3 }
2 catch (Exception &e)
4
{
2
5 cout << e.msg << endl;
2 }
6
2}
7
2
8
2
9
3
0

3.1 Canal – Escolho entre os canais de cor


RGB o que melhor representa o objeto
Figura 4 – Canal R – canal Red selecionado

3.2 Canal R – Os canais RGB, são imagens


em tons de cinza com profundidade de 8 bits
cada, neste caso escolhemos o RED que
contém a melhor informação dos tons do
biscoito, note que ele é cinza mas representa
o vermelho de 0 á 100% na imagem colorida.
3.3 Threshold – Função que binariza a
imagem, ou seja, transforma uma imagem em
tons de cinza em uma imagem preto e branco,
note que ainda existem alguns ruídos na
imagem.
Figura 5 – Threshold – imagem binária

3.4 Cleanup – Remoção de ruídos (Reduz a


imagem na metade do tamanho, passa filtros
de transformação morfológica de abrir e
fechar e depois amplia a imagem para o
tamanho original).
Após esses passos eliminam se os ruídos e
temos uma imagem mais uniforme. Essa
operação está dentro da função processing.
Figura 6 – Máscara – Imagem filtrada

4- Inspeção / análise, Após o objeto ser


detectado, a frame filtrada e transformada em
máscara é chamada a função de inspeção,
que vai analisar a geometria do objeto.
Essa função analisa a máscara e verifica se o
objeto é convexo, (possui todos os cantos
arredondados) e valida as dimensões do
objeto. Podemos medir a área, perímetro,
altura e largura.
4.1 Contornos, é uma função que transforma
a máscara binária em linhas ou contornos.
C
1//função busca contornos(máscara, resultado,
2hierarquia, modo, método, offset)
findContours( mask.clone(), contours,
hierarchy, RETR_CCOMP,
CHAIN_APPROX_SIMPLE, Point(0, 0) );

4.2 – Aproximação poligonal, transforma as


linhas em um polígono com ajustes de
precisão do comprimento das linhas. Essa
precisão é chamada epsilon.
https://docs.opencv.org/3.1.0/dd/d49/
tutorial_py_contour_features.html
C
1 //Perímetro
2 int cnt_len = arcLength(current_contour, 1);
3 //Fator da precisão epsilon = 0,36% (neste
4 caso)
5 Float epsilon =( 00.0036 * cnt len)
6
7 //aproximação do
poligono(contorno_entrada, contorno_saída,
8 epsilon, flag_recursão )
9 approxPolyDP( Mat(contours[largestComp]),
1 current_contour, epsilon, true );
0
//Validação da análise número de pontos,
área e convexidade
if ( cnt_size > 6 and cnt_area > 60000 and
cnt_area < 80000 and cnt_convex )...

5 – Resultado, Após a análise temos o


resultado e podemos mostrar os contornos na
tela com cores verde e vermelho indicando
passa ou falha e em um sistema completo
acionar uma saída digital de rejeito de
defeitos.
C
1/desenha contornos(imagem, contornos,
2maior contorno, cor, linha, hierarquia)
drawContours( result, contours, largestComp,
color, 1, LINE_8, hierarchy );
Figura 7 – Resultado, biscoito reprovado

Figura 8 – Resultado, biscoito aprovado


Existem diversas maneiras de analisar
imagens, com muitos outros algoritmos como
por exemplo ORB, AKAZE e SURF. Neste caso
nós utilizamos análise geométrica básica, que
é um código simples, leve e eficiente. Na
maioria das aplicações é necessário otimizar o
código ao máximo para atingir alta velocidade
de inspeção com precisão e segurança.
Materiais utilizados no teste
Fonte 12V, Fita Led, Bola de Isopor, pseye, um
suporte de abajur de mesa e uma mesa
giratória com fundo de EVA preto para simular
o movimento da esteira.

Figura 9 – Materiais
Figura 10 – Iluminação domo

Montagem da iluminação domo, feita com


meia bola de isopor e fita led, a lente da
câmera fica no centro do domo, os leds
apontam para dentro do domo, esta
iluminação é difusa, utilizada para evitar
reflexo e obter uma iluminação uniforme.
Funciona bem próximo ao objeto a ser
inspecionado.
Figura 11 – Projeto final
Observações
Em projetos industriais utilizamos
equipamentos industriais de alto desempenho
e precisão, como as câmeras inteligentes da
COGNEX, que possuem processadores
dedicados com software próprio, iluminações
como as da ADVANCED ILLUMINATION e
lentes de alta qualidade, para realizar
inspeções em linhas de alta velocidade com
precisão e repetibilidade, além de conexão
com robôs e outros equipamentos industriais.
É possível utilizar este software com câmeras
industriais, mas é necessário fazer a
integração! Nosso objetivo aqui é fornecer um
ambiente de estudos de visão computacional
de baixo custo com materiais simples e
promover o uso de software livre para fins
educacionais!
Isso é o mais simples que posso fazer, não é
utilizado para fins industriais e sim para
estudos!
Repositório do projeto para download
Vocês podem baixar o exemplo completo
neste repositório.
Vídeo

Existe uma versão em Python e uma em C++


para este projeto.
https://embarcados.com.br/aplicacao-de-visao-
computacional-com-opencv/

https://github.com/llpassarelli/thcv

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy