s1091536 作業6

 1.     題目說明:

Run-Length Based Image Compression 練習

附件中為三張利用將晶片高度以色彩視覺化後的圖片。 請設計一個基於 Run-Length 的壓縮法方,對圖檔作無失真壓縮後儲存成新檔案。 部落格上應敘述你的壓縮方法,提供壓縮檔之格式,並計算三張圖的平均壓縮率 (compression ratio)



2.開發環境:

OS: Windows 11
Editor: PyCharm
Language: python 3.8
Package: OpenCV 4.7.0

 

3.操作:

“python .\hw6_1091536.py”啟動程式。

將壓縮後的資料存成.txt,並產生解壓縮後的檔案.bmp

程式依序輸出:

壓縮完成、壓縮完成、解壓縮成功(是否與原始檔案相同)

壓縮前檔案大小、壓縮後檔案大小、壓縮率、資料減少量比率

剩餘資料量率


 =>=>


 

壓縮格式:(尺寸+三行數值)

col,row,

# B

val, 次數, val, 次數, val, 次數, val, 次數, val, 次數, val, 次數, ...

# G

val, 次數, val, 次數, val, 次數, val, 次數, val, 次數, val, 次數, ...

# R

val, 次數, val, 次數, val, 次數, val, 次數, val, 次數, val, 次數, ...


結果輸出:

------------------------- img1 -------------------------

img1 is encoding...

Encoding done!

Decoding done!

Decode success!

img1  size:

Before:  14321.537109375 KB

After:   5800.8671875 KB

Compression ratio:  2.469

Relative data redundancy:  59.5  %

### Remain  40.5  % of data ###

------------------------- img2 -------------------------

img2 is encoding...

Encoding done!

Decoding done!

Decode success!

img2  size:

Before:  14321.537109375 KB

After:   9990.9765625 KB

Compression ratio:  1.433

Relative data redundancy:  30.2  %

### Remain  69.8  % of data ###

------------------------- img3 -------------------------

img3 is encoding...

Encoding done!

Decoding done!

Decode success!

img3  size:

Before:  14321.537109375 KB

After:   5322.6923828125 KB

Compression ratio:  2.691

Relative data redundancy:  62.8  %

### Remain  37.2  % of data ###

Average Compression ratio:  2.198

 

 

4.方法及實作:

(a)概念&程式:

讀入影像

img1 = cv2.imread('img1.bmp')

壓縮

def RLencoding(img, path): # encode
    (rows, cols, channels) = img.shape
    # split 3 channels to 3 arrays
    
BGR = cv2.split(img)
    
print(path'is encoding...')
    path += 
'.txt'
    
f = open(path'w')
    # flatten each channel to 1 dimension
    B = B.flatten()
    f.write(
str(rows) + "," str(cols) + "," "\n")
    # encoding for B
    
val = B[0]
    quantity = 
0
    
for in range(len(B)):
        
if B[i] == val:
            quantity += 
1
        
else:
            f.write(
str(val) + "," str(quantity) + ",")
            val = B[i]
            quantity = 
1
    
f.write(str(val) + "," str(quantity) + ",")
    f.write(
"\n")

    
# encoding for G & R
    
...
    f.close()
    
print('Encoding done!')

解壓縮

def RLdecoding(path): # decode
    rowscols = 00
    
B = np.array([])
    
...
    
with open(path) as f:
        count = 
0
        
for line in f.readlines():
            t = line.split(
',')
            t.pop()
            t = 
list(map(intt))
            
if count == 0:
                rows = t[
0]
                cols = t[
1]
            
elif count == 1:
                B = t
            
elif count == 2:
                G = t
            
else:
                R = t
            count += 
1

    
B = np.array(B)
    
...
    
# reshape to 2 dimension
    
B = B.reshape(int(len(B) / 2)2)
    
...
    
# transform to image format
    
b = input_to_img(rowscolsB)
    
...
    output = np.array(cv2.merge([b
gr])dtype='uint8')
    
print('Decoding done!')
    
return output

 

留言

這個網誌中的熱門文章

rzwang Homework #1

s1093350 Homework #2

s1081444 Homework #7