2013-08-11 5 views
0

J'essaie de superposer une image sur la capture vidéo de mon programme avec OpenCV mais j'ai du mal à la faire fonctionner. J'ai mis la région d'intérêt dans le cadre original pris de la webcam sous la forme d'un rectangle. Ensuite, je le copie dans le cadre d'origine. Cependant, il n'apparaît jamais sur le nouveau cadre capturé par la webcam. Je l'ai testé et l'image se charge correctement mais elle n'est pas copiée dans la nouvelle image pour une raison quelconque.Overlay Image dans OpenCV

code ci-dessous en C++:

#include<opencv2/core/core.hpp> 
#include<opencv2/contrib/contrib.hpp> 
#include<opencv2/highgui/highgui.hpp> 
#include<opencv2/imgproc/imgproc.hpp> 
#include<iostream> 
#include<vector> 

using namespace std; 
using namespace cv; 

int main(){ 
    VideoCapture cap; 
    cap.open(0); 

    if(!cap.isOpened()){ 
     cerr << "Error opening the webcam!" << endl; 
     return -1; 
    } 


    for(;;){ 
     Mat frame; 

     cap>>frame; 

     Mat newFrame; 
     frame.copyTo(newFrame); 

     Mat image = imread("C:\\User\\Desktop\\images\\image.png"); 


    int cx = (newFrame.cols - 70)/2; 
     if (image.data) { 
      // Get a BGR version of the face, since the output is BGR color 
      Mat srcBGR = Mat(face.size(), CV_8UC3); 
      cvtColor(image, srcBGR, CV_GRAY2BGR); 
      // Get the destination ROI (and make sure it is within the image) 
      Rect dstRC = Rect(cx, newFrame.rows/2, 70, 70); 
      Mat dstROI = newFrame(dstRC); 
      // Copy the pixels from src to dst. 
      srcBGR.copyTo(dstROI); 
     } 


     imshow("frame", newFrame); 
     char key = (char) waitKey(30); 
     // Exit this loop on escape: 
     if(key == 27) 
      break; 
    } 

    return 0; 
} 

Toute suggestion ou aide serait appréciée. Merci.

Répondre

2

Vous avez converti l'image en BGR en n'utilisant pas celle-ci: changez ceci: image.copyTo (dstROI); à ce srcBGR.copyTo (dstROI);

#include<opencv2/core/core.hpp> 
#include<opencv2/contrib/contrib.hpp> 
#include<opencv2/highgui/highgui.hpp> 
#include<opencv2/imgproc/imgproc.hpp> 
#include<iostream> 
#include<vector> 

using namespace std; 
using namespace cv; 

int main(){ 
    VideoCapture cap; 
    cap.open(0); 

    if(!cap.isOpened()){ 
     cerr << "Error opening the webcam!" << endl; 
     return -1; 
    } 
    Mat image = imread("D:\\ImagesForTest\\Lena.jpg",0); 
    cv::resize(image,image,Size(70,70)); 
    Mat frame; 
    for(;;){ 
     cap>>frame; 
     Mat newFrame=frame.clone(); 
     int cx = (newFrame.cols - 70)/2; 
     if (!image.empty()) { 
      // Get a BGR version of the face, since the output is BGR color 
      Mat srcBGR = Mat(image.size(), CV_8UC3); 
      cvtColor(image, srcBGR, CV_GRAY2BGR); 
      // Get the destination ROI (and make sure it is within the image) 
      Rect dstRC = Rect(cx, newFrame.rows/2, 70, 70); 
      Mat dstROI = newFrame(dstRC); 
      // Copy the pixels from src to dst. 
      srcBGR.copyTo(dstROI); 
     } 
     imshow("frame", newFrame); 
     char key = (char) waitKey(30); 
     // Exit this loop on escape: 
     if(key == 27) 
      break; 
    } 

    return 0; 
} 
+0

En fait, c'est comme ça que je l'avais avant et ça n'a pas marché. Je le testais d'une autre façon et j'ai oublié de le changer. Je viens de le changer dans l'exemple de code à srcBGR.copyTo (dstROI) – TeddyG

+0

Ok, j'ai édité ma réponse, j'ai testé ce code et cela fonctionne. –

+0

Merci. Le problème était que je ne redimensionnais pas l'image en fonction de la taille de la destination. Merci encore. – TeddyG