美文网首页
图像指定区域的亮度增强与直方图绘制

图像指定区域的亮度增强与直方图绘制

作者: 此间不留白 | 来源:发表于2021-03-21 12:12 被阅读0次

    图像亮度增强

    图像的亮度变化,简单来说就是对图像像素的线性变化,公式如下所示:
    g_{i,j}(x) = \alpha f_{i,j}(x)+ \beta
    其中,f_{i,j}(x)表示第i行,第j列的像素值,g_{i,j}(x)表示亮度变换后的像素值。综上,图像亮度变化的代码如下:

    
    static const double alpha = 1.5;
    static const double beta = -90;
    void highLigh(Mat& img,int x0,int y0,int x1,int y1) //指定区域进行亮度调整
    {
        for (int i=x0;i<x1;i++)
        {
            for (int j = y0; j < y1; j++)
            {
                for (int c = 0; c < 3; c++)
                {
                    img.at<Vec3b>(j, i)[c] = saturate_cast<uchar>(img.at<Vec3b>(j, i)[c] * alpha + beta);
                }
            }
        }
    
    }
    

    指定区域的亮度增强和直方图绘制

    对于一副图像有如下要求:

    1. 加载并显示图像,通过鼠标左键拖拽选定指定区域,当释放左键时,对选定的区域实现亮度增强。
    2. 对选定的区域实现直方图绘制,横轴每个单位区间指定为32个像素值(即0-31,32-63…,224-255),而纵轴显示为区间像素出现的次数。对于一副RGB三通道图像,用红色表示R通道的直方图分布,绿色表示G通道的像素分布,蓝色表示B通道。

    针对以上问题,利用C++和opencv实现的代码如下所示:

    
    #include<iostream>
    
    #include <opencv2/imgproc.hpp>
    #include <opencv2/highgui.hpp>
    #include<opencv2/opencv.hpp>
    using namespace cv;
    
    /*
    create a program that reads in and displays an image.
    a. Allow the user to select a rectangular region in the image by drawing a rectangle with the mouse button held down
       and highlight the region when the mouse button is released.Be careful to save an image copy in memory so that your
       drawing into the image dose not destory the original values there. The next mouse click should start the process all
       over again from the original image
    b. In a separate window,use the drawing functions to draw a graph in blue,green,and red that represents how many pixels
      of each value were found in the selected box.This is the color histofram of that color region.The x-axis should be eight
      bins that represent pixel values falling within the ranges 0-31,32-63,…,223-255.The y-axis should be counts of the number
      of pixels that were found ub that bin range.Do this for each color channel.BGR.
    
    */
    
    
    Point2i initP(0, 0); // 记录初始点坐标
    static const double alpha = 1.5;
    static const double beta = -90;
    void highLigh(Mat& img,int x0,int y0,int x1,int y1) //指定区域进行亮度调整
    {
        for (int i=x0;i<x1;i++)
        {
            for (int j = y0; j < y1; j++)
            {
                for (int c = 0; c < 3; c++)
                {
                    img.at<Vec3b>(j, i)[c] = saturate_cast<uchar>(img.at<Vec3b>(j, i)[c] * alpha + beta);
                }
            }
        }
    
    }
    
    
    void histGraph(Mat& histMat,Mat& histImg, const int& width,const Scalar& rgb)
    {
        double maxVal = 0;
        minMaxLoc(histMat, 0, &maxVal, 0, 0);
        for (int h = 0; h < width; h++)
        {
            int value = cvRound(histImg.rows * 0.9 * histMat.at<float>(h) / maxVal);
            rectangle(histImg, Point(h * (histImg.cols/width), (histImg.rows - 1)), Point((h + 1) * (histImg.cols/width), histImg.rows - 1 - value), rgb, -1);
        }
    }
    void HandlerMouseEvent(int event,int x,int y,int flags,void *p)
    {
        Mat srcImg = *(static_cast<Mat*>(p));
        Mat cloneImg = srcImg.clone();
        //Mat histdigram;
        const int channels[] = { 0,1,2 }; //绘制直方图:3个通道
        int bins_ranges[] = {32};
        MatND m_hist;  //直方图输出
        float hranges[] = { 0,256 };
        const float* ranges[] = {hranges}; //直方图纵轴
        
        
        switch (event)
        {
        case EVENT_LBUTTONDOWN:
        {
    
            initP.x = x;
            initP.y = y;
            break;
    
        }
        case (EVENT_MOUSEMOVE &&  (flags&EVENT_FLAG_LBUTTON)):
        {
            rectangle(cloneImg, Rect2i(initP, Point2i(x, y)), Scalar(233, 233, 84), 1, 8, 0);
            //highLigh(cloneImg, initP.x, initP.y, x, y);
            imshow("SelectRectImage", cloneImg);
            break;
    
        }
        
        case EVENT_LBUTTONUP:
        {
            
            highLigh(cloneImg, initP.x, initP.y, x, y);
            imshow("selectRectImage", cloneImg);
            //画直方图
            Mat histdigram = Mat(cloneImg, Rect(initP, Point2d(x, y)));
            std::vector<Mat> bgr_channels;
            split(histdigram, bgr_channels);  //通道分离
            Mat m_hist1, m_hist2, m_hist3;
            calcHist(&bgr_channels[0], 1, 0, Mat(),  m_hist1, 1, bins_ranges, ranges, true, false);
            calcHist(&bgr_channels[1], 1, 0, Mat(), m_hist2, 1, bins_ranges, ranges, true, false);
            calcHist(&bgr_channels[2], 1, 0, Mat(), m_hist3, 1, bins_ranges, ranges, true, false);
            
            
            Mat histImg = Mat::zeros(512, 512, CV_8UC3);
            histGraph(m_hist1, histImg, bins_ranges[0], Scalar(255, 0, 0));
            histGraph(m_hist2, histImg, bins_ranges[0], Scalar(0, 255, 0));
            histGraph(m_hist3, histImg, bins_ranges[0], Scalar(0, 0, 255));
    
            
            imshow("HistTest", histImg);
            break;
        }
        
        
        }
    
    }
    
    int main()
    {
        Mat img = imread("ex3.jpg");
        namedWindow("SelectRectImage",0);
        namedWindow("HistTest"); //显示直方图视图
        setMouseCallback("SelectRectImage", HandlerMouseEvent,&img);
        HandlerMouseEvent(0, 0, 0, 1,&img);
        imshow("SelectRectImage", img);
        waitKey(0);
    
        return 0;
    }
    
    

    根据以上代码,实现效果如下:

    对选定ROI区域进行亮度增强,并绘制ROI区域的直方图

    注意:直方图绘制中,利用了opencv提供的calchist()函数,对于此函数的参数参数,有如下解释:

    void cv::calchist(const Mat* images,     //输入图像(源图像),具有相同的深度和尺寸
                              int nimages,        //输出图像的数量
                              const int* channels,   //通道数量,对于多幅图像而言,第一个图像的通道数为0-images[0].channels(),第二个数组通道为images[0].channels()-images[0].channels()+images[1].channels()-1,以此类推
                              inputArray mask,        //可选参数,一般默认为Mat(),如果不为空,设定为与images[i]相同的尺寸
                              outputArray hist,       //直方图输出,一个密集或稀疏矩阵
                              int dims,                   // 直方图的维数,必须是正值且大于 CV_MAX_DIMS
                              const int* histSIze,   //直方图每一个维度的大小
                              const float** ranges,  // 每个维度中直方图bin边界的dims数组。 当直方图是均匀的时(uniform = true),则对于每个维度i,足以指定第0个直方图bin的下(包含)边界L0和上界(专有)边界UhistSize [i] -1 最后一个直方图bin histSize [i] -1。 也就是说,在均匀的直方图的情况下,range [i]中的每个都是2个元素的数组。 当直方图不一致时(均等= false),则每个range [i]都包含histSize [i] +1个元素:L0,U0 = L1,U1 = L2,...,UhistSize [i] -2 = LhistSize [i] -1,UhistSize [i] -1。 不在L0和UhistSize [i] -1之间的数组元素不计入直方图。
                              bool uniform = true,  // 是否均匀分布
                              bool accumulate=false  //累加标记,当设置为1时,当分配直方图时,不会清楚初始状态,此功能可以从几组数组中计算单个直方图或者及时更新直方图。
    
    
    )
    

    相关文章

      网友评论

          本文标题:图像指定区域的亮度增强与直方图绘制

          本文链接:https://www.haomeiwen.com/subject/cyebcltx.html