Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV v2.4.9 equalizeHist() Function CV_Assert

Hi everyone, in my project I get webcam images and try to apply histogram equalization. But when I use equalizeHist() function I get CV_Assert. Here, it's the message I get when I run the program. OpenCV Error: Assertion failed (src.type() == CV_8UC1) in cv::equalizeHist, file ........\opencv\modules\imgproc\src\histogram.cpp, line 3128. I don't know how to convert my src image to CV_8UC1. Is there anyone who can help me? Thanks, already.

void detectObjectsCustom(const Mat &img, CascadeClassifier &cascade, vector<Rect> &objects, int flags, Size minFeatureSize, float searchScaleFactor, int minNeighbors, const int scaledWidth = 320){ 
Mat gray;
if(img.channels() == 3){
   cvtColor(img, gray, CV_BGR2GRAY);
}
else if(img.channels() == 4){
   cvtColor(img, gray, CV_BGRA2GRAY);
}
else{       
    gray = img;
}

Mat inputImg;
float scale = img.cols/(float)scaledWidth;
if(img.cols > scaledWidth){
    int scaledHeight = cvRound(img.rows / scale);
    resize(img, inputImg, Size(scaledWidth, scaledHeight));
}
else{
    inputImg = img;
}
cvtColor(inputImg,inputImg,CV_8UC1);    

Mat equalizedImg;
equalizeHist(inputImg, equalizedImg);

cascade.detectMultiScale(equalizedImg, objects, searchScaleFactor, minNeighbors, flags, minFeatureSize);

if(img.cols > scaledWidth){
    for(int i=0; i < (int)objects.size(); i++){
        objects[i].x = cvRound(objects[i].x * scale);
        objects[i].y = cvRound(objects[i].y * scale);
        objects[i].width = cvRound(objects[i].width * scale);
        objects[i].height = cvRound(objects[i].height * scale);
    }
}

for(int i=0; i < (int)objects.size(); i++){
    if(objects[i].x < 0)
        objects[i].x = 0;
    if(objects[i].y < 0)
        objects[i].y = 0;
    if(objects[i].x + objects[i].width > img.cols)
        objects[i].x = img.cols - objects[i].width;
    if(objects[i].y + objects[i].height > img.rows)
        objects[i].y = img.rows - objects[i].height;
}    }