Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Accept Grayscale as Input #46

Closed
EzequielAdrianM opened this issue Apr 3, 2017 · 5 comments
Closed

Enhancement: Accept Grayscale as Input #46

EzequielAdrianM opened this issue Apr 3, 2017 · 5 comments

Comments

@EzequielAdrianM
Copy link

Currently when detection is performed, the code reads the channels of a Mat object as input. But if it is in grayscale (Channels == 1) it is converted again to color:

detector.h

virtual inline int det(cv::Mat& image) {
    if(image.empty()) return 0;
    if(image.channels() == 1) {
        cv::cvtColor(image, image, CV_GRAY2BGR);
    } CHECK(image.channels() == 3);
    dlib::cv_image<dlib::bgr_pixel> img(image);
    mRets = mFaceDetector(img);
    return mRets.size();
}

However, the Dlib documentation points out that: "Any built in scalar type may be used as a grayscale pixel type. For example, unsigned char, int, double, etc."
So bgr_pixel type is not the only alternative.

@EzequielAdrianM
Copy link
Author

Solution:

virtual inline int det(cv::Mat& image) {
  if(image.empty()) return 0;
  if(image.channels() > 1) {
    //Convert to GrayScale
    cv::cvtColor(image, image, CV_BGR2GRAY);
  }
  dlib::cv_image<uchar> img(image);
  mRets = mFaceDetector(img);
  return mRets.size();
}

This is really speeding up the detection!!! Hope helping somebody

@e-snail
Copy link

e-snail commented Dec 8, 2017

@EzequielAdrianM Thanks!

cv::cvtColor(image, image, CV_BGR2GRAY); leads to an error. Instead, I try following code in jni/jni_detections/jni_face_det.cpp :

cv::cvtColor(rgbaMat, bgrMat, cv::COLOR_RGBA2GRAY); //replace cv::cvtColor(rgbaMat, bgrMat, cv::COLOR_RGBA2BGR);

But it did NOT work in speeding up the detection. Any idea ?

@EzequielAdrianM
Copy link
Author

EzequielAdrianM commented Dec 8, 2017

It does not work because you are not sending the gray scaled mat to the detector. You keep sending the original in color.
cv::cvtColor(bgrMat, grayMat, cv::COLOR_BGR2GRAY);
Here, "grayMat" is the new simplified Mat you must give to the detector. But you keep sending "image" (the original full color Mat):
dlib::cv_image<unsigned char> img(grayMat);

@e-snail
Copy link

e-snail commented Dec 11, 2017

@EzequielAdrianM I did send gray scaled mat to the detector, NOT the bgr mat. For ur convenience, I adjust the code sequence as following:

in jni/jni_detections/detector.h

cv::Mat image_gray;
if (image.channels() > 1) {
cv::cvtColor(image, image_gray, CV_BGR2GRAY);
}
//CHECK(image.channels() == 3);
// TODO : Convert to gray image to speed up detection
// It's unnecessary to use color image for face/landmark detection
dlib::cv_image<unsigned char> img(image_gray);
mRets = mFaceDetector(img);

Is it right ?

@EzequielAdrianM
Copy link
Author

Yes, it is right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants