图像主题色 代码 --code, Windows

来源:互联网 发布:数据库设计与关系理论 编辑:程序博客网 时间:2024/06/13 20:41

                                                                                                           图像主题色提取算法——code


本文主要任务: run code

  • 配置: Windows, anoconda python 2.7
  • 源码  https://github.com/rainyear/ImageColorTheme
  • run code :  直接运行 test.py 文件 , 主函数如下:
  • if __name__ == '__main__':    # testColorSpace()    # testMMCQ()    # kmvs()    print(testKmeans([getPixData()], 7, False))    print(testKmeans([getPixData()], 7))

                                                                                                                                                                                                                                                                
  • 运行源test.py chuxian错误:
  1.   无法 import cv2 模块 
    • 解决办法:Windows 上安装 opencv 最简单的办法就是, 从opencv 官网下载源码,解压后将cv2.pyd 文件复制到python的 site-packages 目录下. 类似于下图所示:                                                                                                                                                                                                                                                                                                                                            

         2.   error :    SyntaxError: Non-ASCII character '\xe5'in file ict\MMCQ.py on line 36, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details  

    • 解决办法:在相应文件的第一行加入         # -*- coding: utf-8 -*-       
    • 原因是:(1). Python默认是以ASCII作为编码方式的,需要在文件开头设置一下编码.                                                                                                              (2). 可能是因为我在代码中加入了中文注释,所以出现编码问题.                                        
      # -*- coding: UTF-8 -*-     或者 #coding=utf-8 
    •                                                                                                                                                                                                                                                                                                                                                                                    

        3.  error:    from queue import PriorityQueue as PQueue ImportError: No module named queue         

    • 解决办法: 将代码修改,如下图所示 
    • 原因:  Queue 在 multiprocessing module 中
    • 源代码与修改后代码对比, 如下图                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        源代码
    •                                                                                                                                                                                                                                                                                                修改后                                                                                                                                                                                                                                                                                                      

      4.   error:  time 有关问题

    •  解决办法:  将文件test.py 里的  testMMCQ(pixDatas, maxColor),  testOQ(pixDatas, maxColor),   testKmeans(pixDatas, maxColor, skl=True), 三个函数里的 start  = time.process_time()  ,  print("MMCQ Time cost: {0}".format(time.process_time() - start))  两句话修改如下:                                                                         
    • def testMMCQ(pixDatas, maxColor):    #start  = time.process_time()    start = time.time()    themes = list(map(lambda d: MMCQ(d, maxColor).quantize(), pixDatas))    #print("MMCQ Time cost: {0}".format(time.process_time() - start))    print("MMCQ Time cost: {0}".format(time.time() - start))    return themes

      至此源test.py 文件可以运行成功!!!!!!!!!!!!!!!奋斗奋斗奋斗奋斗                                                                                                                                                              
  •  main() 函数加入其它函数,跑出 主题色:
    • def OQvs():    imgs = map(lambda i: 'imgs/photo%s.jpg' % i, range(1,5))    pixDatas = list(map(getPixData, imgs))    maxColor = 7    themes =  [testOQ(pixDatas, maxColor)]    imgPalette(pixDatas, themes, ["OQ Palette"])    def kmvs():    imgs = map(lambda i: 'imgs/photo%s.jpg' % i, range(1,5))    pixDatas = list(map(getPixData, imgs))    maxColor = 7    themes = [testKmeans(pixDatas, maxColor), testKmeans(pixDatas, maxColor, False)]    imgPalette(pixDatas, themes, ["KMeans Palette", "KMeans DIY"])def MMCQvs():    imgs = map(lambda i: 'imgs/photo%s.jpg' % i, range(1,5))    pixDatas = list(map(getPixData, imgs))    maxColor = 7    themes =  [testMMCQ(pixDatas, maxColor)]    imgPalette(pixDatas, themes, ["MMCQ Palette"])    def vs():    imgs = map(lambda i: 'imgs/photo%s.jpg' % i, range(1,5))    pixDatas = list(map(getPixData, imgs))    maxColor = 7    themes = [testMMCQ(pixDatas, maxColor), testOQ(pixDatas, maxColor), testKmeans(pixDatas, maxColor)]    imgPalette(pixDatas, themes, ["MMCQ Palette", "OQ Palette", "KMeans Palette"])if __name__ == '__main__':    testColorSpace()    # testMMCQ()    # print(testKmeans([getPixData()], 7, False))    # print(testKmeans([getPixData()], 7))    # correct code begin    kmvs()    MMCQvs()     vs()


    • error:      pale[y,:,:] = np.array(theme[int(y / ph)], dtype=np.uint8) IndexError: list index out of range
    • 解决办法:修改test.py 文件中的 imgPalette(imgs, themes, titles) 函数,修改如下
    • def imgPalette(imgs, themes, titles):    N = len(imgs)    fig = plt.figure()    gs  = gridspec.GridSpec(len(imgs), len(themes)+1)    print(N)    for i in range(N):        print(i)        im = fig.add_subplot(gs[i, 0])        im.imshow(imgs[i])        im.set_title("Image %s" % str(i+1))        im.xaxis.set_ticks([])        im.yaxis.set_ticks([])        t = 1                for themeLst in themes:            theme = themeLst[i]            pale = np.zeros(imgs[i-1].shape, dtype=np.uint8)            h, w, _ = pale.shape            ph  = h / len(theme)            for y in range(h):                pale[y,:,:] = np.array(theme[int(y / ph)-1], dtype=np.uint8)            pl = fig.add_subplot(gs[i, t])            pl.imshow(pale)            pl.set_title(titles[t-1])            pl.xaxis.set_ticks([])            pl.yaxis.set_ticks([])            t += 1                plt.show()




                          

   

                         

                      

                        

    







原创粉丝点击