顯示具有 s1071531 標籤的文章。 顯示所有文章
顯示具有 s1071531 標籤的文章。 顯示所有文章

2021年6月12日 星期六

s1071531 影像處理作業7

作業說明:

  影像形態學函式練習
        計算每張圖圍棋個數

執行環境:

  • Arch Linux x86_64
  • Python 3.9.2
  • Python venv
  • OpenCV 4.5.1

實作方式:

  • step 1 : read image in grayscale and median blur
  • step 2 : find stone of go by erode
    • step 2-1 : otsu thresholding
    • step 2-2 : erode
    • step 2-3 : find all connected components
    • step 2-4 : standrize area of connected components
    • step 2-5 : assume stone is found if abs(std) < 1.0
    • step 2-6 : mask thresholding image with circle of found stones
    • step 2-7 : count connected components
  • step 3 : find stone of go by adaptive threshold
    • step 3-1 : adaptive thresholding
    • step 3-2 : mask previous result with adaptive threshold
    • step 3-3 : erode
    • step 3-4 : find all connected components
    • step 3-5 : standrize area of connected components
    • step 3-6 : assume stone is found if abs(std) < 1.0
    • step 3-7 : mask erode image with circle of found stones
    • step 3-8 : count connected components
  • step 4 : find stone of go by clahe and adaptive threhold
    • step 4-1 : adaptive thresholding after clahe
    • step 4-2 : dilate previous result
    • step 4-3 : mask it with adaptive threshold
    • step 4-4 : erode
    • step 4-5 : closing
    • step 4-6 : find all connected components
    • step 4-7 : remove connected componets are too small
    • step 4-8 : count connected components
  • step 5 : add all counts
  • step 6 : merge all result binary image into B,G,R and save

執行結果:

如下圖
藍色、綠色、紅色分別為第二步、第三步、第四步的結果
三種顏色可能重疊 因為找到的棋子將會假設在色塊中心
因此若色塊太大 前一步驟的色塊就會與後一步驟的重疊

原圖 棋子數為 24
執行結果 三步驟分別找到 15 顆、2 顆、7 顆棋子
共 24 顆 誤差 0
原圖 棋子數為 31
執行結果 三步驟分別找到 25 顆、4 顆、2 顆棋子
共 31 顆 誤差 0
原圖 棋子數為 31
執行結果 三步驟分別找到 26 顆、3 顆、2 顆棋子
共 31 顆 誤差 0
執行結果

問題討論:

本次作業若要剛剛好數字全對需要手動試錯調整參數
一直到剛好三題都對
即使這樣像是第二張圖的第二次尋找其實也沒切割正確
只是數字剛好對
因此有想要想一個更可靠的辦法
但是最後沒做出來
因此才用分段尋找 只需要調整第二次和第三次尋找的參數就好
減少試錯次數

2021年6月7日 星期一

s1071531 影像處理作業8

作業說明:

  影像二值化練習

執行環境:

  • Arch Linux x86_64
  • Python 3.9.2
  • Python venv
  • OpenCV 4.5.1

實作方式:

  • step 1 : read jpeg in grayscale
  • step 2 : preform clahe on grayscale jpeg (adjust brightness)
  • step 3 : preform bilateral filter (denoise)
  • step 4 : preform adaptive mean threshold
  • step 5 : preform median blur (remove salt and pepper)
  • step 6 : save images

執行結果:

如下圖
原圖

二值化結果


2021年6月5日 星期六

s1071531 影像處理作業6

作業說明:

  Run-Length Encoding 影像壓縮練習

執行環境:

  • Arch Linux x86_64
  • Python 3.9.2
  • Python venv
  • OpenCV 4.5.1

實作方式:

  • compress
    1. split image to R/G/B planes
    2. flatten plane into 1d array (column major and row major)
    3. preform rle on 1d array to get (value, length) pairs
    4. encode pipeline of values
      1. differential encoding.
      2. fold it and then add 1.
      3. Elias gamma encoding.
    5. save length (length should not be zero)
      1. length from 1 to 3 save in 2 bits.
      2. length from 4 to 15 save in 6 bits.
      3. length > 15 save in 14, 22, 38, 70 bits if ceil(log2(length -15)) less or equal than 8, 16, 32, 64, respectively. 
    6. save the smaller compress result for each plane (column major or row major)
    7. for each plane write column major or row major as flag in header of file (1 bit)
    8. add header(size of image)
    9. write data to file
                format of saved length:
                first two bits: 00 if length > 3 else just save length
                third to sixth bits: 0 to 3 represent type of length if length > 15 else just save length
                0000: uint8   0001: uint16   0010: uint32  0011: uint64
                remaining bits: save length - 15

                example:
                3         :  11
                5         :  00 0101
                83       :  00 0000 01000100
                268     :  00 0000 11111101
                23786 :  00 0001 01011100 11011011
  • uncompress
    1.  read header
    2. decode values
    3. read length
    4. reconstruct 1d array
    5. reshape
    6. merge R/G/B channels

執行結果:

如下圖
壓縮率分別為 15.09, 11.00, 15.01
平均壓縮率為 13.70
執行時間均為約一至二秒左右
png 壓縮率分別為 10.89, 7.77, 11.09
png 平均壓縮率為 9.91

壓縮之檔案

壓縮率計算

解壓縮驗證

執行時間 (total)

png 檔案大小

png 壓縮率計算

問題討論:

經過壓縮後的檔案仍然可以被deflate壓縮30%
代表仍有重複冗餘
有想解決但每個 row 做 BWT 壓縮率會降低
整張圖一起做記憶體會不足
想使用 MED 做 predictive encoding 但是 numpy 無法 vectorize 可能會很慢
MTF 在比較新的研究中已經被拿掉
Huffman encoding 沒有看得懂的實作
LZ77 跟 RLE 的性質太像
對長度做 differential encoding 反而會降低壓縮率
因此最後就只對數值做 differential encoding 後接 Elias gamma encoding
比尚未壓縮數值的版本 平均壓縮率從 8.3 進步到 13.7

參考資料:

Python, fast compression of large amount of numbers with Elias Gamma - stackoverflow
(Elias Gamma 實作程式碼取自此)

2021年5月17日 星期一

s1071531 影像處理作業5

 作業說明:

  深度資訊可視化

執行環境:

  • Arch Linux x86_64
  • Python 3.9.2
  • Python venv
  • OpenCV 4.5.1

實作方式:

  • step 1 : read raw and bmp
  • step 2 : preform clahe on grayscale bmp (adjust brightness)
  • step 3 : unpack header
  • step 4 : unpack each channel
  • step 5 : change invalid point to None
  • step 6 : get nanmax and nanmin
  • step 7 : interpolate from [nanmin, nanmax] to [120, 0] to get hue
  • step 8 : map NaN to 0 and others to 255 to get s and v
  • step 9 : merge to hsv (combine bmp to v channel)
  • step 10 : convert color space to bgr
  • step 11 : save images

執行結果:

依序為 02 08 10 的 bmp 與 raw 與疊加圖
最低值藍色 最高值紅色 無效資料黑色或白色(視亮度而定)












2021年5月16日 星期日

s1071531 影像處理作業4

 作業說明:

  膚色偵測

執行環境:

  • Arch Linux x86_64
  • Python 3.9.2
  • Python venv
  • OpenCV 4.5.1

實作方式:

  • step 1 : read image
  • step 2 : convert to YCbCr color space
  • step 3 : YCbCr threshold
  • step 4 : convert to HSV color space
  • step 5 : HSV threshold
  • step 6 : bitwise and two threshold
  • step 7 : masking
  • step 8 : save images
       use morphology opening and median filter to remove noise

執行結果:

  依序為原圖和 mask 以及保留之膚色


參考資料:

CHEREF-Mehdi/SkinDetection - GitHubMorphological Transformations - OpenCVZero-sum game theory model for segmenting skin regions

2021年5月2日 星期日

s1071531 影像處理作業3

作業說明:

在 frequency domain apply 一個 gaussian low pass filter
並且得到 input 的 magnitude 以及 phase (shift 過的)

執行環境:

  • Arch Linux x86_64
  • Python 3.9.2
  • Python venv
  • OpenCV 4.5.1

實作方式:

  • step 1 : read image in grayscale
  • step 2 : zero padding to 2Mx2N
  • step 3 : multiply f(x,y) by (-1)^(x+y)
  • step 4 : compute dft of the image
  • step 4-1 : getting magnitude and phase
  • step 5 : generate filter function and form the product
  • step 6 : obtain the processed image
  • step 7 : obtain g(x,y) by extracting the MxN region
  • step 7-1 : save images

2021年3月18日 星期四

s1071531 影像處理作業2

題目:

讀取一圖像並以Sobel operator為基礎偵測邊緣

執行環境:

  • Arch Linux x86_64
  • Python 3.9.2
  • Python venv
  • OpenCV 4.5.1

實作方式:

用imread讀取圖片並使用grayscale的flag

呼叫openCV的Canny演算法API

(見參考資料Canny Edge Detection - OpenCV內Finding Intensity Gradient of Image一節,使用Sobel operator)

最後imwrite即可

執行結果:

由上而下為原圖以及其邊緣偵測結果



2021年2月26日 星期五

s1071531 影像處理作業1

 題目:

調整圖像之亮度

執行環境:

  • Arch Linux x86_64
  • Python 3.9.1
  • Python venv
  • OpenCV 4.5.1

實作方式:

用imread讀取圖片
使用numpy造一個相同大小矩陣

若v正數則用add
負數則用subtract abs

最後imwrite即可

執行結果:

由上而下為 原圖 暗版(v=-50) 亮版(v=50)






參考資料:


圖片來源:


自行轉jpg