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()