Learning opencv step by step

Re: Learning opencv step by step

Postby Antonio Linares » Fri Dec 24, 2021 8:54 am

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42120
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Learning opencv step by step

Postby Antonio Linares » Fri Dec 24, 2021 10:03 am

Let the magic begin... :-)

Lets start recognizing "eyes" (also "faces") and later on we will recognize anything (training our own "Haar Cascades").
We select and use a CascadeClassifier, for eyes "haarcascade_eye.xml"

There are a few "false" positives that we can fine tune with these values:
cv::Size( 30, 60 ) where 30 is the smallest size allowed ad 60 is the largest value allowed

cv1.prg modified to recognize "eyes"
Code: Select all  Expand view  RUN
#include "FiveWin.ch"

#define WINDOW_AUTOSIZE 1

function Main()

   local hMat := cv_ImRead( "007.jpg" )

   cv_namedWindow( "window title", WINDOW_AUTOSIZE )
   cv_ImShow( "window title", hMat )
   cv_WaitKey()

return nil

#pragma BEGINDUMP

#include <hbapi.h>
#include <opencv.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>

static cv::Mat mat1;

HB_FUNC( CV_IMREAD )
{
   cv::CascadeClassifier face_cascade;
   std::vector<cv::Rect> faces;

   face_cascade.load( "c:/opencv/sources/data/haarcascades/haarcascade_eye.xml" );

   mat1 = cv::imread( hb_parc( 1 ) );

   face_cascade.detectMultiScale( mat1, faces, 1.1, 3, 0, cv::Size( 30, 60 ) );  

   for( size_t i = 0; i < faces.size(); i++ )
      cv::rectangle( mat1, faces[ i ], cv::Scalar( 255, 255, 255 ), 1, 1, 0 );

   hb_retptr( &mat1 );
}

HB_FUNC( CV_NAMEDWINDOW )
{
   cv::namedWindow( hb_parc( 1 ), hb_parnl( 2 ) );
}

HB_FUNC( CV_IMSHOW )
{
   cv::imshow( hb_parc( 1 ), * ( ( cv::Mat * ) hb_parptr( 2 ) ) );
}

HB_FUNC( CV_WAITKEY )
{
   cv::waitKey( hb_parnl( 1 ) );
}

#pragma ENDDUMP


Image
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42120
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Learning opencv step by step

Postby Antonio Linares » Fri Dec 24, 2021 10:17 am

Now we simply change "haarcascade_eye.xml" with "haarcascade_frontalface_alt.xml" and we start recognizing faces :-)

Next... lets recognize anything we want ;-)

Image
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42120
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Learning opencv step by step

Postby Antonio Linares » Sat Dec 25, 2021 9:17 am

Instructions to recognize anything we may want:

https://docs.opencv.org/4.5.4/dc/d88/tutorial_traincascade.html

To support this tutorial, several official OpenCV applications will be used: opencv_createsamples, opencv_annotation, opencv_traincascade and opencv_visualisation.


Createsamples, traincascade and opencv_haartraining are deprecated


Positive samples are created by the opencv_createsamples application


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.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42120
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Learning opencv step by step

Postby mgsoft » Tue Dec 28, 2021 4:57 pm

Excellent, thank you very much for sharing!

Merry Christmas to the whole community.
Saludos,

Eduardo
User avatar
mgsoft
 
Posts: 422
Joined: Mon Aug 17, 2009 12:18 pm
Location: España

Re: Learning opencv step by step

Postby Antonio Linares » Thu Dec 30, 2021 12:07 pm

Instead of having to train our data and create a XML file ("Haar Cascade") we have found that for images matching it is much simpler to use cv.matchTemplate() as this way we don't need to train any data, we simply search an image inside another image.

We have found two ways of doing this: Using GDI+ or using OpenCV

Thanks to Manuel Alvarez and Cristobal Navarro now we have a new function GDP_IMAGEINTOIMAGE() that uses GDI+.
You can think of it like doing a simple At( "bitmap_to_find.bmp", "larger_bitmap_that_contains_the_previous_bitmap_to_search_for.bmp" )

Using OpenCV we can rotate the "bitmap_to_find.bmp" and find it in any position (90,180,270 degrees), something that GDP_IMAGEINTOIMAGE() does not support yet, though GDI+ can also rotate an image. So we can expect that we are going to have two different ways of searching a bitmap inside a larger bitmap

I see this as a huge progress in bitmaps management :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42120
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Learning opencv step by step

Postby Verhoven » Sat Sep 17, 2022 7:23 pm

Creo que lo de reconocer objetos puede ser un trabajo arduo, no obstante se va a intentar.
He conseguido compilar el ejemplo de Antonio, pero al compilar me arroja los siguientes errores que no se como resolver:

Code: Select all  Expand view  RUN
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\ostream(410): warning C4530: Se ha utilizado el controlador de excepciones de C++, pero la semántica de desenredo no está habilitada. Especifique /EHsc
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\ostream(403): note: al compilar la función del miembro clase plantilla "std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(double)"
C:\opencv\build\include\opencv2/core/utility.hpp(400): note: Vea la referencia a la creación de una instancia de la función plantilla "std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(double)" que se está compilando
C:\opencv\build\include\opencv2/core/cvstd.inl.hpp(82): note: Vea la referencia a la creación de una instancia de clase plantilla "std::basic_ostream<char,std::char_traits<char>>" que se está compilando
Verhoven
 
Posts: 522
Joined: Sun Oct 09, 2005 7:23 pm

Previous

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 63 guests