Hi All,
I'm new to OpenCV and can't seem to get the calcHist method to work. I am attempting to compute an RGB histogram with 8 bins per channel but I always seem to get the following returned:
OpenCV Error: Assertion failed (s >= 0) in cv::setSize, file C:\buildslave64\win64_amdocl\master_PackSlave-win64-vc11-shared\opencv\modules\core\src\matrix.cpp, line 293
I have been attempting to follow the tutorial: http://www.pyimagesearch.com/2014/07/14/3-ways-compare-histograms-using-opencv-python/ albeit in c++, Here is the code:
// OpenCV3TestApplication.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <map>
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
const int nimages = 1,
numDims = 8,
numChannels = 3;
int channels[numChannels] = { 0, 1, 2 };
int histSize[numChannels] = { 31, 31, 31 };
float rrange[2] = { 0, 255 };
float grange[2] = { 0, 255 };
float brange[2] = { 0, 255 };
const float* ranges[numChannels] = { rrange, grange, brange };
if(argc != 3)
{
cout <<" Usage: OpenCV3TestApplication <path to output image> <path to expected image>" << endl;
return -1;
}
Mat output = cv::imread(argv[1], cv::ImreadModes::IMREAD_COLOR);
Mat expected = cv::imread(argv[2], cv::ImreadModes::IMREAD_COLOR);
if(output.empty()) // Check for invalid input
{
cout << "Could not open or find the output image" << endl;
return -1;
}
else if (expected.empty())
{
cout << "Could not open or find the expected image" << endl;
return -1;
}
// Convert to RGB
cv::cvtColor(output, output, cv::COLOR_BGR2RGB);
cv::cvtColor(expected, expected, cv::COLOR_BGR2RGB);
cv::Mat outputHist;
cv::Mat expectedHist;
cv::calcHist(&output, nimages, channels, Mat(), outputHist, numDims, histSize, ranges);
cv::calcHist(&expected, nimages, channels, Mat(), expectedHist, numDims, histSize, ranges);
std::map<int, string> histCompMethods;
histCompMethods[cv::HISTCMP_CORREL] = "CORREL";
histCompMethods[cv::HISTCMP_CHISQR] = "CHI-SQUARED";
histCompMethods[cv::HISTCMP_INTERSECT] = "INTERSECT";
histCompMethods[cv::HISTCMP_HELLINGER] = "HELLINGER";
histCompMethods[cv::HISTCMP_CHISQR_ALT] = "CHI-SQUARED ALT";
histCompMethods[cv::HISTCMP_KL_DIV] = "KL DIV";
/*
for (int i = 0; i < histCompMethods.count; i++)
{
//cout << "Comparison using method '" << histCompMethods[i] << "': " << cv::compareHist(na, na, i);
}
*/
waitKey(0);
return 0;
}
I have tried a variety of different images and settings with no luck. This is using the pre-built version of OpenCV 3.0 (64bit). Any help would be greatly appreciated, thanks!
James