难得被人求助一次, 这个必须回答一下 不过你的需求确实没有写得太清楚 根据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()
可用sobel算子提取图像的边界。
具体的Matlab程序可为:
I=imread('bonemarrtif');
[BW1,th1]=edge(I,'sobel',007);
th1str=num2str(th1)
imshow(I);
title('图1:bonemarrtif原图','fontsize',14,'position',[128,260,0]);
figure;imshow(BW1);
ti='图8: sobel算子提取的边界,阈值为';
ti=strcat(ti,th1str)
title(ti,'fontsize',12,'position',[128,260,0])
识别里文字的方法,通常称为OCR(Optical
Character
Recognition,光学字符识别),需要利用非常复杂的模式识别算法才能实现。自己用VB开发OCR程序工作量太大。
可以考虑采用OCR插件,即在电脑上安装OCR控件,然后在VB程序中调用该控件。如WPS,紫光、汉王的OCR系统都带有可调用的控件,但都比较大,而且有使用限制。我曾经用过一个免费的91OCR控件,识别效果还马马虎虎可以。
以上就是关于用python K值聚类识别图片主要颜色的程序,算法python代码已经有了全部的内容,包括:用python K值聚类识别图片主要颜色的程序,算法python代码已经有了、最高效的模糊图像识别算法是什么(就是在低端计算机上也能快速是别的那种)、vb编程识别图片中文字等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)