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的网站,里面有很多有趣的项目,供大家学习

你可能感兴趣的:(OpenCV,Python学习)