Opencv(C++)笔记--直方图均衡化、直方图计算

目录

1--直方图均衡化

2--直方图计算


1--直方图均衡化

① 简述:

        对图片的对比度进行调整,输入为灰度图像,对亮度进行归一化处理,提高灰度图的对比度;

② Opencv API:

Opencv(C++)笔记--直方图均衡化、直方图计算_第1张图片

cv::equalizeHist(gray, dst);

③ 代码实例:

# include 
# include 
# include 

int main(int argc, char** argv){

    cv::Mat src;
    src = cv::imread("C:/Users/Liujinfu/Desktop/opencv_bilibili/test1.jpg");
    if (src.empty()){
        printf("could not load image..\n");
        return -1;
    }
    cv::imshow("input", src);

    cv::Mat gray, dst;
    cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY);
    cv::imshow("gray", gray);
    cv::equalizeHist(gray, dst);
    cv::imshow("output", dst);

    cv::waitKey(0);
    return 0;
}

2--直方图计算

① Opencv API:

Opencv(C++)笔记--直方图均衡化、直方图计算_第2张图片

void cv::calcHist(const cv::Mat *images, int nimages, const int *channels, cv::InputArray mask, cv::OutputArray hist, int dims, const int *histSize, const float **ranges, bool uniform = true, bool accumulate = false);

参数简要分析:(具体参数解释见Opencv API)

const cv::Mat *images:输入图像
int nimages:输入图像的数目,第一幅图像的通道标号从 0 到 image[0].channels( ) - 1
const int *channels:需要统计的通道 dim
cv::InputArray mask:掩码,大小和 image 一致,其中把需要处理的部分部分指定为 1,不需要处理的部分指定为0,一般设置为 None,表示处理整幅图像;
cv::OutputArray hist:输出的灰度分布数组
int dims:hist 的维度
const int *histSize:hist 的大小,即直方图横坐标的范围
const float **ranges:hist 的范围,即直方图纵坐标的范围
bool uniform = true:是否归一化

② 代码实例:

# include 
# include 
# include 


int main(int argc, char** argv){

    cv::Mat src;

    src = cv::imread("C:/Users/Liujinfu/Desktop/opencv_bilibili/test1.jpg");
    if (src.empty()){
        printf("could not load image..\n");
        return -1;
    }
    cv::imshow("input", src);

    // 分通道显示
    std::vector bgr_planes;
    cv::split(src, bgr_planes);

    // 计算直方图
    int histSize = 256;
    float range[] = {0, 256};
    const float *histRanges = {range};
    cv::Mat b_hist, g_hist, r_hist;
    cv::calcHist(&bgr_planes[0], 1, 0, cv::Mat(), b_hist, 1, &histSize, &histRanges, true, false);
    cv::calcHist(&bgr_planes[1], 1, 0, cv::Mat(), g_hist, 1, &histSize, &histRanges, true, false);
    cv::calcHist(&bgr_planes[2], 1, 0, cv::Mat(), r_hist, 1, &histSize, &histRanges, true, false);

    // 归一化
    int hist_h = 400;
    int hist_w = 600;
    int bin_w = hist_w / histSize;
    cv::Mat histImage(hist_w, hist_h, CV_8UC3, cv::Scalar(0, 0, 0));
    cv::normalize(b_hist, b_hist, 0, hist_h, cv::NORM_MINMAX, -1, cv::Mat()); // 归一化到(0, hist_h)
    cv::normalize(g_hist, g_hist, 0, hist_h, cv::NORM_MINMAX, -1, cv::Mat());
    cv::normalize(r_hist, r_hist, 0, hist_h, cv::NORM_MINMAX, -1, cv::Mat());

    // 绘制直方图(直方图坐标和画图坐标不同,画图坐标从左上角开始,即左上角为零点)
    for(int i = 1; i < histSize; i++){
        cv::line(histImage, cv::Point((i - 1) * bin_w, hist_h - cvRound(b_hist.at(i - 1))), 
        cv::Point((i)*bin_w, hist_h - cvRound(b_hist.at(i))), cv::Scalar(255, 0, 0), 2, cv::LINE_AA);

        cv::line(histImage, cv::Point((i - 1) * bin_w, hist_h - cvRound(g_hist.at(i - 1))), 
        cv::Point((i)*bin_w, hist_h - cvRound(g_hist.at(i))), cv::Scalar(0, 255, 0), 2, cv::LINE_AA);

        cv::line(histImage, cv::Point((i - 1) * bin_w, hist_h - cvRound(r_hist.at(i - 1))), 
        cv::Point((i)*bin_w, hist_h - cvRound(r_hist.at(i))), cv::Scalar(0, 0, 255), 2, cv::LINE_AA);
    }

    cv::imshow("output", histImage);

    cv::waitKey(0);
    return 0;
}

Opencv(C++)笔记--直方图均衡化、直方图计算_第3张图片

你可能感兴趣的:(Opencv(C++)学习笔记,opencv,计算机视觉,人工智能)