공학왕이될거야

'OpenCV'에 해당되는 글 3건

  1. 얼굴 중심점 찾기
  2. 노트북 카메라 연동 얼굴 인식
  3. Opencv 설치하기 1

얼굴 중심점 찾기

전공/ARDrone2.0

칼만필터를 이용해서 얼굴 중심점 찾는 코딩을 해봤는데

 

필터를 이용해 원을 그리면 원의 반경이 점점 커지더라고요.

 

 

 

 

 

그래서 그냥 원의 반경을 고정시켜놓고 하였습니다.

 

 

#include "ardrone/ardrone.h"

#include <iostream>

// --------------------------------------------------------------------------
// main(Number of arguments, Argument values)
// Description  : This is the entry point of the program.
// Return value : SUCCESS:0  ERROR:-1
// --------------------------------------------------------------------------
int main(int argc, char *argv[])
{
    // AR.Drone class
    ARDrone ardrone;

   cv::CascadeClassifier face_classifier;

   face_classifier.load("C:/Users/hayoon/Downloads/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml");

    // Initialize
    if (!ardrone.open()) {
        std::cout << "Failed to initialize." << std::endl;
        return -1;
    }

    // Battery
    std::cout << "Battery = " << ardrone.getBatteryPercentage() << "[%]" << std::endl;

    // Instructions
    std::cout << "***************************************" << std::endl;
    std::cout << "*       CV Drone sample program       *" << std::endl;
    std::cout << "*           - How to play -           *" << std::endl;
    std::cout << "***************************************" << std::endl;
    std::cout << "*                                     *" << std::endl;
    std::cout << "* - Controls -                        *" << std::endl;
    std::cout << "*    'Space' -- Takeoff/Landing       *" << std::endl;
    std::cout << "*    'Up'    -- Move forward          *" << std::endl;
    std::cout << "*    'Down'  -- Move backward         *" << std::endl;
    std::cout << "*    'Left'  -- Turn left             *" << std::endl;
    std::cout << "*    'Right' -- Turn right            *" << std::endl;
    std::cout << "*    'Q'     -- Move upward           *" << std::endl;
    std::cout << "*    'A'     -- Move downward         *" << std::endl;
    std::cout << "*                                     *" << std::endl;
    std::cout << "* - Others -                          *" << std::endl;
    std::cout << "*    'C'     -- Change camera         *" << std::endl;
    std::cout << "*    'Esc'   -- Exit                  *" << std::endl;
    std::cout << "*                                     *" << std::endl;
    std::cout << "***************************************" << std::endl;

   // Kalman filter
   cv::KalmanFilter kalman(4, 2, 0);

   // Sampling time [s]
   const double dt = 1.0;

   // Transition matrix (x, y, vx, vy)
   cv::Mat1f A(4, 4);
   A << 1.0, 0.0, dt, 0.0,
      0.0, 1.0, 0.0, dt,
      0.0, 0.0, 1.0, 0.0,
      0.0, 0.0, 0.0, 1.0;
   kalman.transitionMatrix = A;

   // Measurement matrix (x, y)
   cv::Mat1f H(2, 4);
   H << 1, 0, 0, 0,
      0, 1, 0, 0;
   kalman.measurementMatrix = H;

   // Process noise covariance (x, y, vx, vy)
   cv::Mat1f Q(4, 4);
   Q << 1e-5, 0.0, 0.0, 0.0,
      0.0, 1e-5, 0.0, 0.0,
      0.0, 0.0, 1e-5, 0.0,
      0.0, 0.0, 0.0, 1e-5;
   kalman.processNoiseCov = Q;

   // Measurement noise covariance (x, y)
   cv::Mat1f R(2, 2);
   R << 1e-1, 0.0,
      0.0, 1e-1;
   kalman.measurementNoiseCov = R;

   while (1) {
      bool frame_valid = true;

      // Key input
      int key = cv::waitKey(33);
      if (key == 0x1b) break;

      // Get an image
      cv::Mat image = ardrone.getImage();

      cv::Mat frame = image.clone();

      // Take off / Landing
      if (key == ' ') {
         if (ardrone.onGround()) ardrone.takeoff();
         else                    ardrone.landing();
      }

      // Move
      double vx = 0.0, vy = 0.0, vz = 0.0, vr = 0.0;
      if (key == 'i' || key == CV_VK_UP)    vx = 1.0;
      if (key == 'k' || key == CV_VK_DOWN)  vx = -1.0;
      if (key == 'u' || key == CV_VK_LEFT)  vr = 1.0;
      if (key == 'o' || key == CV_VK_RIGHT) vr = -1.0;
      if (key == 'j') vy = 1.0;
      if (key == 'l') vy = -1.0;
      if (key == 'q') vz = 1.0;
      if (key == 'a') vz = -1.0;
      ardrone.move3D(vx, vy, vz, vr);

      // Change camera
      static int mode = 0;
      if (key == 'c') ardrone.setCamera(++mode % 4);

      if (frame_valid) {
         try {
            // convert captured frame to gray scale & equalize
            cv::Mat grayframe;
            cv::cvtColor(frame, grayframe, CV_BGR2GRAY);
            cv::equalizeHist(grayframe, grayframe);

            // -------------------------------------------------------------
            // face detection routine

            // a vector array to store the face found
            std::vector<cv::Rect> faces;

            face_classifier.detectMultiScale(grayframe, faces,
               1.1, // increase search scale by 10% each pass
               3,   // merge groups of three detections
               CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_SCALE_IMAGE,
               cv::Size(30, 30)
               );

            // -------------------------------------------------------------

            // Prediction
            cv::Mat1f prediction = kalman.predict();
            int radius = 1e+3 * kalman.errorCovPre.at<float>(0, 0);
           
            // draw the results
            for (int i = 0; i < faces.size(); i++) {
               cv::Point lb(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
               cv::Point tr(faces[i].x, faces[i].y);

               cv::rectangle(frame, lb, tr, cv::Scalar(0, 255, 0), 3, 4, 0);
        
               cv::Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
           
               cv::circle(frame, center, 10, cv::Scalar(0, 255, 0), 2);
            }
           
            // Display the image
            //cv::imshow("camera", image);
            cv::imshow("webcam", frame);
         }
         catch (cv::Exception& e) {
            std::cerr << "Exception occurred. Ignoring frame... " << e.err << std::endl;
         }
      }
   }

    // See you
    ardrone.close();

    return 0;
}

 

'전공 > ARDrone2.0' 카테고리의 다른 글

피부색 인식  (0) 2017.07.14
ARDrone 영상 받아오기  (0) 2017.07.12

노트북 카메라 연동 얼굴 인식

전공/Opencv



#include <iostream>

#include <opencv2/opencv.hpp>


#include <stdlib.h>

#include <stdio.h>


using namespace std;

using namespace cv;



/* @ function main */

int main(int argc, char *argv[])

{


   // open the default camera

   cv::VideoCapture cap(0);


   // check if we succeeded

   if (!cap.isOpened()) {

      std::cerr << "Could not open camera" << std::endl;

      return -1;

   }


   // create a window

   cv::namedWindow("webcam", 1);


   // face detection configuration

   cv::CascadeClassifier face_classifier;


   face_classifier.load("C:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml");


   cv::Mat frame;


   while(1) {

      bool frame_valid = true;


      try {


         // get a new frame from webcam

         cap >> frame;


      }

      catch (cv::Exception& e) {


         std::cerr << "Exception occurred. Ignoring frame... " << e.err << std::endl;

         frame_valid = false;


      }


      if (frame_valid) {

         try {

            // convert captured frame to gray scale & equalize

            cv::Mat grayframe;

            cv::cvtColor(frame, grayframe, CV_BGR2GRAY);

            cv::equalizeHist(grayframe, grayframe);


            // -------------------------------------------------------------

            // face detection routine


            // a vector array to store the face found

            std::vector<cv::Rect> faces;


            face_classifier.detectMultiScale(grayframe, faces,

               1.1, // increase search scale by 10% each pass

               3,   // merge groups of three detections

               CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_SCALE_IMAGE,

               cv::Size(30, 30)

            );


            // -------------------------------------------------------------

            // draw the results

            for (int i = 0; i < faces.size(); i++) {

               cv::Point lb(faces[i].x + faces[i].width, faces[i].y + faces[i].height);

               cv::Point tr(faces[i].x, faces[i].y);


               cv::rectangle(frame, lb, tr, cv::Scalar(0, 255, 0), 3, 4, 0);

            }


            // print the output

            cv::imshow("webcam", frame);


         }

         catch (cv::Exception& e) {

            std::cerr << "Exception occurred. Ignoring frame... " << e.err << std::endl;

         }

      }


      if (cv::waitKey(30) == 10) break;

   }


   // VideoCapture automatically deallocate camera object

   return 0;


}



'전공 > Opencv' 카테고리의 다른 글

Face-tracking Robot 졸업작품  (18) 2018.01.29
저장된 사진 띄우기  (0) 2017.07.24
ubuntu 16.04 opencv 3.2.0 설치  (2) 2017.07.21
Opencv 설치하기 / 우분투  (0) 2017.07.11
Opencv 설치하기  (1) 2017.07.07

Opencv 설치하기

전공/Opencv

opencv 설치하기에 앞서 visual studio는 2013버전으로 설치해주었습니다.

 

C에서와 마찬가지로 프로젝트를 실행시켜주는데 .c가 아닌 원래의 .cpp로 프로젝트를 열어줍니다.

 

 

 

C에서 하듯이 source에 new item을 추가해주고, 프로젝트의 property를 열어줍니다.

 

 

C/C++ - General - Additional Included Directories에 opencv 폴더들을 추가해줍니다.

 

(opencv 파일이 필요하신 분은 댓글 남겨주세요)

 

 

Linker - General - Additional Library Directories 에 opencv 폴더의 x64 - lib를 추가해줍니다.

 

 

Linker - Input - Additional Dependencies에 필요한 opencv library들을 추가해줍니다.

 

 

debug를 해줍니다.

 

(코드가 필요하신 분은 댓글 남겨주세요)

 

 

dll 에러가 발생합니다.

 

 

Visual Studio 2013 - Projects - (프로젝트 명) 의 Source 파일이 있는 폴더에 opencv의 dll 파일들을 추가해줍니다.

 

다시 debug를 해주면 노트북의 영상을 따올 수 있습니다.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'전공 > Opencv' 카테고리의 다른 글

Face-tracking Robot 졸업작품  (18) 2018.01.29
저장된 사진 띄우기  (0) 2017.07.24
ubuntu 16.04 opencv 3.2.0 설치  (2) 2017.07.21
노트북 카메라 연동 얼굴 인식  (0) 2017.07.14
Opencv 설치하기 / 우분투  (0) 2017.07.11