diy:projets:chromakey
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
diy:projets:chromakey [2018/06/25 08:49] – [Vidéo de démonstration et sources du projet] sdurand | diy:projets:chromakey [2018/07/02 09:35] (Version actuelle) – [Sources du projet] sdurand | ||
---|---|---|---|
Ligne 13: | Ligne 13: | ||
Il est important de noter que vous aurez besoin d'une installation d' | Il est important de noter que vous aurez besoin d'une installation d' | ||
De plus, si vous ne travaillez pas directement sur le raspberry (mais par exemple en ssh), je met à votre disposition un programme permettant de visualiser | De plus, si vous ne travaillez pas directement sur le raspberry (mais par exemple en ssh), je met à votre disposition un programme permettant de visualiser | ||
- | les images traitées aussi simplement que possible | + | les images traitées aussi simplement que possible, |
ou, pour plus d' | ou, pour plus d' | ||
Ce programme nécessite une installation d' | Ce programme nécessite une installation d' | ||
Ligne 77: | Ligne 77: | ||
===== Développement du programme ===== | ===== Développement du programme ===== | ||
- | ==== La compilation ==== | ||
- | |||
- | Pour ne pas vous embêter avec la compilation de votre code, je vous recommande fortement d' | ||
- | votre disposition [[https:// | ||
- | les **options de compilation** nécessaire pour compiler un programme utilisant OpenCV. Ce makefile requiert que votre fichier soit nommé chroma_key.cpp, | ||
- | modifier ça à votre convenance. Vous devrez également veiller à ce que ce soit bien votre compilateur C++ qui soit renseigné dans le makefile (variable CC). L' | ||
==== La fonction de traitement ==== | ==== La fonction de traitement ==== | ||
Ligne 88: | Ligne 82: | ||
La fonction de traitement (qui est en quelque sorte le coeur de notre code) se contente de **traduire scrupuleusement | La fonction de traitement (qui est en quelque sorte le coeur de notre code) se contente de **traduire scrupuleusement | ||
l' | l' | ||
- | (une version complète du code est disponible sur [[https:// | ||
<code C++> | <code C++> | ||
// frame: notre image à traiter | // frame: notre image à traiter | ||
Ligne 126: | Ligne 119: | ||
Vous pouvez également voir que j' | Vous pouvez également voir que j' | ||
- | la valeur de notre couleur "à remplacer" | + | la valeur de notre couleur "à remplacer" |
- | [[https:// | + | |
Ligne 251: | Ligne 243: | ||
images traitées en jpeg pour les transmettre sur le réseau et les afficher en exécutant un autre programme sur le poste | images traitées en jpeg pour les transmettre sur le réseau et les afficher en exécutant un autre programme sur le poste | ||
sur lequel vous travaillez. Cette affichage nécessitera comme annoncé au début de ce tutoriel une installation d' | sur lequel vous travaillez. Cette affichage nécessitera comme annoncé au début de ce tutoriel une installation d' | ||
- | compilateur C++ sur votre machine (et n' | + | compilateur C++ sur votre machine (et n' |
- | il est accompagné du makefile permettant de le compiler. | + | |
Si vous êtes curieux de savoir ce qui se passe vraiment ici, je vous invite à consulter la documentation d' | Si vous êtes curieux de savoir ce qui se passe vraiment ici, je vous invite à consulter la documentation d' | ||
Ligne 259: | Ligne 250: | ||
votre raspberry et à exécuter **sur votre poste**: | votre raspberry et à exécuter **sur votre poste**: | ||
<code shell> | <code shell> | ||
- | netcat -l -p un_numéro_de_port | bin/dbgrm | + | netcat -l -p un_numéro_de_port | dbgrm |
</ | </ | ||
Et **sur votre raspberry**: | Et **sur votre raspberry**: | ||
<code shell> | <code shell> | ||
- | bin/chrk paramètres_de_la commande | netcat ip_de_votre_poste le_même_numéro_de_port | + | chrk paramètres_de_la commande | netcat ip_de_votre_poste le_même_numéro_de_port |
</ | </ | ||
+ | |||
+ | **Le programme exécuter sur votre machine (ci-dessus) se contentera simplement de lire les données de chaque image sur stdin, de les décoder et de les afficher** (cf rubrique " | ||
**Vous avez terminé !** Cependant, vous êtes probablement déçu de la fluidité de la vidéo si vous utilisez la 2ème méthode. | **Vous avez terminé !** Cependant, vous êtes probablement déçu de la fluidité de la vidéo si vous utilisez la 2ème méthode. | ||
Ligne 317: | Ligne 310: | ||
===== Sources du projet ===== | ===== Sources du projet ===== | ||
- | ==== Vidéo de démonstration | + | ==== Sources du projet |
- | Comme vous pourrez le voir dans cette vidéo, on n' | + | === " |
- | en temps réel. Vous observerez également également que dans des conditions moyennes, une partie de l' | + | |
- | ne souhaiterai pas voir disparaître disparaît (ici, mon bras). | + | <code C++> |
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include <omp.h> | ||
- | {{youtube> | ||
- | ==== Sources du projet ==== | + | #define WIDTH 1280 |
+ | #define HEIGHT 720 | ||
+ | #define FRAME_SIZE WIDTH*HEIGHT*3 | ||
+ | #define NUMBER_OF_FRAMES 150 | ||
- | Comme je l'ai répété à plusieurs reprises tout au long de ce tutoriel, les sources de tout le projet sont | + | #define ALL_GOOD 0 |
- | disponibles à [[https:// | + | #define INVALID_ARGUMENTS -1 |
+ | #define INVALID_IMAGE -2 | ||
- | Cela inclus à la fois la partie capture/ | ||
- | méthode d' | ||
- | [[https:// | ||
+ | // For a bit of comfort | ||
+ | using namespace cv; | ||
+ | |||
+ | // Simple function to test if a channel of a pixel (B, G or R) is within the interval [value - offset; value + offset] | ||
+ | inline bool inBounds(int value, int bound, int offset) { | ||
+ | return abs(value - bound) <= offset; | ||
+ | } | ||
+ | |||
+ | |||
+ | // Most important part of the code: | ||
+ | // Replaces the pixels of the frame close enough (depending of the offset) to the color B, G, R | ||
+ | inline void chromakey(Mat& | ||
+ | // Using openMP was definitely worth on a Raspberry Pi 3 model B | ||
+ | #pragma omp parallel for | ||
+ | // Iterating on the rows of our frame | ||
+ | for(int i = 0; i < frame.rows; i++) { | ||
+ | // Using a pointer to the rows is the fastest way to access a matrix | ||
+ | Vec3b *Fi = frame.ptr< | ||
+ | const Vec3b *Bi = background.ptr< | ||
+ | |||
+ | // Iterating on the elements of each row | ||
+ | for(int j = 0; j < frame.cols; j++) { | ||
+ | // If the pixel (i, j) of our frame is close enough to B, G and R, we replace it with the color of the background (i, j) pixel | ||
+ | if(inBounds(Fi[j][0], | ||
+ | for(int k = 0; k < 3; k++) { | ||
+ | Fi[j][k] = Bi[j][k]; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | int main(int argc, char** argv) { | ||
+ | // Just a simple parameters check | ||
+ | if(argc != 6) { | ||
+ | std::cerr << " | ||
+ | |||
+ | return INVALID_ARGUMENTS; | ||
+ | } | ||
+ | |||
+ | // This Mat contains our background | ||
+ | Mat background; | ||
+ | background = imread(argv[5]); | ||
+ | |||
+ | // Testing that the images size is the same as defined | ||
+ | // We could also just reshape the image, it would be a bit less restrictive | ||
+ | if(!background.data || background.cols != WIDTH || background.rows != HEIGHT) { | ||
+ | std::cerr << " | ||
+ | |||
+ | return INVALID_IMAGE; | ||
+ | } | ||
+ | |||
+ | int B, G, R, offset; | ||
+ | |||
+ | // Getting the parameters from the command line | ||
+ | std:: | ||
+ | std:: | ||
+ | std:: | ||
+ | std:: | ||
+ | |||
+ | rb >> B; | ||
+ | rg >> G; | ||
+ | rr >> R; | ||
+ | rt >> offset; | ||
+ | |||
+ | // This Mat will contain the images | ||
+ | Mat frame; | ||
+ | |||
+ | // Initializing the capture, should be equivalent to: | ||
+ | // VideoCapture cam(0); | ||
+ | VideoCapture cam(0); | ||
+ | |||
+ | // Setting the width and height of the capture as defined | ||
+ | // If not set, default resolution is 640*480 | ||
+ | cam.set(3, WIDTH); | ||
+ | cam.set(4, HEIGHT); | ||
+ | |||
+ | // Adding the OpenCV JPEG parameters to a vector, this is used to encode a Mat with non-default settings | ||
+ | std:: | ||
+ | jpg_parameters.push_back(CV_IMWRITE_JPEG_QUALITY); | ||
+ | jpg_parameters.push_back(75); | ||
+ | |||
+ | std:: | ||
+ | |||
+ | for(int frame_count = 0; frame_count < NUMBER_OF_FRAMES; | ||
+ | const auto start = std:: | ||
+ | |||
+ | do { | ||
+ | cam >> frame; | ||
+ | } while(frame.empty()); | ||
+ | |||
+ | chromakey(frame, | ||
+ | |||
+ | // Encoding the frame | ||
+ | imencode(" | ||
+ | const unsigned int size = buffer.size(); | ||
+ | |||
+ | // Writing the encoded image to stdout | ||
+ | // unsigned int and uchar size are the same on ARM and 64 bits regular computer | ||
+ | fwrite(& | ||
+ | fwrite(buffer.data(), | ||
+ | |||
+ | // We use stderr to print an FPS counter and not because we are already writing images on stdout | ||
+ | const std:: | ||
+ | |||
+ | std::cerr << " | ||
+ | } | ||
+ | std::cerr << std::endl; | ||
+ | |||
+ | return ALL_GOOD; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === " | ||
+ | |||
+ | <code C++> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | // WIDTH, HEIGHT and NUMBER_OF_FRAMES should always be set to the same value as in the transmitter | ||
+ | #define WIDTH 1280 | ||
+ | #define HEIGHT 720 | ||
+ | // Maximum size of the received frame | ||
+ | // In theory, if jpeg doesn' | ||
+ | // I'm not sure it can ever happen, so, as this program should not be used for critical stuff (it really should not), I'm just gonna assume it won't happen | ||
+ | #define FRAME_SIZE WIDTH*HEIGHT*3 | ||
+ | #define NUMBER_OF_FRAMES 150 | ||
+ | |||
+ | #define ALL_GOOD 0 | ||
+ | |||
+ | |||
+ | |||
+ | int main(int argc, char **argv) { | ||
+ | // Actual size of the received frame | ||
+ | unsigned int size; | ||
+ | uchar buffer[FRAME_SIZE]; | ||
+ | cv::Mat frame; | ||
+ | |||
+ | for(int frame_count = 0; frame_count < NUMBER_OF_FRAMES; | ||
+ | // Reading the encoded image from stdin | ||
+ | // unsigned int and uchar size are the same on ARM and 64 bits regular computer | ||
+ | fread(& | ||
+ | fread(buffer, | ||
+ | |||
+ | std:: | ||
+ | |||
+ | // Decoding the received frame | ||
+ | frame = cv:: | ||
+ | |||
+ | // Displaying the received frame | ||
+ | cv:: | ||
+ | |||
+ | // This wait is mandatory for the image to actually display | ||
+ | cv:: | ||
+ | } | ||
+ | |||
+ | return ALL_GOOD; | ||
+ | } | ||
+ | </ | ||
---- | ---- | ||
- | // | + | // |
diy/projets/chromakey.1529916586.txt.gz · Dernière modification : 2018/06/25 08:49 de sdurand