Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Image Compression | Results Analysis
Principal Component Analysis

Stryg for at vise menuen

book
Image Compression

Let's move on to the final task that PCA can solve - this is image compression. The solution of this problem occurs according to the same algorithm as usual. We already know how to create PCA models and load data into them. So now we will delve into other details. Compression of black and white and color images is done differently. Compressing black and white images is no different from compressing regular ones. While for color images it is required to: split the image into 3 RGB color channels, reduce the dimension of each channel using PCA and then combine the channels into a full-fledged color image. To read images and separate them into RGB channels, we need the matplotlib and cv2 libraries:

import matplotlib.pyplot as plt
import cv2

img = plt.imread('test.jpg')
b, g, r = cv2.split(RGB_img)

We standardize the data. We can implement this easier, without using a library, but only with the help of division:

r_scaled = r / 255
g_scaled = g / 255
b_scaled = b / 255

Now let's create 3 PCA models:

from sklearn.decomposition import PCA

pca_r = PCA(n_components=50)
pca_r_trans = pca_r.fit_transform(r_scaled)

pca_g = PCA(n_components=50)
pca_g_trans = pca_g.fit_transform(g_scaled)

pca_b = PCA(n_components=50)
pca_b_trans = pca_b.fit_transform(b_scaled)

Now we can combine the received data into one image:

pca_r_org = pca_r.inverse_transform(pca_r_trans)
pca_g_org = pca_g.inverse_transform(pca_g_trans)
pca_b_org = pca_b.inverse_transform(pca_b_trans)

img_compressed = cv2.merge((pca_b_org, pca_g_org, pca_r_org))
Opgave

Swipe to start coding

Reduce the dimension of the black and white image to 40 components.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 5
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

close

Awesome!

Completion rate improved to 5.26

book
Image Compression

Let's move on to the final task that PCA can solve - this is image compression. The solution of this problem occurs according to the same algorithm as usual. We already know how to create PCA models and load data into them. So now we will delve into other details. Compression of black and white and color images is done differently. Compressing black and white images is no different from compressing regular ones. While for color images it is required to: split the image into 3 RGB color channels, reduce the dimension of each channel using PCA and then combine the channels into a full-fledged color image. To read images and separate them into RGB channels, we need the matplotlib and cv2 libraries:

import matplotlib.pyplot as plt
import cv2

img = plt.imread('test.jpg')
b, g, r = cv2.split(RGB_img)

We standardize the data. We can implement this easier, without using a library, but only with the help of division:

r_scaled = r / 255
g_scaled = g / 255
b_scaled = b / 255

Now let's create 3 PCA models:

from sklearn.decomposition import PCA

pca_r = PCA(n_components=50)
pca_r_trans = pca_r.fit_transform(r_scaled)

pca_g = PCA(n_components=50)
pca_g_trans = pca_g.fit_transform(g_scaled)

pca_b = PCA(n_components=50)
pca_b_trans = pca_b.fit_transform(b_scaled)

Now we can combine the received data into one image:

pca_r_org = pca_r.inverse_transform(pca_r_trans)
pca_g_org = pca_g.inverse_transform(pca_g_trans)
pca_b_org = pca_b.inverse_transform(pca_b_trans)

img_compressed = cv2.merge((pca_b_org, pca_g_org, pca_r_org))
Opgave

Swipe to start coding

Reduce the dimension of the black and white image to 40 components.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

close

Awesome!

Completion rate improved to 5.26

Stryg for at vise menuen

some-alt