In the previous article, we learned about the principle of singular value decomposition (SVD) . Today, we will use the singular value decomposition of the matrix to compress the image.
Learn by doing
I made an online image compression application, you can feel it.
The function is very simple, upload the image that needs to be compressed, select the compression ratio, and submit it.
Let's take a look at the implementation process
Compress images with SVD
The principle is simple:
decompose the image into RGB three-color matrices, perform singular value decomposition for each color matrix, and then select a specified number of features to compress the matrix.
core code
You can clone my huggingface for the complete code
Core code 1:
p represents the percentage of singular values. According to the specified definition, the higher the definition, the lower the compression ratio, the more the number of singular values extracted, and the less the picture will be distorted)
def rebuild_img(u, sigma, v, percent):
m = len(u)
n = len(v)
a = np.zeros((m, n))
count = (int)(sum(sigma))
curSum = 0
k = 0
while curSum <= count * percent:
uk = u[:, k].reshape(m, 1)
vk = v[k].reshape(1, n)
a += sigma[k] * np.dot(uk, vk)
curSum += sigma[k]
k += 1
a[a < 0] = 0
a[a > 255] = 255
复制代码
Core code 2: It is mainly to define the implementation of the inderence function and the gradio front end
import os
os.system("pip install --upgrade pip")
os.system("pip install opencv-python-headless")
import cv2
import numpy as np
import gradio as gr
from func import rebuild_img
def inference(img,k):
input_img = cv2.imread(img, cv2.IMREAD_COLOR)
u, sigma, v = np.linalg.svd(input_img[:, :, 0])
R = rebuild_img(u, sigma, v, k)
u, sigma, v = np.linalg.svd(input_img[:, :, 1])
G = rebuild_img(u, sigma, v, k)
u, sigma, v = np.linalg.svd(input_img[:, :, 2])
B = rebuild_img(u, sigma, v, k)
restored_img = np.stack((R, G, B), 2)
return Image.fromarray(restored_img[:, :, ::-1])
gr.Interface(
inference,
[
gr.inputs.Image(type="filepath", label="Input"),gr.inputs.Slider(0, 1, 0.1,default=0.6,label= 'Compression ratio')],
gr.outputs.Image(type="pil", label="Output"),
title=title,
description=description,
article=article
).launch(enable_queue=True,cache_examples=True,share=True)
复制代码
online
Gradio + Huggingface online machine learning application (purely free) I have introduced it many times, so I won't repeat it here, students who are not familiar with it, please move to my article: Tencent's algorithm, I moved it to the Internet, whatever Play!
Here are some minor problems and solutions.
Since cv2 is used, opencv-python needs to be installed, but the error is reported as follows:
File "/home/user/.local/lib/python3.8/site-packages/cv2/__init__.py", line 8, in <module>
from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
复制代码
For this error, there are the following methods online:
1 yum install:
yum install libglvnd-glx
复制代码
2 Reinstall the opencv package:
pip uninstall opencv-python
pip install opencv-python-headless
复制代码
The first method requires root privileges. It is recommended to directly use the second method, which will save trouble.