用python K值聚类识别图片主要颜色的程序,算法python代码已经有了

用python K值聚类识别图片主要颜色的程序,算法python代码已经有了,第1张

难得被人求助一次, 这个必须回答一下 不过你的需求确实没有写得太清楚 根据k值算法出来的是主要颜色有三个, 所以我把三个颜色都打在记事本里了 如果和你的需求有误, 请自行解决吧

另外这里需要用到numpy的库, 希望你装了, 如果没装, 这个直接安装也比较麻烦, 可以看一下portablepython的绿色版。

代码如下:

# -- coding: utf-8 --

import Image

import random

import numpy

class Cluster(object):

    def __init__(self):

        selfpixels = []

        selfcentroid = None

    def addPoint(self, pixel):

        selfpixelsappend(pixel)

    def setNewCentroid(self):

        R = [colour[0] for colour in selfpixels]

        G = [colour[1] for colour in selfpixels]

        B = [colour[2] for colour in selfpixels]

        R = sum(R) / len(R)

        G = sum(G) / len(G)

        B = sum(B) / len(B)

        selfcentroid = (R, G, B)

        selfpixels = []

        return selfcentroid

class Kmeans(object):

    def __init__(self, k=3, max_iterations=5, min_distance=50, size=200):

        selfk = k

        selfmax_iterations = max_iterations

        selfmin_distance = min_distance

        selfsize = (size, size)

    def run(self, image):

        selfimage = image

        selfimagethumbnail(selfsize)

        selfpixels = numpyarray(imagegetdata(), dtype=numpyuint8)

        selfclusters = [None for i in range(selfk)]

        selfoldClusters = None

        randomPixels = randomsample(selfpixels, selfk)

        for idx in range(selfk):

            selfclusters[idx] = Cluster()

            selfclusters[idx]centroid = randomPixels[idx]

        iterations = 0

        while selfshouldExit(iterations) is False:

            selfoldClusters = [clustercentroid for cluster in selfclusters]

            print iterations

            for pixel in selfpixels:

                selfassignClusters(pixel)

            for cluster in selfclusters:

                clustersetNewCentroid()

            iterations += 1

        return [clustercentroid for cluster in selfclusters]

    def assignClusters(self, pixel):

        shortest = float('Inf')

        for cluster in selfclusters:

            distance = selfcalcDistance(clustercentroid, pixel)

            if distance < shortest:

                shortest = distance

                nearest = cluster

        nearestaddPoint(pixel)

    def calcDistance(self, a, b):

        result = numpysqrt(sum((a - b)  2))

        return result

    def shouldExit(self, iterations):

        if selfoldClusters is None:

            return False

        for idx in range(selfk):

            dist = selfcalcDistance(

                numpyarray(selfclusters[idx]centroid),

                numpyarray(selfoldClusters[idx])

            )

            if dist < selfmin_distance:

                return True

        if iterations <= selfmax_iterations:

            return False

        return True

    # ############################################

    # The remaining methods are used for debugging

    def showImage(self):

        selfimageshow()

    def showCentroidColours(self):

        for cluster in selfclusters:

            image = Imagenew("RGB", (200, 200), clustercentroid)

            imageshow()

    def showClustering(self):

        localPixels = [None]  len(selfimagegetdata())

        for idx, pixel in enumerate(selfpixels):

                shortest = float('Inf')

                for cluster in selfclusters:

                    distance = selfcalcDistance(

                        clustercentroid,

                        pixel

                    )

                    if distance < shortest:

                        shortest = distance

                        nearest = cluster

                localPixels[idx] = nearestcentroid

        w, h = selfimagesize

        localPixels = numpyasarray(localPixels)\

            astype('uint8')\

            reshape((h, w, 3))

        colourMap = Imagefromarray(localPixels)

        colourMapshow()

    

if __name__=="__main__":

    from PIL import Image

    import os

    

    k_image=Kmeans()

    path = r'\\pics\\'

    fp = open('file_colortxt','w')

    for filename in oslistdir(path):

        print path+filename

        try:

            color = k_imagerun(Imageopen(path+filename))

            fpwrite('The color of '+filename+' is '+str(color)+'\n')

        except:

            print "This file format is not support"

    fpclose()

这个就比较简单,一般手机中都有这样的工具的,一般我们使用工具就可以去进行 *** 作,答主就简单和你们分享一些经验吧!

一般我们识别中的文字可以利用手机上的软件去进行识别或者使用一些在线网站去进行文字识别

手机识别文字我们可以借助微信小程序

可以在小程序或者搜索框中搜索迅捷文字识别然后进行点击进入,将自己的照片或者上传上去就可以将进行文字识别了

识别效果图:

当然我们还可以选择进行复制转发以及翻译都是可以的,关键看自己的需要!

除了这些之外,我们还可以利用在线网站去将转换为文字,具体步骤我就不写了,小伙伴们可以自己去试一试!

以上就是关于用python K值聚类识别图片主要颜色的程序,算法python代码已经有了全部的内容,包括:用python K值聚类识别图片主要颜色的程序,算法python代码已经有了、图片识别文字在线怎么识别、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10132477.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存