欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

OpenCV函数用法详解31~40,含代码示例,可直接运行

时间:2023-04-23
opencv4 文章目录

opencv4

1.平移2.旋转3.几个函数的使用案例4.通道合并5.透视6.重映射7.复制8.图像缩小9.阈值处理10.自适应阈值处理 1.平移

#dst(x, y) = scr(x+100, y+200)import cv2import numpy as npimg = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0239.bmp")height, width = img.shape[:2]x = 100y = 200M = np.float32([[1, 0, x], [0, 1, y]])move = cv2.warpAffine(img, M, (width, height))cv2.imshow("img", img)cv2.imshow("move", move)cv2.waitKey()cv2.destroyAllWindows()

2.旋转

retval = cv2.getRotationMatrix2D#(center, angle(旋转角度), scale(缩放大小))

3.几个函数的使用案例

#ret:用来判断这个视频有没有图像#frame :这个图像用于操作的东西#用array进行标记(案例标记的是绿色区域)#将标记的定义为mask 然后,读取def extrace_object_demo(): capture = cv2.VideoCapture("balabala") while True: ret, frame = capture.read() if not ret: break hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) lower_hsv = np.array([37, 43, 46]) upper_hsv = np.array([77, 255, 255]) mask = cv2.inRange(hsv, lowerb= lower_hsv, upperb= upper_hsv) cv2.imshow("video", frame) cv2.imshow("mask", mask) c = cv2.waitKey() if c==27: break

4.通道合并

b, g, r = cv2.split(src)src = cv2.merge([b, g, r])

5.透视

dst = cv2.warpPerspective(要透视图像,变换矩阵,输出图像尺寸大小)

6.重映射

重映射:把一副图像内的像素点放置到另一幅图像内的指定位置。

import cv2import numpy as npimg = np.random.randint(0, 256, size=[4, 5], dtype=np.uint8)rows, cols = img.shapemapx = np.ones(img.shape, np.float32)*3mapy = np.ones(img.shape, np.float32)*0rst = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)print("img=n",img)print("mapx=n", mapx)print("mapy=n", mapy)print("rst=n", rst)#(第0行,第3列)

7.复制

import cv2import numpy as npimg = np.random.randint(0, 256, size=[4, 5], dtype=np.uint8)rows, cols = img.shapemapx = np.ones(img.shape, np.float32)mapy = np.ones(img.shape, np.float32)for i in range (rows): for j in range(cols): mapx.itemset((i, j), j) mapy.itemset((i, j), i)rst = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)print("img=n",img)print("mapx=n", mapx)print("mapy=n", mapy)print("rst=n", rst)

图像复制

import cv2import numpy as npimg = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0228.JPG")rows, cols = img.shape[:2]mapx = np.zeros(img.shape[:2], np.float32)mapy = np.zeros(img.shape[:2], np.float32)for i in range(rows): for j in range(cols): mapx.itemset((i, j), j)//若要其绕y轴翻转,j->(col-1-j), mapy.itemset((i, j), i)//若要其绕x轴翻转,i->(row-1-i),x,y互换,就把i,j互换rst = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)cv2.imshow("original", img)cv2.imshow("result", rst)cv2.waitKey()cv2.destroyAllWindows()

8.图像缩小

import cv2import numpy as npimg = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0228.JPG")rows, cols = img.shape[:2]mapx = np.zeros(img.shape[:2], np.float32)mapy = np.zeros(img.shape[:2], np.float32)for i in range(rows): for j in range(cols): if 0.25*cols< i<0.75*cols and 0.25*rows< j<0.75*rows: mapx.itemset((i,j),2*(j-cols*0.25)+0.5) mapy.itemset((i,j),2*(j-rows*0.25)+0.5) else: mapx.itemset((i, j), 0) mapy.itemset((i, j), 0)rst = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)cv2.imshow("original", img)cv2.imshow("result", rst)cv2.waitKey()cv2.destroyAllWindows()

9.阈值处理

设定某一个值为阈值,大于阈值为255,反之为0

import cv2img = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0239.bmp")t, rst = cv2.threshold(img, 127,255,cv2.THRESH_BINARY)//反二值化阈值处理(cv2.THRESH_BINARY_INV)cv2.imshow("img", img) //截断阈值化处理(cv2.THRESH_TRUNC)cv2.imshow("rst", rst) //超阈值零处理(cv2.THRESH_TOZERO_INV)cv2.waitKey() //低阈值零处理(cv2.THRESH_TOZERO)cv2.destroyAllWindows()

10.自适应阈值处理

import cv2img = cv2.imread("C:/Users/Lenovo/PycharmProjects/pythonProject3/IMG_0239.bmp",0)t1, thd = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)athdMEAN = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,5, 3)athdGAUS = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,5, 3)cv2.imshow("img", img)cv2.imshow("thd",thd)cv2.imshow("athdMEAN", athdMEAN)cv2.imshow("athdGAUS", athdGAUS)cv2.waitKey()cv2.destroyAllWindows()

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。