Histogram equalization not working on color image - OpenCV

前端 未结 3 789
礼貌的吻别
礼貌的吻别 2020-12-23 02:10

I am trying to perform a histogram equalization using OpenCV using the following function

Mat Histogram::Equalization(const Mat& inputImage)
{
    if(inp         


        
3条回答
  •  旧巷少年郎
    2020-12-23 02:49

    I implemented a histogram equalization for BGRA image. I think this function is useful for your goal (but you should ignore the alpha channel).

    Mat equalizeBGRA(const Mat& img)
    {
    Mat res(img.size(), img.type());
    Mat imgB(img.size(), CV_8UC1);
    Mat imgG(img.size(), CV_8UC1);
    Mat imgR(img.size(), CV_8UC1);
    Vec4b pixel;
    
    if (img.channels() != 4)
    {
        cout << "ERROR: image input is not a BGRA image!" << endl;
        return Mat();
    }
    
    for (int r = 0; r < img.rows; r++)
    {
        for (int c = 0; c < img.cols; c++)
        {
            pixel = img.at(r, c);
            imgB.at(r, c) = pixel[0];
            imgG.at(r, c) = pixel[1];
            imgR.at(r, c) = pixel[2];
        }
    }
    
    equalizeHist(imgB, imgB);
    equalizeHist(imgG, imgG);
    equalizeHist(imgR, imgR);
    
    for (int r = 0; r < img.rows; r++)
    {
        for (int c = 0; c < img.cols; c++)
        {
            pixel = Vec4b(imgB.at(r, c), imgG.at(r, c), imgR.at(r, c), img.at(r, c)[3]);
            res.at(r, c) = pixel;
        }
    }
    
    return res;
    }
    

提交回复
热议问题