python代码实现对RGB图片进行费歇尔(Fisher)二类分类

'''
1.设置样本数据,设置分类标签 水果--1 背景--0
2.求两类样本的均值向量
3.求两类样本的协方差矩阵
4.求 w
5.求 f(水果) f(背景)
6.求 d
7.读入测试图像,求解RGB
8.对图像的每一个像元进行分类,转换RGB借鉴贝叶斯分类
'''

import cv2
import numpy as np
import math
from  copy import deepcopy

# 设置样本数据
def Dataset():
    # 25 个样本
    samples_data = [[216,114,74],[215,113,73],[214,112,72],
                   [214,112,72],[214,112,72],[156,93,62],
                   [174,113,84],[176,115,86],[184,125,95],
                   [186,127,97],[180,123,94],[184,127,97],
                   [187,130,101],[180,123,93],[186,129,99],
                   [180,173,163],[184,177,167],[183,179,168],
                   [194,205,209] ,[177,175,163],[192,203,207],
                   [176,172,163],[178,174,165],[179,175,166],
                   [179,175,166]]
    class_lable = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0]  # 水果--1 背景--0
    return samples_data, class_lable

# 分别得到水果和背景的RGB集
def Get_ClassRGB(samples_data, class_lable):
    L = len(samples_data)
    m = 0
    n = 0
    for i in range(L):
        if class_lable[i] == 1:
            m += 1
        else:
            n += 1
    sample1 = [[0,0,0] for i in range(m)]
    sample0 = [[0,0,0] for i in range(n)]
    for i in range(L):
        if i < m:
            sample1[i] = samples_data[i]
        else:
            sample0[i-m] = samples_data[i]
    return sample1, sample0

# 并把每个像元转为3行1列的向量
def Get_Change_xl(sample1, sample0):
    L1 = len(sample1)
    L0 = len(sample0)
    ve1 = [[[0],[0],[0]] for i in range(L1)]
    ve0 = [[[0],[0],[0]] for i in range(L0)]
    for i in range(L1):
        for j in range(3):
            ve1[i][j][0] = sample1[i][j]

    for i in range(L0):
        for j in range(3):
            ve0[i][j][0] = sample0[i][j]

    return ve1,ve0

# 求RGB均值向量
def Get_Junzhi(samples_data,class_lable):
    L = len(samples_data)
    m = 0  # 水果
    n = 0  # 背景
    r1 = 0
    g1 = 0
    b1 = 0
    r0 = 0
    g0 = 0
    b0 = 0
    mv = [0]
    mean_vector1 = [mv]*3
    mean_vector0 = [mv]*3
    for i in range(L):
        if class_lable[i] == 1:
            m += 1
        else:
            n += 1

    for i in range(L):
        if i < m:
            r1 += samples_data[i][0]
            g1 += samples_data[i][1]
            b1 += samples_data[i][2]
        else:
            r0 += samples_data[i][0]
            g0 += samples_data[i][1]
            b0 += samples_data[i][2]
    mean_vector1 = [[int(r1/m)],[int(g1/m)],[int(b1/m)]]  # 三维均值向量
    mean_vector0 = [[int(r0/n)],[int(g0/n)],[int(b0/n)]]
    print(mean_vector1)
    print(mean_vector0)
    return mean_vector1,mean_vector0

# 求协方差矩阵
def Get_Cov(samples_data,mean_vector1,mean_vector0):
    L= len(samples_data)
    m = 15
    n = 6
    cov = [0]*3
    Cov_1 = [cov]*3
    Cov_0 = [cov]*3
    cov_bb1 = 0
    cov_gb1 = 0
    cov_gg1 = 0
    cov_rb1 = 0
    cov_rg1 = 0
    cov_rr1 = 0
    cov_bb0 = 0
    cov_gb0 = 0
    cov_gg0 = 0
    cov_rb0 = 0
    cov_rg0 = 0
    cov_rr0 = 0
    for i in range(L):
        if i < m:
            cov_rr1 += (samples_data[i][0]-mean_vector1[0][0])*(samples_data[i][0]-mean_vector1[0][0])
            cov_rg1 += (samples_data[i][0]-mean_vector1[0][0])*(samples_data[i][1]-mean_vector1[1][0])
            cov_rb1 += (samples_data[i][0]-mean_vector1[0][0])*(samples_data[i][2]-mean_vector1[2][0])
            cov_gg1 += (samples_data[i][1]-mean_vector1[1][0])*(samples_data[i][1]-mean_vector1[1][0])
            cov_gb1 += (samples_data[i][1]-mean_vector1[1][0])*(samples_data[i][2]-mean_vector1[2][0])
            cov_bb1 += (samples_data[i][2]-mean_vector1[2][0])*(samples_data[i][2]-mean_vector1[2][0])
        else:
            cov_rr0 += (samples_data[i][0] - mean_vector0[0][0]) * (samples_data[i][0] - mean_vector0[0][0])
            cov_rg0 += (samples_data[i][0] - mean_vector0[0][0]) * (samples_data[i][1] - mean_vector0[1][0])
            cov_rb0 += (samples_data[i][0] - mean_vector0[0][0]) * (samples_data[i][2] - mean_vector0[2][0])
            cov_gg0 += (samples_data[i][1] - mean_vector0[1][0]) * (samples_data[i][1] - mean_vector0[1][0])
            cov_gb0 += (samples_data[i][1] - mean_vector0[1][0]) * (samples_data[i][2] - mean_vector0[2][0])
            cov_bb0 += (samples_data[i][2] - mean_vector0[2][0]) * (samples_data[i][2] - mean_vector0[2][0])
    a = m-1
    b = n-1
    Cov_1 = [[cov_rr1/a,cov_rg1/a,cov_rb1/a],[cov_rg1/a,cov_gg1/a,cov_gb1/a],[cov_rb1/a,cov_gb1/a,cov_bb1/a]]
    Cov_0 = [[cov_rr0/b,cov_rg0/b,cov_rb0/b],[cov_rg0/b,cov_gg0/b,cov_gb0/b],[cov_rb0/b,cov_gb0/b,cov_bb0/b]]
    return Cov_1, Cov_0

# 求 w  返回w
def Get_W(Cov_1, Cov_0, mean_vector1, mean_vector0):

    a = [[[0],[0],[0]] for i in range(3)]
    b = [[0],[0],[0]]

    # 两样本协方差之差
    for i in range(3):
        for j in range(3):
           a[i][j] = Cov_1[i][j] + Cov_0[i][j]
    # 求两协方差之差的 逆矩阵
    Inver = np.linalg.inv(a)
    # 两均值向量的差
    for i in range(3):
        for j in range(1):
            b[i][j] = mean_vector1[i][j] - mean_vector0[i][j]
    # 求 W
    W = np.dot(Inver, b)
    return W

# 求 f(x)
def Get_F(data, W):
    # 求W的转置
    W_T = [[0,0,0]]
    for i in range(3):
        for j in range(1):
            W_T[j][i] = W[i][j]
    F = 0.0
    for i in range(len(data)):
        for j in range(3):
            F += (W_T[i-i][j] * np.mat(data[i][j]))
    # print(F1)
    return F

# 改变图像RGB存储形式
# 编成3行1列形式
def Get_RGB(image):
    w = image.shape[0]
    h = image.shape[1]
    data = []
    ve = [[0] for i in range(3)]
    new_data = [ve for i in range(w*h)]
    for i in range(w):
        for j in range(h):
            for k in range(1):  # B G 调换
                a = image[i,j,k+0]
                image[i,j,k+0] = image[i,j,k+2]
                image[i,j,k+2] = a
    # print(image)
    for i in range(w):
        for j in range(h):
            new_data[i*h+j][0][0] = image[i][j][0]
            new_data[i*h+j][1][0] = image[i][j][1]
            new_data[i*h+j][2][0] = image[i][j][2]
            V = deepcopy(ve)
            data.append(V)
    return data

#实现费歇尔分类
def Get_Fisher(test_data, image, d, W):
    w = image.shape[0]
    h = image.shape[1]
    L = len(test_data)

    for i in range(w):
        for j in range(h):
            a = [test_data[i*h+j]]
            F_X = Get_F(a, W)
            if F_X >= d:
                image[i][j][0] = 74  # 74,114,216 这里图片是RGB形式
                image[i][j][1] = 114  # 赋值时需要注意
                image[i][j][2] = 216
            else:
                image[i][j] = 255

    return image


sample_data, class_lable = Dataset()
sample1, sample0 = Get_ClassRGB(sample_data, class_lable)
mv1, mv0 = Get_Junzhi(sample_data, class_lable)
cov_1, cov_0 = Get_Cov(sample_data, mv1, mv0)
W = Get_W(cov_1, cov_0, mv1, mv0)
data1, data0 = Get_Change_xl(sample1, sample0)
F1 = Get_F(data1, W)
F0 = Get_F(data0, W)
# d = 0.5*(F1/15 + F0/10)
# d = (15 * F1 + 10 * F0) / (15 + 10)
d = ( F1 +  F0) / (15 + 10)

# a = [[[173],[171],[159]]]
# F = Get_F(a, W)

image =cv2.imread('testJuzi1.jpg')
# 改变RGB存储形式
test_data = Get_RGB(image)
Fisher_image = Get_Fisher(test_data, image, d, W)
cv2.imshow('Fiher_image', Fisher_image)
cv2.waitKey(0)
cv2.destroyAllWindows()



'''
sample_data, class_lable = Dataset()
sample1, sample0 =Get_ClassRGB(sample_data, class_lable)
mv1, mv0 = Get_Junzhi(sample_data, class_lable)
cov_1, cov_0 = Get_Cov(sample_data,mv1,mv0)
W = Get_W(cov_1, cov_0, mv1, mv0)
data1, data0 = Get_Change_xl(sample1,sample0)
d = Get_F(data1, data0, W)

print(mv1)
print(mv0)
print(cov_1)
print(cov_0)
print(W)
print(data1)
print(data0)
print(d)
'''


 

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读