我如何使用奇异值分解压缩图像?

问题描述 投票:0回答:1
import matplotlib.pyplot as plt
from skimage import io
import numpy as np
%matplotlib inline

# Defining the load and display functions
def load(path):
    out = io.imread(path)
    out = out.astype(np.float64)/255
    return out

def display(image):
    plt.figure(figsize = (10, 10))
    plt.axis('off')
    plt.imshow(image)

def get_rgb(image, channel):
    out = image.copy()
    red = out[:, :, 0].copy()
    green = out[:, :, 1].copy()
    blue = out[:, :, 2].copy()
    if channel == 'R':
        return red
    elif channel == 'G':
        return red
    elif channel == 'B':
        return blue
    return (red, green, blue)

def svd_compression(image, rank:int):
    row, col, dim = image.shape
    red = get_rgb(image, 'R')
    blue = get_rgb(image, 'B')
    green = get_rgb(image, 'G')
    channels = (red, green, blue)

    image = np.zeros((row, col, 3))
    subimages = []
    for colour in channels:
        u, s, v = get_svd(colour)
        u = u[:, :rank]
        s = s[:rank]
        v = v[:rank, :]
        subimages.append(np.linalg.multi_dot((u, np.diag(s), v)))
    image[: ,:, 0] = subimages[0]
    image[:, :, 1] = subimages[1]
    image[:, :, 2] = subimages[2]
    return image

我尝试压缩一些图像,并且在压缩图像时,图像中的颜色变得不同,压缩时不应发生这种情况。有人会建议对此代码进行任何更改吗? (我用此图像https://images.app.goo.gl/LDs7EihDzuqpXeeM8尝试过)

python matlab image-processing compression svd
1个回答
0
投票

由于代码中有一个小错误,在get_rgb channel

© www.soinside.com 2019 - 2024. All rights reserved.