Ask Your Question
0

Issue with calcHist

asked 2016-03-08 20:38:00 -0600

Durifto gravatar image

updated 2016-03-09 17:38:40 -0600

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/... 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

edit retag flag offensive close merge delete

Comments

please replace your screenshot with a text version, so folks here can actually try your code.

berak gravatar imageberak ( 2016-03-09 01:09:01 -0600 )edit

Updated with the paste, cheers.

Durifto gravatar imageDurifto ( 2016-03-09 17:40:53 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-03-09 18:52:29 -0600

You have to change your numDims from 8 to 3.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-03-08 20:38:00 -0600

Seen: 957 times

Last updated: Mar 09 '16