天天看點

opencv-python 基礎知識

import imutils
import cv2

image = cv2.imread('11.jpg')
#input the shape of image
(h, w, d) = image.shape
print("width = {}, height = {}, depth = {}".format(w, h, d))

#input one pixel of image
(B, G, R) = image[2000,2000]
print("R = {}, G = {}, R = {}".format(R, G, B))

#extract a 100x100 pixel square ROI (Region of Interest) from input image
roi = image[1000:2000,1000:2000]
cv2.namedWindow('roi',cv2.WINDOW_KEEPRATIO)
cv2.imshow('roi',roi)
cv2.waitKey(0)

#resize the image to 1000x1000px, ignoring aspect ratio
resized = imutils.resize(image, width=2000)
cv2.namedWindow('resized',cv2.WINDOW_KEEPRATIO)
cv2.imshow('resized', image)
cv2.waitKey(0)

#display the image to our screen
cv2.namedWindow('image',cv2.WINDOW_KEEPRATIO)
cv2.imshow('image', image)
cv2.waitKey(0)

#let's rotate an image 45 degrees clockwise using opencv
center = (w//2,h//2)
M = cv2.getRotationMatrix2D(center, -45, 1.0)#順時針45度,有一部分圖像缺失
rotated = cv2.warpAffine(image, M, (w,h))
#rotation can also be easily accomplished via imutils with less code
rotated = imutils.rotate_bound(image, -45)#逆時針45度,且不損失圖像
cv2.namedWindow('opencv rotation',cv2.WINDOW_KEEPRATIO)
cv2.imshow('opencv rotation', rotated)
cv2.waitKey(0)

#對圖像進行高斯模糊處理
blurred = cv2.GaussianBlur(image,(11,11), 0)
cv2.namedWindow('blurred',cv2.WINDOW_KEEPRATIO)
cv2.imshow('blurred', blurred)

cv2.waitKey(0)
           

詳細代碼請看https://www.pyimagesearch.com/2018/07/19/opencv-tutorial-a-guide-to-learn-opencv/

此網站是一個很好的學習opencv-python的網站,裡面有很多有趣的項目,供大家學習