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

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

作者: 此间不留白 | 来源:发表于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时,当分配直方图时,不会清楚初始状态,此功能可以从几组数组中计算单个直方图或者及时更新直方图。


)

相关文章

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

    图像亮度增强 图像的亮度变化,简单来说就是对图像像素的线性变化,公式如下所示:其中,表示第行,第列的像素值,表示亮...

  • 直方图均衡 Histogram Equalization

    亮度直方图 在说明直方图均衡之前,先说说亮度直方图的概念。为了评估一个图像的色调转换,首先需要建立亮度直方图。亮度...

  • 9- OpenCV+TensorFlow 入门人工智能图像处理-

    图像美化 案例1: 直方图 案例2: 直方图均衡化 案例3: 亮度增强 案例4: 磨皮美白 案例5: 图片滤波 案...

  • 图像直方图与直方图均衡化

    图像直方图(英语:Image Histogram)是用以表示数字图像中亮度分布的直方图,标绘了图像中每个亮度值的像...

  • 46. 彩色图片直方图

    关于图片美化部分的解释有直方图 直方图均衡化 亮度增强 磨皮美白 图像滤波 高斯滤波 等11种效果。其中,彩色图片...

  • Core Image编程指南翻译四(自动增强图像)

    示例代码下载 自动增强图像 Core Image的自动增强功能可分析图像的直方图,面部区域内容和元数据属性。然后它...

  • 基本图像操作

    基本图像操作 1.直方图(histograms) 定义 直方图是对图像在某个指标的不同值的数量的统计,如亮度直方图...

  • 2019-04-10 OpenCV学习

    11边缘保留滤波(EPF) 美化图片 12图像直方图 13直方图应用 直方图均衡化:图像增强的一个手段 直方图比较...

  • CSS进阶知识点--背景

    背景图像区域 background-clip 属性 指定背景绘制区域 语法 background-clip:bor...

  • CSS3背景与渐变

    CSS3 背景图像区域 background-clip(指定背景绘制区域)ackground-clip: bord...

网友评论

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

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