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
本节英文原版代码下载 https://itbooks.pipipan.com/fs/18113597-320636142
来源: http://www.jianshu.com/p/618af554b0ec