• 13---OpenCV:图像进阶操作之①图像直方图②图像金字塔


    一、图像直方图

            在分析图像、物体和视频信息的时候,我们经常用直方图来表达我们关注的信息。直方图在计算机视觉中应用广泛。例如,通过判断帧与帧之间边缘和颜色的统计量是否出现巨大变化,来检测视频中场景的变换数字图像处理中,通常使用的是灰度直方图,灰度直方图是一种计算代价非常小但很有用的工具,它概括了一幅图像的灰度级信息灰度直方图是图像灰度级的函数,用来描述每个灰度级在图像矩阵中的像素个数或者占有率。直方图分布较广较均匀的图像对比度高,视觉效果好。

    API介绍

    1. void calcHist( const Mat* images, int nimages,const int* channels, InputArray mask,OutputArray hist, int dims, const int* histSize,const float** ranges, bool uniform = true, bool accumulate = false );
    2. /*******************************************************************
    3. * images: 输入图
    4. * nimages:   输入图个数
    5. * channels: 统计第几通道
    6. * mask: 掩膜
    7. * hist:   数组存储输出值
    8. * dims: 输出直方图维度
    9. * histSize: 直方图区间
    10. * ranges: 统计像素区间
    11. * uniform:   直方图数组是否归一化处理
    12. * accumulate:   多图时是否累计计算
    13. *********************************************************************/
    1. void calcHist( const Mat* images, int nimages, const int* channels, InputArray mask,SparseMat& hist, int dims,const int* histSize, const float** ranges,bool uniform = true, bool accumulate = false );
    2. /*******************************************************************
    3. * images: 输入图
    4. * nimages:   输入图个数
    5. * channels: 统计第几通道
    6. * mask: 掩膜
    7. * hist:   图像存储输出值
    8. * dims: 输出直方图维度
    9. * histSize: 直方图区间
    10. * ranges: 统计像素区间
    11. * uniform:   直方图数组是否归一化处理
    12. * accumulate:   多图时是否累计计算
    13. *********************************************************************/
    1. void calcHist( InputArrayOfArrays images,const std::vector<int>& channels,InputArray mask, OutputArray hist,const std::vector<int>& histSize,const std::vector<float>& ranges,bool accumulate = false );
    2. /*******************************************************************
    3. * images: 输入图
    4. * channels: 变换矩阵
    5. * mask: 输出图大小
    6. * hist:   数组存储输出值
    7. * histSize: 直方图区间
    8. * ranges: 统计像素区间
    9. * accumulate:   多图时是否累计计算
    10. *********************************************************************/

    综合代码

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. using namespace std;
    7. using namespace cv;
    8. class Hist
    9. {
    10. public:
    11. Hist(string url = "./mm.jpg") :img(imread(url))
    12. {
    13. hist["img"] = img;
    14. }
    15. void TestCalcHist()
    16. {
    17. split(img, bgr);
    18. //准备直方图的参数
    19. Mat Blue, Green, Red;
    20. const int channerl[] = { 0 };
    21. const int histSize[] = { 256 };
    22. float rang[] = { 0,255 };
    23. const float* ranges[] = { rang };
    24. calcHist(&bgr[0], 1, channerl, Mat(), Blue, 1, histSize, ranges);
    25. calcHist(&bgr[1], 1, channerl, Mat(), Green, 1, histSize, ranges);
    26. calcHist(&bgr[2], 1, channerl, Mat(), Red, 1, histSize, ranges);
    27. //绘制直方图
    28. int width = 500;
    29. int height = 300;
    30. hist["hist"] = Mat(height, width, CV_8UC3, Scalar(0, 0, 0));
    31. //归一化处理
    32. normalize(Blue, Blue, 0, 300, NORM_MINMAX);
    33. normalize(Green, Green, 0, 300, NORM_MINMAX);
    34. normalize(Red, Red, 0, 300, NORM_MINMAX);
    35. //比例问题
    36. int step = cvRound(float(width) / float(256));   //每一个点的占比
    37. for (int i = 1; i < 256; i++)
    38. {
    39. line(hist["hist"], Point(step * (i - 1), height - cvRound(Blue.at<float>(i - 1))), Point(step * (i), height - cvRound(Blue.at<float>(i))),Scalar(255,0,0));
    40. line(hist["hist"], Point(step * (i - 1), height - cvRound(Blue.at<float>(i - 1))), Point(step * (i), height - cvRound(Green.at<float>(i))), Scalar(0, 255, 0));
    41. line(hist["hist"], Point(step * (i - 1), height - cvRound(Blue.at<float>(i - 1))), Point(step * (i), height - cvRound(Red.at<float>(i))), Scalar(0, 0, 255));
    42. }
    43. }
    44. void Show()
    45. {
    46. for (auto& v : hist)
    47. {
    48. imshow(v.first, v.second);
    49. }
    50. waitKey(0);
    51. }
    52. protected:
    53. Mat img;
    54. map hist;
    55. vector bgr;
    56. };
    57. int main()
    58. {
    59. unique_ptr p(new Hist);
    60. p->TestCalcHist();
    61. p->Show();
    62. return 0;
    63. }

    二、图像金字塔

    图像金字塔有两种,

    • 高斯金字塔:高斯金字塔用来向下采样

    • 拉普拉斯金字塔:图像底层图像重建上层未采样图像

    在数据图像处理中,就是数据残差,可以对图像进行最大程度的还原,配合高斯金字塔—起使用。

    高斯金字塔

    高斯金字塔是由底部的最大分辨率图像逐次向下采样得到的一系列图像。最下面的图像分辨率最高,越往上图像分辨率越低。高斯金字塔的向下采样过程是:对于给定的图像先做一次高斯平滑处理,也就是使用一个卷积核对图像进行卷积操作,再对图像采样,去除图像中的偶数行和偶数列,然后就得到一幅图片,对这幅图片再进行上述操作,就可以得到高斯金字塔。

    拉普拉斯金字塔

    拉普拉斯金字塔与高斯金字塔正好相反,高斯金字塔通过底层图像构建上层图像,而拉普拉斯是通过上层小尺寸的图像构建下层大尺寸的图像。主要应用于图像融合,即是通过残差来还原原图。

    图像向上采样是由小图像不断放大的过程。它将图像在每个方向上扩大为原图像的⒉倍,新增的行和列均用0来填充,并使用与“向下采样”相同的卷积核乘以4,再与放大后的图像进行卷积运算,以获得“新增像素”的新值。所有元素都被规范化为4,而不是1。值得注意的是,放大后的图像比原始图像要模糊。

     API介绍

    1. //向下采样
    2. void pyrDown( InputArray src, OutputArray dst,const Size& dstsize = Size(), int borderType = BORDER_DEFAULT );
    3. /*******************************************************************
    4. * src: 输入图
    5. * dst: 输出图
    6. * dstsize: 卷积核大小
    7. * borderType: 边界模式
    8. *********************************************************************/
    9. //向上采样
    10. void pyrUp( InputArray src, OutputArray dst,const Size& dstsize = Size(), int borderType = BORDER_DEFAULT );
    11. /*******************************************************************
    12. * src: 输入图
    13. * dst: 输出图
    14. * dstsize: 卷积核大小
    15. * borderType: 边界模式
    16. *********************************************************************/

    综合代码

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. using namespace cv;
    7. class Pyr
    8. {
    9. public:
    10. Pyr() :img(imread("20.jpg"))
    11. {
    12. pyr["img"] = img;
    13. }
    14. void TestPyrDown()
    15. {
    16. pyrDown(img, pyr["d1"]);
    17. pyrDown(pyr["d1"], pyr["d2"]);
    18. pyrDown(pyr["d2"], pyr["d3"]);
    19. }
    20. void TestPyrUp()
    21. {
    22. pyrUp(img, pyr["u1"]);
    23. pyrUp(pyr["u1"], pyr["u2"]);
    24. //pyrUp(pyr["u2"], pyr["u3"]);
    25. }
    26. void Show()
    27. {
    28. int i = 0;
    29. for (auto& v : pyr)
    30. {
    31. imshow(v.first, v.second);
    32. moveWindow(v.first, i++ * 100, 100);
    33. }
    34. waitKey(0);
    35. }
    36. protected:
    37. Mat img;
    38. map pyr;
    39. };
    40. int main()
    41. {
    42. unique_ptr p(new Pyr);
    43. p->TestPyrUp();
    44. p->TestPyrDown();
    45. p->Show();
    46. return 0;
    47. }

     

  • 相关阅读:
    PyG 使用过程中出现的一些小bug。GAE 的negative_sampling中
    Microsoft Certified Systems Engineer 词汇表
    【爬虫】(一)fossies.org
    Web自动化Selenium-键盘操作
    做亚马逊测评有哪些需要注意的?
    ssm网上书城系统毕业设计-附源码180919
    vue中获取复选框是否被选中的值、如何用JavaScript判断复选框是否被选中
    【Vue 基础知识】v-for的使用和注意事项
    图像超分——Real-ESRGAN快速上手
    商城项目11_商品SPU、SKU、详解表结构、属性分组列表展示、修改、新增、分类级联更新
  • 原文地址:https://blog.csdn.net/zjjaibc/article/details/126667588