Python之《机器学习系统设计》第十章

第十章:计算机视觉:模式识别

使用mahotas来处理图像

import mahotas as mh
from matplotlib import pyplot as plt
image = mh.imread('003.jpg')
plt.imshow(image)
plt.show()



#从图形减去像素均值
image = image - image.mean()

#阈值 threshold_value
image = mh.colors.rgb2gray(image,dtype=np.uint8) #灰度化
plt.imshow(image)
plt.show()

matplotlib默认会把单通道图像显示为假彩色图像,可以这样显示

#阈值 threshold_value
image = mh.colors.rgb2gray(image,dtype=np.uint8) #灰度化
plt.imshow(image)
plt.gray()
plt.show()


#二值化
thresh = mh.thresholding.otsu(image)
plt.imshow(image>thresh)
plt.gray()
plt.show()



#close 操作
otsubin = (image<=thresh)
otsubin = mh.close(otsubin,np.ones((15,15)))

#另一种阈值方法
thresh = mh.thresholding.rc(image)


#高斯模糊
#image = mh.colors.rgb2gray(image)
#im8 = mh.gaussian_filter(image,8) #8x8的高斯模糊

#读入lena
lenna = mh.imread('lena.jpg',as_grey=True)
#加入椒盐噪声
salt = np.random.random(lenna.shape)>.975
pepper = np.random.random(lenna.shape)>.975
lenna = mh.stretch(lenna)
lenna = np.maximum(salt*170,sep)
lenna = np.minimun(pepper*30 + lenna*(~pepper),lenna)
plt.imshow(image>thresh)
plt.show()

用lena来做滤波

原图如下




读入lena图像,针对三个通道进行高斯滤波再合并

# Read in the image
im = mh.demos.load('lena')

# This breaks up the image into RGB channels
r, g, b = im.transpose(2, 0, 1)
h, w = r.shape

# smooth the image per channel:
r12 = mh.gaussian_filter(r, 12.)
g12 = mh.gaussian_filter(g, 12.)
b12 = mh.gaussian_filter(b, 12.)

# build back the RGB image
im12 = mh.as_rgb(r12, g12, b12)

mgrid返回一个(h,w)的数组,里面的值分别对应与y和x的坐标

X, Y = np.mgrid[:h, :w]
X = X - h / 2. #将中心移到h/2
Y = Y - w / 2.
X /= X.max() #归一化到-1...+1
Y /= Y.max()

# Array C will have the highest values in the center, fading out to the edges:

C = np.exp(-2. * (X ** 2 + Y ** 2)) #用高斯函数赋予中心区域一个高权重
C -= C.min() #再次归一化
C /= C.ptp()
C = C[:, :, None] #会在C里增加一个虚设的第三维度

# The final result is sharp in the centre and smooths out to the borders:
ring = mh.stretch(im * C + (1 - C) * im12) #将两幅图像组合起来,让中间成为焦点
mh.imsave('lena-ring.jpg', ring)




作者提供的同于分类的几个实例

# This code is supporting material for the book
# Building Machine Learning Systems with Python
# by Willi Richert and Luis Pedro Coelho
# published by PACKT Publishing
#
# It is made available under the MIT License

import mahotas as mh
from sklearn import cross_validation
from sklearn.linear_model.logistic import LogisticRegression
import numpy as np
from glob import glob
from features import texture, edginess_sobel

basedir = '../SimpleImageDataset/'


haralicks = []
sobels = []
labels = []

print('This script will test (with cross-validation) classification of the simple 3 class dataset')
print('Computing features...')
# Use glob to get all the images
images = glob('{}/*.jpg'.format(basedir))

# We sort the images to ensure that they are always processed in the same order
# Otherwise, this would introduce some variation just based on the random
# ordering that the filesystem uses
for fname in sorted(images):
    im = mh.imread(fname, as_grey=True)
    haralicks.append(texture(im))
    sobels.append(edginess_sobel(im))

    # Files are named like building00.jpg, scene23.jpg...
    labels.append(fname[:-len('xx.jpg')])

print('Finished computing features.')

haralicks = np.array(haralicks)
sobels = np.array(sobels)
labels = np.array(labels)

# We use logistic regression because it is very fast.
# Feel free to experiment with other classifiers
scores = cross_validation.cross_val_score(
    LogisticRegression(), haralicks, labels, cv=5)
print('Accuracy (5 fold x-val) with Logistic Regression [std features]: {}%'.format(
    0.1 * round(1000 * scores.mean())))

haralick_plus_sobel = np.hstack([np.atleast_2d(sobels).T, haralicks])
scores = cross_validation.cross_val_score(
    LogisticRegression(), haralick_plus_sobel, labels, cv=5).mean()
print('Accuracy (5 fold x-val) with Logistic Regression [std features + sobel]: {}%'.format(
    0.1 * round(1000 * scores.mean())))


# We can try to just use the sobel feature. The result is almost completely
# random.
scores = cross_validation.cross_val_score(
    LogisticRegression(), np.atleast_2d(sobels).T, labels, cv=5).mean()
print('Accuracy (5 fold x-val) with Logistic Regression [only using sobel feature]: {}%'.format(
    0.1 * round(1000 * scores.mean())))


# This code is supporting material for the book
# Building Machine Learning Systems with Python
# by Willi Richert and Luis Pedro Coelho
# published by PACKT Publishing
#
# It is made available under the MIT License

from __future__ import print_function
import mahotas as mh
from sklearn import cross_validation
from sklearn.linear_model.logistic import LogisticRegression
from matplotlib import pyplot as plt
import numpy as np
from glob import glob

basedir = 'AnimTransDistr'
print('This script will test classification of the AnimTransDistr dataset')


def features_for(images):
    fs = []
    for im in images:
        im = mh.imread(im, as_grey=True).astype(np.uint8)
        fs.append(mh.features.haralick(im).mean(0))
    return np.array(fs)


def features_labels(groups):
    labels = np.zeros(sum(map(len, groups)))
    st = 0
    for i, g in enumerate(groups):
        labels[st:st + len(g)] = i
        st += len(g)
    return np.vstack(groups), labels

classes = [
    'Anims',
    'Cars',
    'Distras',
    'Trans',
]

print('Computing whole-image texture features...')
features = []
labels = []
for ci, cl in enumerate(classes):
    images = glob('{}/{}/*.jpg'.format(basedir, cl))
    features.extend(features_for(images))
    labels.extend([ci for _ in images])

features = np.array(features)
labels = np.array(labels)

scores0 = cross_validation.cross_val_score(
    LogisticRegression(), features, labels, cv=10)
print('Accuracy (5 fold x-val) with Logistic Regrssion [std features]: %s%%' % (
    0.1 * round(1000 * scores0.mean())))

tfeatures = features

from sklearn.cluster import KMeans
from mahotas.features import surf

images = []
labels = []

for ci, cl in enumerate(classes):
    curimages = glob('{}/{}/*.jpg'.format(basedir, cl))
    images.extend(curimages)
    labels.extend([ci for _ in curimages])
labels = np.array(labels)

print('Computing SURF descriptors...')
alldescriptors = []
for im in images:
    im = mh.imread(im, as_grey=1)
    im = im.astype(np.uint8)

    # To use dense sampling, you can try the following line:
    # alldescriptors.append(surf.dense(im, spacing=max(im.shape)//32))
    alldescriptors.append(surf.surf(im, descriptor_only=True))

print('Descriptors done')
k = 256
km = KMeans(k)

concatenated = np.concatenate(alldescriptors)
concatenated = concatenated[::64]
print('Clustering with K-means...')
km.fit(concatenated)
features = []
for d in alldescriptors:
    c = km.predict(d)
    features.append(
        np.array([np.sum(c == i) for i in range(k)])
    )
features = np.array(features)
print('predicting...')
scoreSURFlr = cross_validation.cross_val_score(
    LogisticRegression(), features, labels, cv=5).mean()
print('Accuracy (5 fold x-val) with Log. Reg [SURF features]: %s%%' % (
    0.1 * round(1000 * scoreSURFlr.mean())))

print('Attemping classification with all features combined...')
allfeatures = np.hstack([features, tfeatures])
scoreSURFplr = cross_validation.cross_val_score(
    LogisticRegression(), allfeatures, labels, cv=5).mean()

print('Accuracy (5 fold x-val) with Log. Reg [All features]: %s%%' % (
    0.1 * round(1000 * scoreSURFplr.mean())))

plt.plot([0, 1, 2], 100 *
         np.array([scores0.mean(), scoreSURFlr, scoreSURFplr]), 'k-', lw=8)
plt.plot(
    [0, 1, 2], 100 * np.array([scores0.mean(), scoreSURFlr, scoreSURFplr]),
    'o', mec='#cccccc', mew=12, mfc='white')
plt.xlim(-.5, 2.5)
plt.ylim(scores0.mean() * 90., scoreSURFplr * 110)
plt.xticks([0, 1, 2], ["Texture", "SURF", "combined"])
plt.ylabel('Accuracy (%)')
plt.savefig('../1400OS_10_18+.png')











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值