Combining Biometrics SDK with OpenCV

General information about Neurotechnology and its software

Combining Biometrics SDK with OpenCV

Postby ygiuseppin » Aug 03, 2017 19:04

I want to use OpenCV Mat images to create some NSubjects and do the enroll, identification, matching ..
I found in this forum that the best way to do that is to create a NImage with NImageCreateFromData and then set the image to the face with NFaceSetImage.

I could not find an example for this so I ask you for some code that help me with the NImage creation arguments (...)

Code: Select all

NSubject createSubjectFromCVMat(cv::Mat& cv_img){

  NImage nimg;
  NResult res = NImageCreateFromDataEx(...);
  NFace face;
  face.SetImage(nimg);
  NSubject subj;
  subj.GetFaces().Add(face);
  subj.SetId(subj_id);
  return subj;

}

Is this the way?
ygiuseppin
 
Posts: 8
Joined: Aug 03, 2017 18:26

Re: Combining Biometrics SDK with OpenCV

Postby MartynasV » Aug 04, 2017 10:51

ygiuseppin wrote:I want to use OpenCV Mat images to create some NSubjects and do the enroll, identification, matching ..
I found in this forum that the best way to do that is to create a NImage with NImageCreateFromData and then set the image to the face with NFaceSetImage.

I could not find an example for this so I ask you for some code that help me with the NImage creation arguments (...)

Code: Select all

NSubject createSubjectFromCVMat(cv::Mat& cv_img){

  NImage nimg;
  NResult res = NImageCreateFromDataEx(...);
  NFace face;
  face.SetImage(nimg);
  NSubject subj;
  subj.GetFaces().Add(face);
  subj.SetId(subj_id);
  return subj;

}

Is this the way?

Hello ygiuseppin,

Code: Select all
void convertMat2Image(cv::Mat frame, HNImage *hImage){
    NResult result = NImageCreateFromDataEx(NPF_RGB_8U, frame.cols, frame.rows, 0, frame.cols * 3, (void*)frame.data, frame.cols * frame.rows * 3, NI_SRC_ALPHA_CHANNEL_FIRST, hImage);
    if (NFailed(result))
    {
        std::cout << "NImageCreateFromDataEx failed" << std::endl;
    }
}
Martynas V
Neurotechnology
MartynasV
Neurotechnology
 
Posts: 248
Joined: Feb 27, 2015 11:24
Location: Vilnius, Lithuania

Re: Combining Biometrics SDK with OpenCV

Postby ygiuseppin » Aug 07, 2017 17:24

Thank you for your example,
but that code didnt build for me, this is the error:

error: invalid cast from type ‘const Neurotec::Images::NPixelFormat’ to type ‘Neurotec::Images::NPixelFormat_ {aka unsigned int}’

In this line of code:
NResult result = NImageCreateFromDataEx(NPixelFormat_(NPF_RGB_8U), frame.cols, frame.rows, 0, frame.cols * 3, (void*)frame.data, frame.cols * frame.rows * 3, NI_SRC_ALPHA_CHANNEL_FIRST, hImage);

How can I do this convertion?
ygiuseppin
 
Posts: 8
Joined: Aug 03, 2017 18:26

Re: Combining Biometrics SDK with OpenCV

Postby MartynasV » Aug 08, 2017 09:12

ygiuseppin wrote:Thank you for your example,
but that code didnt build for me, this is the error:

error: invalid cast from type ‘const Neurotec::Images::NPixelFormat’ to type ‘Neurotec::Images::NPixelFormat_ {aka unsigned int}’

In this line of code:
NResult result = NImageCreateFromDataEx(NPixelFormat_(NPF_RGB_8U), frame.cols, frame.rows, 0, frame.cols * 3, (void*)frame.data, frame.cols * frame.rows * 3, NI_SRC_ALPHA_CHANNEL_FIRST, hImage);

How can I do this convertion?

Hello ygiuseppin,

Use following with C "NImageCreateFromDataEx" method:
Code: Select all
NPF_RGB_8U.GetValue()

OR

Use following C++ method:
Code: Select all
Neurotec::NSizeType srcPlane[] = { img.rows * img.cols * 3 };
NImage nImage = NImage::FromData(NPF_RGB_8U, 0, img.cols, img.rows, img.cols * 3, &(void * const *)img.data, &srcPlane[0], 1, 0, 0, img.cols, img.rows, NI_SRC_ALPHA_CHANNEL_FIRST);


As you are working with C++, I think you should use C++ method.
Martynas V
Neurotechnology
MartynasV
Neurotechnology
 
Posts: 248
Joined: Feb 27, 2015 11:24
Location: Vilnius, Lithuania

Re: Combining Biometrics SDK with OpenCV

Postby ygiuseppin » Aug 10, 2017 23:59

I tried the C++ example:

Code: Select all
NImage convertMat2Image(cv::Mat& frame){
    Neurotec::NSizeType srcPlane[] = { frame.rows * frame.cols * 3 };
    NImage nImage = NImage::FromData(NPF_RGB_8U, 0, frame.cols, frame.rows, frame.cols * 3, (void * const *)frame.data, &srcPlane[0], 1, 0, 0, frame.cols, frame.rows, NI_SRC_ALPHA_CHANNEL_FIRST);
    return nImage;
}


But it throws SEGFAULT on FromData function. I checked if the Mat image was the problem but not, it shows perfectly.
What could be the problem ?
ygiuseppin
 
Posts: 8
Joined: Aug 03, 2017 18:26

Re: Combining Biometrics SDK with OpenCV

Postby MartynasV » Aug 11, 2017 11:24

ygiuseppin wrote:I tried the C++ example:

Code: Select all
NImage convertMat2Image(cv::Mat& frame){
    Neurotec::NSizeType srcPlane[] = { frame.rows * frame.cols * 3 };
    NImage nImage = NImage::FromData(NPF_RGB_8U, 0, frame.cols, frame.rows, frame.cols * 3, (void * const *)frame.data, &srcPlane[0], 1, 0, 0, frame.cols, frame.rows, NI_SRC_ALPHA_CHANNEL_FIRST);
    return nImage;
}


But it throws SEGFAULT on FromData function. I checked if the Mat image was the problem but not, it shows perfectly.
What could be the problem ?

Hello ygiuseppin,

SEGFAULT is thrown when you mismanage memory, some of the parameters probably are written incorrectly.

My coleague provided even simplier method:
Code: Select all
NImage image = NImage::GetWrapper(NPF_GRAYSCALE_8U, frame.cols, frame.rows, frame.step, frame.data, frame.cols * frame.rows * 3, false);

NPF_GRAYSCALE_8U - Change this value according to OpenCV image type
Martynas V
Neurotechnology
MartynasV
Neurotechnology
 
Posts: 248
Joined: Feb 27, 2015 11:24
Location: Vilnius, Lithuania

Re: Combining Biometrics SDK with OpenCV

Postby ygiuseppin » Aug 11, 2017 12:44

This other way didnt throw segfault for my rgb image. But there is another problem with that image.
In some part of my "detectFeatures" function, I do:

Code: Select all
        // Create the subject
        NSubject subject;
        NImage nimg = convertMat2Image(cv_img);
        NFace face;
        face.SetImage(nimg);
        subject.GetFaces().Add(face);
        subject.SetMultipleSubjects(true);

        // Perform the detection of features
        NBiometricTask task = biometricClient->CreateTask(nboDetectSegments, subject);
        biometricClient->PerformTask(task);
        NBiometricStatus status = task.GetStatus();


Status is always different of nbsOk, no matter the image.

If I change
Code: Select all
face.SetImage(nimg);

for
Code: Select all
std::string filename = "image.png";
face.SetFileName((char*)filename.c_str());


It works, and It is the same image saved from an opencv Mat.
It always work for image files.
ygiuseppin
 
Posts: 8
Joined: Aug 03, 2017 18:26

Re: Combining Biometrics SDK with OpenCV

Postby MartynasV » Aug 14, 2017 10:39

ygiuseppin wrote:This other way didnt throw segfault for my rgb image. But there is another problem with that image.
In some part of my "detectFeatures" function, I do:

Code: Select all
        // Create the subject
        NSubject subject;
        NImage nimg = convertMat2Image(cv_img);
        NFace face;
        face.SetImage(nimg);
        subject.GetFaces().Add(face);
        subject.SetMultipleSubjects(true);

        // Perform the detection of features
        NBiometricTask task = biometricClient->CreateTask(nboDetectSegments, subject);
        biometricClient->PerformTask(task);
        NBiometricStatus status = task.GetStatus();


Status is always different of nbsOk, no matter the image.

If I change
Code: Select all
face.SetImage(nimg);

for
Code: Select all
std::string filename = "image.png";
face.SetFileName((char*)filename.c_str());


It works, and It is the same image saved from an opencv Mat.
It always work for image files.

Hello ygiuseppin,

What status or error do you get?
Try saving converted image as file:
Code: Select all
nimg.Save("image.png");
Martynas V
Neurotechnology
MartynasV
Neurotechnology
 
Posts: 248
Joined: Feb 27, 2015 11:24
Location: Vilnius, Lithuania


Return to Public

Who is online

Users browsing this forum: No registered users and 3 guests