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

Image size for detection #12

Closed
pallegd opened this issue May 11, 2017 · 4 comments
Closed

Image size for detection #12

pallegd opened this issue May 11, 2017 · 4 comments

Comments

@pallegd
Copy link

pallegd commented May 11, 2017

It appears that the number of pixels in each direction must be a multiple of 16 for the detection to find e.g. faces using the included Haar cascade for frontal faces. When the image dimensions are not multiple of 16, there is no error reported, but the detection algorithm does not find any faces.

@ermig1979
Copy link
Owner

Could you write a code that you use to perform detection? I have tried to detect objects when the image dimensions are not multiple of 16, but the algorithm works fine. So I think that you could incorrect use Simd::Detection.

@pallegd
Copy link
Author

pallegd commented May 11, 2017

This is pretty much it:

#include "simd/src/Simd/SimdDetection.hpp"
#include "simd/src/Simd/SimdDrawing.hpp"

typedef Simd::DetectionSimd::Allocator Detection;
// set to false for no debug output
bool debug_active = false;

void Debug(const char *s)
{
if (debug_active)
{
printf("%s\n", s);
}
}

extern "C" long InitDetection(char *detectionPath, int pictureWidth, int pictureHeight)
{
// create detection object
Detection *detection = new Detection();

// attempt to load detection
if (detection->Load(detectionPath))
{
    // load of detection ok, create size object
    Detection::Size size;
Detection::Size size_min;
    size.x = pictureWidth;
    size.y = pictureHeight;
    if (detection->Init(size))
    {
        // all ok, return pointer to created detection object
        return (long) detection;
    }
    else
    {
        Debug("InitDetection: Failed to init detection.");
    }
}
else
{
    Debug("InitDetection: Failed to load detection.");
}

// load of detection failed
// remove detection, and return 0
delete detection;
return 0;

}

extern "C" int CloseDetection(long detectionObject)
{

extern "C" int CalcDetection(long detectionObject, int pictureWidth, int pictureHeight, void* pictureData, int* results)
{
// get detection object
Detection detection = (Detection) detectionObject;

// return -1 if Detect fails
int found = -1;

// clear results
memset(results, -1, 4 * MAX_RESULTS * sizeof(int));

// create view object from image data
Detection::View *image = new Detection::View(pictureWidth, pictureHeight, Detection::View::Format::Gray8, pictureData, 16);

Detection::Objects objects;
if (detection->Detect(*image, objects))
{
    Debug("CalcDetection: Detect returned ok.");

	// set number of objects found
	found = objects.size();

    // make results array
    for (size_t i = 0; (i < objects.size()) && (i < MAX_RESULTS); ++i)
    {
    	*results++ = objects[i].rect.left;
        *results++ = objects[i].rect.top;
        *results++ = objects[i].rect.right;
        *results++ = objects[i].rect.bottom;
    }

	// only for debug purposes
	if (debug_active)
	{
		// display number of found objects
		char s[80];
		sprintf(s, "CalcDetection: Found %i objects.", (int)objects.size());
		Debug(s);

    	// draw rectangle around hits
    	for (size_t i = 0; i < objects.size(); ++i)
    		Simd::DrawRectangle(*image, objects[i].rect, uint8_t(255));
	
		// save output
		if (image->Save("output.pgm"))
		{
			Debug("CalcDetection: Save ok.");
		}
	}
}
else
{
    Debug("CalcDetection: Failed to perform calculation.");
}

// remove view object
delete image;

// return number of objects found, -1 if error
return found;

}

@ermig1979
Copy link
Owner

You have a mistake at this line:

Detection::View *image = new Detection::View(pictureWidth, pictureHeight, Detection::View::Format::Gray8, pictureData, 16);

This constructor creates a Simd::View with given alignment but original image doesn't have the same one.
Better way is using another constructor to create Simd::View:

Detection::View *image = new Detection::View(pictureWidth, pictureHeight, pictureWidth, Detection::View::Format::Gray8, pictureData);

@pallegd
Copy link
Author

pallegd commented May 15, 2017

Thank, this solves my problem, closing the issue.

@pallegd pallegd closed this as completed May 15, 2017
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