添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
  1. 实验介绍
  2. 实验环境
  3. 实验过程
    1. 查看不同颜色空间的编码
    2. 画出颜色直方图
    3. 比较颜色直方图
  4. 参考

学习颜色空间和颜色直方图 1 2 ,使用OpenCV + Python 3 进行一些小实验。

  • 对图片进行颜色空间的转换

  • 画出图片的颜色直方图

  • 对两张图片的颜色直方图进行比较

  • 操作系统:Ubuntu 14.04.3 LTS

    (刚开始用Windows 10,然后发现用Python的PIL读取jpg文件时得到的RGB编码与Ubuntu下不同,而bmp文件却是一致的。经测试在Ubuntu下用Python得到jpg文件的RGB编码与mspaint一致,故改用Ubuntu。)

  • 开发环境:Python 2.7.6 + OpenCV 2.4.11

    (OpenCV 3.x与OpenCV 2.x略有不同。)

  • Python Library:

  • numpy 1.10.2

  • matplotlib 1.5.0

  • Pillow 3.1.1

  • 查看不同颜色空间的编码

    输入:图片文件路径、颜色空间

    输出:编码

    参考: http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html#converting-colorspaces

  • 读入图片(OpenCV中默认颜色空间为BGR)

  • 转换颜色空间

  • 输出每个通道的编码

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    import cv2

    input_filepath = 'Lenna.png'
    output_filepath = ['R.txt', 'G.txt', 'B.txt']
    img = cv2.imread(input_filepath)
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # imgLAB = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
    for k in range(3):
    fd = open(output_filepath[k], 'w')
    for i in imgRGB[:, :, k]:
    fd.write(' '.join(['{:3}'.format(j) for j in i]) + '\n')
    fd.close()

    画出颜色直方图

    输入:图片文件路径、颜色空间

    输出:颜色直方图

    参考: http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_histograms/py_table_of_contents_histograms/py_table_of_contents_histograms.html#table-of-content-histograms

    http://docs.opencv.org/3.0-beta/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html#histogram-calculation

  • 读入图片 4

  • 转换颜色空间

  • 获得颜色直方图(可量化,注意HSV的H通道的大小是180)

  • 绘制颜色直方图

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    import cv2
    import numpy as np
    import matplotlib.pyplot as plt

    input_filepath = 'Lenna.png'
    img = cv2.imread(input_filepath)
    # RGB
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    hist = [cv2.calcHist([imgRGB], [k], None, [256], [0, 256]) for k in range(3)]
    x = np.arange(256) + 0.5
    plt.subplot(221), plt.imshow(imgRGB)
    plt.subplot(222), plt.bar(x, hist[0], color = 'r', edgecolor = 'r')
    plt.subplot(223), plt.bar(x, hist[1], color = 'g', edgecolor = 'g')
    plt.subplot(224), plt.bar(x, hist[2], color = 'b', edgecolor = 'b')
    plt.show()
    # HSV
    imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    hist = [cv2.calcHist([imgHSV], [0], None, [50], [0, 180]), \
    cv2.calcHist([imgHSV], [1], None, [50], [0, 256])]
    x = np.arange(50) + 0.5
    plt.subplot(211), plt.bar(x, hist[0])
    plt.subplot(212), plt.bar(x, hist[1])
    plt.show()

    比较颜色直方图

    输入:两张图片文件路径、颜色空间、比较方法

    输出:比较结果(一个实数值)

    参考: http://docs.opencv.org/3.0-beta/doc/tutorials/imgproc/histograms/histogram_comparison/histogram_comparison.html#histogram-comparison

  • 转换颜色空间

  • 获得颜色直方图并归一化

  • 比较颜色直方图

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    import cv2
    import numpy as np
    import matplotlib.pyplot as plt

    input_filepath = ['1.jpg', '2.jpg']
    comp_method = [cv2.cv.CV_COMP_CORREL, cv2.cv.CV_COMP_INTERSECT, \
    cv2.cv.CV_COMP_CHISQR, cv2.cv.CV_COMP_BHATTACHARYYA]
    # BGR
    img = [cv2.imread(i) for i in input_filepath]
    hist = [cv2.calcHist([i], [k], None, [256], [0, 256]) for k in range(3) for i in img]
    for i in hist
    for j in i:
    cv2.normalize(j, j)
    hist = [np.mean(i, 0) for i in hist]
    for method in comp_method:
    d = cv2.compareHist(hist[0], hist[1], method)
    print(d)
    # HSV
    imgHSV = [cv2.cvtColor(i, cv2.COLOR_BGR2HSV) for i in img]
    hist = [cv2.calcHist([i], [0, 1], None, [50, 50], [0, 180, 0, 256]) \
    for i in imgHSV]
    for i in hist_set:
    cv2.normalize(i, i)
    for method in comp_method:
    d = cv2.compareHist(hist[0], hist[1], method)
    print(d)
  • 直方图. https://zh.wikipedia.org/wiki/%E7%9B%B4%E6%96%B9%E5%9B%BE#.E9.A2.9C.E8.89.B2.E7.9B.B4.E6.96.B9.E5.9B.BE ↩︎

  • 颜色直方图. http://baike.baidu.com/view/2438797.htm ↩︎

  • OpenCV-Python Tutorials. http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_tutorials.html ↩︎

  • Lenna. https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png ↩︎

  • http://blog.csdn.net/lijiancheng0614

    http://blog.sina.com.cn/lijiancheng5201314

    http://www.douban.com/people/lijiancheng0614

    https://zh.wikipedia.org/wiki/User:Lijiancheng0614

    Android作为客户端(client)对服务器(server)进行request操作,并从服务器中得到response。

    这里主要使用HTTP来进行数据传输,request有GET和POST两种方式。

    完整的项目: https://github.com/lijiancheng0614/android-NetworkConnect