Ask Your Question
0

OpenCV v2.4.9 equalizeHist() Function CV_Assert [closed]

asked 2015-01-02 06:59:16 -0600

darknesska gravatar image

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;
}    }
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by darknesska
close date 2015-01-02 10:12:08.204806

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-01-02 08:05:37 -0600

berak gravatar image

updated 2015-01-02 09:20:46 -0600

here's the problem:

 cvtColor(inputImg,inputImg,CV_8UC1);   // CV_8UC1 is the wrong constant.

what you need instead is:

cvtColor(inputImg,inputImg,COLOR_BGR2GRAY);

CV_8UC1 evaluates to 0, so you did the same as:

cvtColor(inputImg,inputImg, COLOR_BGR2BGRA);

thus equalizeHist complains, - the input is still 3-channel, not 1channel.


but, since you already got a grayscale image above, why not use that instead ?

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(gray, inputImg, Size(scaledWidth, scaledHeight)); //use grayscale img  
}
else{
    inputImg = gray; //use grayscale img 
}
//cvtColor(inputImg, inputImg, COLOR_BGR2GRAY);    // no more needed.

Mat equalizedImg;
equalizeHist(inputImg, equalizedImg);
edit flag offensive delete link more

Comments

Now I get this assertion,

OpenCV Error: Assertion failed (scn ==3 || scn == 4) in cv::cvtColor, file ..\..\..\..\opencv\modules\imgproc\src\color.cpp, line 3737
darknesska gravatar imagedarknesska ( 2015-01-02 09:10:18 -0600 )edit
1

OK, I solved this by simply adding the original channel controls to the code. Which is,

Mat gray;
if(img.channels() == 3){
    cvtColor(img, gray, CV_BGR2GRAY);
}
else if(img.channels() == 4){
    cvtColor(img, gray, CV_BGRA2GRAY);
}
else{       
    gray = img;
}
darknesska gravatar imagedarknesska ( 2015-01-02 09:12:25 -0600 )edit

sorry, i just can't count. ;)

should have been if(img.channels() > 1)

but let's keep your solution anyway !

berak gravatar imageberak ( 2015-01-02 09:18:19 -0600 )edit
1

Thank you for your help I couldn't saw that I didn't use gray instead of img :)

darknesska gravatar imagedarknesska ( 2015-01-02 09:20:03 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-01-02 06:59:16 -0600

Seen: 9,765 times

Last updated: Jan 02 '15