[雪峰磁針石部落格]計算機視覺opcencv工具深度學習快速實戰2opencv快速入門

書籍尋找發表於2018-11-19

opencv基本操作

# -*- coding: utf-8 -*-
# Author:    xurongzhong#126.com wechat:pythontesting qq:37391319
# 技術支援 釘釘群:21745728(可以加釘釘pythontesting邀請加入) 
# qq群:144081101 591302926  567351477
# CreateDate: 2018-11-17

import imutils
import cv2

# 讀取圖片資訊
image = cv2.imread("jp.png")
(h, w, d) = image.shape
print("width={}, height={}, depth={}".format(w, h, d))

# 顯示圖片
cv2.imshow("Image", image)
cv2.waitKey(0)

# 訪問畫素
(B, G, R) = image[100, 50]
print("R={}, G={}, B={}".format(R, G, B))

# 選取圖片區間 ROI (Region of Interest)
roi = image[60:160, 320:420]
cv2.imshow("ROI", roi)
cv2.waitKey(0)

# 縮放
resized = cv2.resize(image, (200, 200))
cv2.imshow("Fixed Resizing", resized)
cv2.waitKey(0)

# 按比例縮放
r = 300.0 / w
dim = (300, int(h * r))
resized = cv2.resize(image, dim)
cv2.imshow("Aspect Ratio Resize", resized)
cv2.waitKey(0)

# 使用imutils縮放
resized = imutils.resize(image, width=300)
cv2.imshow("Imutils Resize", resized)
cv2.waitKey(0)

# 順時針旋轉45度
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, -45, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
cv2.imshow("OpenCV Rotation", rotated)
cv2.waitKey(0)

# 使用imutils旋轉
rotated = imutils.rotate(image, -45)
cv2.imshow("Imutils Rotation", rotated)
cv2.waitKey(0)


# 使用imutils無損旋轉
rotated = imutils.rotate_bound(image, 45)
cv2.imshow("Imutils Bound Rotation", rotated)
cv2.waitKey(0)

# apply a Gaussian blur with a 11x11 kernel to the image to smooth it,
# useful when reducing high frequency noise 高斯模糊
# https://www.pyimagesearch.com/2016/07/25/convolutions-with-opencv-and-python/
blurred = cv2.GaussianBlur(image, (11, 11), 0)
cv2.imshow("Blurred", blurred)
cv2.waitKey(0)

# 畫框
output = image.copy()
cv2.rectangle(output, (320, 60), (420, 160), (0, 0, 255), 2)
cv2.imshow("Rectangle", output)
cv2.waitKey(0)

# 畫圓
output = image.copy()
cv2.circle(output, (300, 150), 20, (255, 0, 0), -1)
cv2.imshow("Circle", output)
cv2.waitKey(0)

# 劃線
output = image.copy()
cv2.line(output, (60, 20), (400, 200), (0, 0, 255), 5)
cv2.imshow("Line", output)
cv2.waitKey(0)

# 輸出文字
output = image.copy()
cv2.putText(output, "https://china-testing.github.io", (10, 25), 
    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
cv2.imshow("Text", output)
cv2.waitKey(0)

原圖:

圖片.png

選取圖片區間 ROI (Region of Interest)

圖片.png

縮放

圖片.png

按比例縮放

圖片.png

旋轉

圖片.png

使用imutils無損旋轉

圖片.png

高斯模糊

圖片.png

畫框

圖片.png

畫圓

圖片.png

劃線

圖片.png

輸出文字

Text_screenshot_17.11.2018.png

執行時的輸出

$ python opencv_tutorial_01.py 
width=600, height=322, depth=3
R=41, G=49, B=37

本節英文原版程式碼下載

關於旋轉這塊,實際上pillow做的更好。比如同樣逆時針旋轉90度。

opencv的實現:

import imutils
import cv2

image = cv2.imread("jp.png")
rotated = imutils.rotate(image, 90)
cv2.imshow("Imutils Rotation", rotated)
cv2.waitKey(0)

Text_screenshot_17.11.2018.png

pillow的實現:

from PIL import Image

im = Image.open("jp.png")
im2 = im.rotate(90, expand=True)
im2.show()

test.jpg

更多參考: python庫介紹-影像處理工具pillow中文文件-手冊(2018 5.*)

deep_learning_face_detection_opencv.gif

參考資料

識別俄羅斯方塊

# -*- coding: utf-8 -*-
# Author:    xurongzhong#126.com wechat:pythontesting qq:37391319
# 技術支援 釘釘群:21745728(可以加釘釘pythontesting邀請加入) 
# qq群:144081101 591302926  567351477
# CreateDate: 2018-11-19
# python opencv_tutorial_02.py --image tetris_blocks.png

import argparse
import imutils
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
    help="path to input image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
cv2.imshow("Image", image)
cv2.waitKey(0)

# 轉為灰度圖
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Gray", gray)
cv2.waitKey(0)

# 邊緣檢測
edged = cv2.Canny(gray, 30, 150)
cv2.imshow("Edged", edged)
cv2.waitKey(0)

# 門限
thresh = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)[1]
cv2.imshow("Thresh", thresh)
cv2.waitKey(0)

# 發現邊緣
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
output = image.copy()

# loop over the contours
for c in cnts:
    # draw each contour on the output image with a 3px thick purple
    # outline, then display the output contours one at a time
    cv2.drawContours(output, [c], -1, (240, 0, 159), 3)
    cv2.imshow("Contours", output)
    cv2.waitKey(0)

# draw the total number of contours found in purple
text = "I found {} objects!".format(len(cnts))
cv2.putText(output, text, (10, 25),  cv2.FONT_HERSHEY_SIMPLEX, 0.7,
    (240, 0, 159), 2)
cv2.imshow("Contours", output)
cv2.waitKey(0)

# we apply erosions to reduce the size of foreground objects
mask = thresh.copy()
mask = cv2.erode(mask, None, iterations=5)
cv2.imshow("Eroded", mask)
cv2.waitKey(0)

# 擴大
mask = thresh.copy()
mask = cv2.dilate(mask, None, iterations=5)
cv2.imshow("Dilated", mask)
cv2.waitKey(0)

# a typical operation we may want to apply is to take our mask and
# apply a bitwise AND to our input image, keeping only the masked
# regions
mask = thresh.copy()
output = cv2.bitwise_and(image, image, mask=mask)
cv2.imshow("Output", output)
cv2.waitKey(0)

原圖和灰度圖:

圖片.png

邊緣檢測

圖片.png

門限

圖片.png

輪廓

圖片.png

查詢結果

圖片.png

腐蝕和擴張

圖片.png

圖片.png

遮蔽和位操作

圖片.png

串在一起執行

$ python opencv_tutorial_02.py --image tetris_blocks.png

test.gif


相關文章