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
deep_learning_face_detection_opencv.gif
网友评论