LinRoger
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ###### tags: `Learning Opencv` # 投影轉換 --- ## 原理 在前一張所提及的方法皆應用於二維空間的轉換,如果物體是在3D空間發生旋轉,一樣可以透過方程式解法(最小平方法擬合),OpenCv 提供函式進行解析,由於多一個Z方向的解,因此需要四組方程組才有辦法得到解析解。 ```python cv2.getPerspectiveTransform(src, dst) ``` $\bigl(\begin{smallmatrix} \tilde{x}\\\\ \tilde{y}\\\\ \tilde{z} \end{smallmatrix}\bigr) = \bigl(\begin{smallmatrix} a_{11} &a_{12} & a_{13}\\\\ a_{21} &a_{22} & a_{23}\\\\ a_{31} &a_{32} & a_{33} \end{smallmatrix}\bigr) \bigl(\begin{smallmatrix} x\\\\ y\\\\ z \end{smallmatrix}\bigr)$ ### Example 假設(0, 0)、(200, 0)、(0, 200)、(200, 200)是原座標,透過某投影轉換依序為(100, 20)、(200, 20)、(50, 70)、(250, 70) sol: ```python import cv2 import numpy as np src = np.array([[0, 0], [200, 0], [0, 200], [200, 200]], np.float32) dst = np.array([[100, 20], [200, 20], [50, 70], [250, 70]], np.float32) p = cv2.getPerspectiveTransform(src, dst) ``` 傳回的投影矩陣為: ``` array([[ 5.00e-01, -3.75e-01, 1.00e+02], [ 0.00e+00, 7.50e-02, 2.00e+01], [-0.00e+00, -2.50e-03, 1.00e+00]]) ``` ## 圖片投影轉換 跟2D的類似方法,不過函式名稱改為 ```python cv2.warpPerspective(src, M, dsize) ``` ```python import numpy as np import cv2 import sys if __name__ == "__main__": if len(sys.argv) > 1: image = cv2.imread(sys.argv[1], 0) else: print("Usage: python warpPercpective.py image") exit() h, w = image.shape[:2] src = np.array([[0, 0], [w - 1, 0], [0, h - 1], [w - 1, h - 1]]) dst = np.array([[50, 50], [w / 3, 50], [50, h - 1], [w - 1, h - 1]]) p = cv2.getPerspectiveTransform(src, dst) r = cv2.warpPerspective(image, p, (w, h), borderValue=125) cv2.imshow("warpPerspective", r) cv2.waitKey(0) cv2.destroyAllWindows() ``` ## 極座標轉換 通常利用極座標轉換來校正影像中的圓形物體或被包含在圓環中的物體。 ### 將笛卡兒座標轉為極座標 將從(x, y)到($\theta , r$)表示,公式如下: $r = \sqrt{(x - \bar{x})^2 + (y - \bar{y})^2}$ $\theta = \left\{\begin{matrix} 2 \pi + \arctan2(y - \bar{y}, x - \bar{x}) &, y - \bar{y}\leq 0\\ \arctan2(y - \bar{y}, x - \bar{x}) &, y - \bar{y}\geq 0 \end{matrix}\right.$ OpenCv 提供函式 ```python import cv2 cv2.cartToPolar(x, y) ``` ### Example ```python import cv2 import numpy as np x = np.array([[0, 1, 2], [0, 1, 2], [0, 1, 2]], np.float64) y = np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2]], np.float64) r, theta = cv2.cartToPolar(x, y, angleInDegrees=True) ``` ``` >>> r array([[0. , 1. , 2. ], [1. , 1.41421356, 2.23606798], [2. , 2.23606798, 2.82842712]]) >>> theta array([[ 0. , 0. , 0. ], [90. , 44.99045563, 26.56710434], [90. , 63.43289566, 44.99045563]]) ``` ### 將極座標轉為笛卡兒座標 極座標轉換是可逆的,再以知道極座標與笛卡兒座標的條件下可以透過以下式子得到笛卡兒座標 $x = \bar{x} + r\cos \theta$ $y = \bar{y} + r \sin \theta$ ```python cv2.polarToCart(magnitude, angle) ``` ### Example ```python import cv2 import numpy as np angle = np.array([[30, 31], [30, 31]], np.float32) r = np.array([[10, 10], [11, 11]], np.float32) # 這樣得到的x, y 是針對(0, 0) 進行旋轉 x, y = cv2.polarToCart(r, angle, angleInDegrees=True) # 只要加上 -12 與 15,就可以針對不同中心進行轉換 x += -12 y += 15 ``` ``` >>> x array([[8.660255, 8.571674], [9.52628 , 9.428843]], dtype=float32) >>> y array([[5.0000005, 5.150382 ], [5.5000005, 5.66542 ]], dtype=float32) ``` ### 極座標對影像進行轉換 假設輸入影像矩陣為I,$(\bar{x}, \bar{y})$代表極座標空間轉換的中心,輸出矩陣為O,直觀的寫法為下式表示 $O(r, \theta) = f_1 (\bar{x}+r\cos\theta, \bar{y} + r\sin\theta)$ 上述的$\theta$與$r$都以1為單位進行離散化,由於轉換步距較大,輸出的影像O可能損失原圖許多資訊,可以透過以下方法進行改進 1. 設定範圍 $r_{min}, r_{max}$、$\theta_{min}, \theta_{max}$ 內進行座標轉換,因此步距值可以用$r_{step}$進行表示 公式最後可以變成 $O(i, j) = f_1 (\bar{x} + r_{min} + r_{step}i * \cos(\theta_{min} + \theta_{step}j), \, \bar{y} + (r_{min} + r_{step}i) * \cos(\theta_{min} + \theta_{step}j))$ #### tips: numpy tile(a, (m, n)) ```python import numpy as np a = np.array([[1, 2], [3, 4]]) b = np.tile(a, (2, 3)) # 將A矩陣分別在垂直方向和水平方向複製2次與3次 ``` ``` array([[1, 2, 1, 2, 1, 2], [3, 4, 3, 4, 3, 4], [1, 2, 1, 2, 1, 2], [3, 4, 3, 4, 3, 4]]) ``` ### polar 函數來實現影像極座標轉換 1. I 代表輸入影像 2. center 代表極座標轉換中心 3. r 代表最小距離與最大距離 4. theta 代表角度範圍 (0, 360) 5. rstep 代表r的轉換步進值 6. thetastep 代表角度的轉換步進值 ```python def polar(I, center, r, theta=(0, 360), rstep=1.0, thetastep=360.0 / (180 * 8)): minr, maxr = r mintheta, maxtheta = theta H = int((maxr - minr) / rstep) + 1 W = int((maxtheta - mintheta) / thetastep) + 1 O = 125 * np.ones((H, W), I.dtype) r = np.linspace(minr, maxr, H) r = tile(r, (W, 1)) r = np.transpose(r) theta = np.linspace(mintheta, maxtheta, W) theta = np.tile(theta, (H, 1)) x, y = cv2.polarToCart(r, theta, angleInDegrees=True) # 最近鄰插值 for i in range(H): for j in range(W): px = int(round(x[i][j] + cx)) py = int(round(y[i][j] + cy)) if ((px >= 0 and px <= w - 1) and (py >= 0 and py <= h - 1)): O[i][j] = I[py][px] return O ```python import cv2 import numpy as np import sys if __name__ == "__main__": if len(sys.argv) > 1: image = cv2.imread(sys.argv[1], 0) else: print("Usage: python warpPercpective.py image") exit() h, w = I.sha[:2] cx, cy = 508, 503 cv2.circle(I, (int(cx), int(cy)), 10, (255.0, 0, 0), 3) O = polar(I, (cx, cy), (200, 550)) O = cv2.flip(O, 0) cv2.imshow("O", O) cv2.waitKey(0) cv2.destroyAllWindows() ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully