Try   HackMD

透視變形校正 Perspective Distortion Correction

前言

這是中興大學資工碩士課程-影像處理的第一個作業。我個人不太喜歡這個教授的教學方式。

這裡的實作就是參考 Jason Chen's Blog 作法,寫成 Python 版本。
他比較厲害,用 C++ 寫的,我菜雞只會用套件解方程式。

作業的目標:將變形的圖片轉成正面,就是將圖片中的四邊形指定區塊,變形成長方形狀(正面)的圖片。

內容

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

轉換過後的圖片。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

參考網址中的定義:

xp=ax+by+cxy+dyp=ex+fy+gxy+h

其中

(height,width)=(y,x)
(yp,xp)
表示四邊形的四個點座標,
(y,x)
表示對應的長方形的點座標。因為有四個點座標,可以列出各四組方程式,可以解出係數。

化為矩陣形式

Ax=b
Ax=b[x1y1x1y110000x2y2x2y210000x3y3x3y310000x4y4x4y4100000000x1y1x1y110000x2y2x2y210000x3y3x3y310000x4y4x4y41][abcdefgh]=[xp1xp2xp3xp4yp1yp2yp3yp4]

  • A
    從正方形轉換至四邊形的座標轉換矩陣,裡面的變數填入正方形點座標。
    x1
    表示正方形左上座標
    x
    數值,
    xp1
    表示對應的左上座標的
    x
    數值,剩餘編號自行類推。
  • x
    為想要解出的係數
  • b
    為四邊形的座標

實作過程

  1. 對圖片四邊形區塊找座標位置,可以使用 https://yangcha.github.io/iview/iview.html
  2. 實際物品測量比例,口罩比例為 (height, width) = (19, 34)。上圖中,輸出後的圖片調整為(190, 340)
  3. 建立
    A
    ,
    b
    矩陣
  4. 使用 numpy 套件計算
    x
    ,程式碼為 x = np.linalg.solve(A, b)
  5. x
    為求得的係數
  6. 使用 opencv 讀取目標檔案,使用
    x
    係數計算轉換後位置
  7. 輸出檔案

不要搞錯 (height, width) = (y, x) 的定義,很快就可以寫好了,就這麼簡單。

心得

numpy 是怎麼實作解

Ax=b 的?線性代數有教但是程式碼怎麼做的就不知道了。