iT邦幫忙

2024 iThome 鐵人賽

DAY 17
0
自我挑戰組

從零開始學Python系列 第 17

[Day17] Python OpenCV -2

  • 分享至 

  • xImage
  •  
  1. 常用函式
  • cvtColor色彩轉換 : 把彩色圖片轉換成黑白圖片
import cv2

img = cv2.imread("dog.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #轉換為灰階

cv2.imshow("img", img)
cv2.imshow("gray", gray)
cv2.waitKey(0)

https://ithelp.ithome.com.tw/upload/images/20240907/20168811mHmJ7mOvxi.png

  • GaussianBlur高斯模糊
    cv2.GaussianBlur(img, (11,11), 0) #(圖片檔案, 和, 標準差)
    只能是奇數!!
import cv2

img = cv2.imread("dog.jpg")
blur = cv2.GaussianBlur(img, (11,11), 0) #(圖片檔案, 和, 標準差)


cv2.imshow("img", img)
cv2.imshow("blur", blur)
cv2.waitKey(0) 

https://ithelp.ithome.com.tw/upload/images/20240907/20168811HRskbfXGuG.png

  • Canny 邊緣檢測:取得圖片的邊緣
import cv2

img = cv2.imread("dog.jpg")
canny = cv2.Canny(img, 250, 300)


cv2.imshow("img", img)
cv2.imshow("canny", canny)
cv2.waitKey(0) 

https://ithelp.ithome.com.tw/upload/images/20240907/20168811YbX2pleFnF.png

  • dilate 膨脹:將圖片邊緣線條膨脹
import cv2
import numpy as np

kernel = np.ones((3,3), np.uint8)

img = cv2.imread("dog.jpg")
canny = cv2.Canny(img, 300, 300)
dilate = cv2.dilate(canny, kernel, iterations=1)


cv2.imshow("canny", canny)
cv2.imshow("dilate", dilate)
cv2.waitKey(0) 

https://ithelp.ithome.com.tw/upload/images/20240907/20168811VFtLqZL740.png

  • erode 侵蝕:將圖片邊緣線條變細
import cv2
import numpy as np

kernel = np.ones((3,3), np.uint8)
kernel2 = np.ones((3,3), np.uint8)

img = cv2.imread("dog.jpg")
canny = cv2.Canny(img, 300, 300)
dilate = cv2.dilate(canny, kernel, iterations=1)
erode = cv2.erode(dilate, kernel2, iterations=1)


cv2.imshow("dilate", dilate)
cv2.imshow("erode", erode)
cv2.waitKey(0) 

https://ithelp.ithome.com.tw/upload/images/20240907/20168811USCaIfAUP3.png

  1. 在圖片上寫東西
  • 畫線(Line)
import cv2
import numpy as np

img = np.zeros((600, 600, 3), np.uint8)
cv2.line(img, (0,0), (400, 300), (255, 0, 0), 3)

cv2.imshow("img", img)
cv2.waitKey(0)

https://ithelp.ithome.com.tw/upload/images/20240907/20168811hffX3rlI28.png

  • 畫矩形
import cv2
import numpy as np

img = np.zeros((600, 600, 3), np.uint8)
cv2.rectangle(img, (0, 00), (400, 400), (0, 255, 0), 1)


cv2.imshow("img", img)
cv2.waitKey(0)

https://ithelp.ithome.com.tw/upload/images/20240907/20168811n9LkXewPUn.png

塗色的

import cv2
import numpy as np

img = np.zeros((600, 600, 3), np.uint8)
cv2.rectangle(img, (0, 00), (400, 400), (0, 255, 0), cv2.FILLED)


cv2.imshow("img", img)
cv2.waitKey(0)

https://ithelp.ithome.com.tw/upload/images/20240907/20168811kIkehcwgAo.png

  • 畫圓形
import cv2
import numpy as np

img = np.zeros((600, 600, 3), np.uint8)
cv2.circle(img, (300, 300), 40, (0, 0, 255), 3)


cv2.imshow("img", img)
cv2.waitKey(0)

https://ithelp.ithome.com.tw/upload/images/20240907/20168811q9TzWcLq5P.png

  • 寫字(只能寫英文字),中文字需做額外處理
import cv2
import numpy as np

img = np.zeros((600, 600, 3), np.uint8)
cv2.putText(img, "dog ", (300, 100), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 3, (255, 20, 90), 4)
#第一個數字是字的大小,第二個字是字的粗度

cv2.imshow("img", img)
cv2.waitKey(0)

https://ithelp.ithome.com.tw/upload/images/20240907/20168811aJgON24gDs.png


上一篇
[Day16] Python OpenCV -1
下一篇
[Day18] Python OpenCV -3
系列文
從零開始學Python30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言