黃駿棠
    • 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
    • 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
    • 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 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
  • 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- title: 'CS362 Assignment #1' disqus: hackmd --- 圖像旋轉 (Image Rotation) === ## 目錄 [TOC] ## 作業說明 撰寫一個程式將一張圖像的(a)整張圖像,(b)中心內切圓區域,旋轉一個角度(逆時針旋 轉 0 度至 359 度):利用一個滑動條(trackbar)控制旋轉角度。 ![](https://i.imgur.com/M4v0IZZ.png) ## 開發環境 ``` $ uname -srvmio Linux 5.19.0-35-generic #36~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Feb 17 15:17:25 UTC 2 x86_64 x86_64 GNU/Linux $ gcc --version gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 $ pkg-config --modversion opencv4 4.5.4 ``` ## 程式碼說明 ### (a)整張圖片旋轉 #### 宣告與初始化變數 ```cpp=1 // include opencv 與 stdio library #include "opencv2/highgui.hpp" #include "opencv2/imgcodecs.hpp" #include <opencv2/opencv.hpp> #include <stdio.h> // 定義GUI視窗名稱、滑動條名稱與最大值 #define WINDOW_TITLE "Rotated image" #define SLIDER_MAX 359 #define TRACK_BAR_NAME "Degree" // 使用 cv namespace using namespace cv; //設定全域變數 src 用來儲存圖片 Mat src; ``` #### 函式宣告 ```cpp=17 // 宣告一個 function 當 trackbar onchange 時執行 static void on_trackbar(int, void *) { // 取得Trackbar的位置(Position) double degree = getTrackbarPos(TRACK_BAR_NAME, WINDOW_TITLE); // 宣告一個物件用於儲存輸出矩陣 Mat output; /* 宣告一個轉置矩陣 * 利用 getRotationMatrix2D 可以產生一個旋轉矩陣 * 第一個參數為旋轉中心,以圖片的中心為旋轉點 * 第二個參數為旋轉角度 * 第三個參數為縮放比例 */ Mat rotation = getRotationMatrix2D(Point2f(src.cols / 2, src.rows / 2), degree, 1); // 矩陣重新映射至 output warpAffine(src, output, rotation, src.size()); // 顯示 output 之影像 imshow(WINDOW_TITLE, output); } ``` 旋轉矩陣運作原理 $M = \begin{bmatrix} \alpha & \beta & (1- \alpha ) \cdot \texttt{center.x} - \beta \cdot \texttt{center.y} \\ - \beta & \alpha & \beta \cdot \texttt{center.x} + (1- \alpha ) \cdot \texttt{center.y} \end{bmatrix}$ $α = scale ⋅ cos(angle)$ $β = scale ⋅ sin(angle)$ #### 介面控制 ```cpp=37 // 透過 argc argv 去接收圖片路徑 int main(int argc, char **argv) { // 當參數不為 2 時表示路徑格是錯誤 if (argc != 2) { printf("usage: DisplayImage.out <Image_Path>\n"); return -1; } // 讀入路徑圖片 src = imread(argv[1], IMREAD_COLOR); if (src.empty()) { printf("%s", "Error loading image \n"); return -1; } // 命名新的視窗 namedWindow(WINDOW_TITLE, WINDOW_AUTOSIZE); // 建立 trackbar createTrackbar(TRACK_BAR_NAME, WINDOW_TITLE, nullptr, SLIDER_MAX, on_trackbar); // 設定 trackbar 位置 setTrackbarPos(TRACK_BAR_NAME, WINDOW_TITLE, 0); // 初始化位置 on_trackbar(0, 0); waitKey(0); return 0; } ``` ### (b)圖片中心旋轉 ```gcc=1 #include "opencv2/highgui.hpp" #include "opencv2/imgcodecs.hpp" #include <opencv2/opencv.hpp> #include <stdio.h> #define WINDOW_TITLE "Rotated image center" #define SLIDER_MAX 359 #define TRACK_BAR_NAME "Degree" #define CIRCLE_SCALE 100 using namespace cv; Mat src; static void on_trackbar(int, void *) { double degree = getTrackbarPos(TRACK_BAR_NAME, WINDOW_TITLE); Mat mask = Mat::zeros(src.size(), src.type()); Mat rev_mask; Mat output, middle; Point2f center(src.cols / 2, src.rows / 2); // 建立一個圓形圖案,並儲存至mask作為遮罩 circle(mask, center, CIRCLE_SCALE, Scalar(255, 255, 255), FILLED); //原圖透過遮罩後存入middle src.copyTo(middle, mask); //建立一個反向遮罩 bitwise_not(mask, rev_mask); //原圖透過反向遮罩存入rev_mask src.copyTo(output, rev_mask); /* 現在已經將原圖拆為兩個部分 * 1.內部圓形 * 2.外部框架 */ // 旋轉中間圓形部分 Mat rotation = getRotationMatrix2D(center, degree, 1); warpAffine(middle, middle, rotation, middle.size()); // 輸出旋轉後的內部圓形與外部框架之合併圖 imshow(WINDOW_TITLE, middle | output); } int main(int argc, char **argv) { // get image path from command line if (argc != 2) { printf("usage: DisplayImage.out <Image_Path>\n"); return -1; } // load image from path src = imread(argv[1], IMREAD_COLOR); if (src.empty()) { printf("%s", "Error loading image \n"); return -1; } // create window namedWindow(WINDOW_TITLE, WINDOW_AUTOSIZE); createTrackbar(TRACK_BAR_NAME, WINDOW_TITLE, nullptr, SLIDER_MAX, on_trackbar); setTrackbarPos(TRACK_BAR_NAME, WINDOW_TITLE, 0); // initial window on_trackbar(0, 0); waitKey(0); return 0; } ``` ## 程式展示 ### 編譯 進入資料夾 ![](https://i.imgur.com/Cn4Ummv.png =600x400) ```bash= cmake . make ``` ![](https://i.imgur.com/kVF8MIj.png =600x400) ![](https://i.imgur.com/6tswSTZ.png =600x400) ### 執行 #### a ```bash= ./HW1a yzu.bmp ``` ![](https://i.imgur.com/Vcfnmut.png =600x400) | 初始化 | 旋轉30 | 旋轉345 | | -------- | -------- | -------- | | ![](https://i.imgur.com/GOjr2qm.png =400x200)| ![](https://i.imgur.com/nPsvHTC.png =400x200) | ![](https://i.imgur.com/oGdjBK3.png =400x200) | #### b ```bash= ./HW1b yzu.bmp ``` ![](https://i.imgur.com/Yv81Vj7.png =600x400) | 初始化 | 旋轉43 | 旋轉270 | | -------- | -------- | -------- | | ![](https://i.imgur.com/t2Se7wk.png)| ![](https://i.imgur.com/QbV061h.png) | ![](https://i.imgur.com/BwLFp3Y.png) | ## 參考資料 [cv::getRotationMatrix2D](https://docs.opencv.org/4.7.0/da/d54/group__imgproc__transform.html#gafbbc470ce83812914a70abfb604f4326) [cv:wrapAffine](https://docs.opencv.org/4.7.0/da/d54/group__imgproc__transform.html#ga0203d9ee5fcd28d40dbc4a1ea4451983) cv Sample(需要從source編譯) ###### tags: `CS362` `s1081050` `HW1`

    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