Ask Your Question
0

i have drawn the rectangle for mouth and i would like to crop that drawn position .how to do it?i am a beginner to openCV

asked 2014-07-29 01:04:39 -0500

adarshmn gravatar image

updated 2014-07-29 02:46:49 -0500

Siegfried gravatar image

here is my code for mouth detection.

#include "stdio.h"
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>

int main(int argc, const char** argv) {
  VideoCapture cap(0);
  CascadeClassifier face, mouth;

  face.load("/home/adarsh/opencv/opencv-4.9/data/haarcascades/haarcascade_frontalface_default.xml");

  mouth.load("/home/adarsh/opencv/opencv-2.4.9/data/haarcascades/haarcascade_mcs_mouth.xml");

  Mat frame, grayframe, testframe;

  while (1) {
    cap.read(frame);

    if (!cap.read(frame)) 
    {
      printf("an error while taking the frame from cap");
    }

    vector<Rect> faces; 
    cvtColor(frame, grayframe, CV_BGR2GRAY);
    equalizeHist(grayframe, testframe);
    face.detectMultiScale(testframe, faces, 1.1, 3, CV_HAAR_SCALE_IMAGE,
                          Size(30, 30));

    for (int i = 0; i < faces.size(); i++)
    {
      rectangle(frame, faces[i], Scalar(255, 0, 0), 1, 8, 0);
      Mat face = frame(faces[i]);
      cvtColor(face, face, CV_BGR2GRAY);
      vector<Rect> mouthi;
      mouth.detectMultiScale(face, mouthi);

      for (int k = 0; k < mouthi.size(); k++) 
      {
        Point pt1(mouthi[0].x + faces[i].x, mouthi[0].y + faces[i].y);
        Point pt2(pt1.x + mouthi[0].width, pt1.y + mouthi[0].height);
        rectangle(frame, pt1, pt2, Scalar(255, 0, 0), 1, 8, 0);
      }
    }

    imshow("output", frame);

    int c = waitKey(10);
    if ((char) c == 27){
      break;
    }
  }
  return 0;
}
edit retag flag offensive close merge delete

Comments

1

could you try to format your code ?

(edit, mark it, press the 10101 button)

berak gravatar imageberak ( 2014-07-29 01:12:05 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2014-07-29 03:03:00 -0500

Siegfried gravatar image

Hi, you can get the ROI of the mouth by adding the following code to the second for loop

cv::Mat croped_image = frame(cv::Rect(pt1, pt2));

for example:

  for (int k = 0; k < mouthi.size(); k++) 
  {
    Point pt1(mouthi[0].x + faces[i].x, mouthi[0].y + faces[i].y);
    Point pt2(pt1.x + mouthi[0].width, pt1.y + mouthi[0].height);
    rectangle(frame, pt1, pt2, Scalar(255, 0, 0), 1, 8, 0);

    cv::Mat mouth_image = frame(cv::Rect(pt1, pt2));
    // do something with the croped mouth here
    // ...

  }
edit flag offensive delete link more
-1

answered 2014-07-29 02:00:08 -0500

u can get the region of interest for x,y, height and width and u can crop the image.

edit flag offensive delete link more

Comments

Next time give more information to the topic starter. Your answer doesn't give the author a single clue on how to approach the fact of retrieving the region of interest.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-07-30 04:57:57 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2014-07-29 01:04:39 -0500

Seen: 190 times

Last updated: Jul 29 '14