opencv练习
灰度化,二值化
import cv2 as cv
import numpy as np
img = cv.imread('./one.png')
gray_img = cv.cvtColor(img,cv.COLOR_RGB2GRAY)
ret,binarization = cv.threshold(gray_img,150,255,cv.THRESH_BINARY)
ret,binarization_inv=cv.threshold(gray_img,150,255,cv.THRESH_BINARY_INV)
cv.putText(img,'initial image',(100,200),cv.FONT_HERSHEY_SIMPLEX,2,(0,0,255))
cv.putText(gray_img,'gray image',(100,200),cv.FONT_HERSHEY_SIMPLEX,2,(0,0,255))
cv.putText(binarization,'binarization image',(50,250),cv.FONT_HERSHEY_SIMPLEX,2,(0,0,255))
cv.imshow('image1',img)
cv.imshow('image2',gray_img)
cv.imshow('image3',binarization)
cv.imshow('image4',binarization_inv)
cv.waitKey(0)
图像的加减乘除
import cv2 as cv
import numpy as np
img1 = cv.imread('./one.png')
img2 = cv.imread('./first.png')
img3 = cv.resize(img2,(640,480))
new_img1 = cv.add(img1,img3)
new_img2 = cv.subtract(img1,img3)
new_img3 = cv.multiply(img1,img3)
new_img4 = cv.divide(img1,img3)
cv.imshow('image1',img1)
cv.imshow('image2',img3)
cv.imshow('add',new_img1)
cv.imshow('subtract',new_img2)
cv.imshow('multiply',new_img3)
cv.imshow('divide',new_img4)
cv.waitKey(0)
图像的仿射变换
import cv2 as cv
import numpy as np
img = cv.imread('./one.png')
img2 = cv.resize(img,None,fx=0.5,fy=0.5)
M = cv.getRotationMatrix2D((100,100),45,1)
h,w,ch = img.shape
img3 = cv.warpAffine(img,M,dsize=(w,h))
M = np.float32([[1,0,200],[0,1,100]])
img4 = cv.warpAffine(img,M,dsize=(w,h))
img5 = img[0:200,0:640]
img6 = cv.flip(img,0)
cv.imshow('image1',img)
cv.imshow('image2',img2)
cv.imshow('image3',img3)
cv.imshow('image4',img4)
cv.imshow('image5',img5)
cv.imshow('image6',img6)
cv.waitKey(0)
图像的增亮,减暗,反色
import cv2 as cv
import numpy as np
img = cv.imread('one.png')
M = np.full((480,640,3),fill_value=50,dtype=np.uint8)
for c in range(img.shape[2]):
for y in range(img.shape[0]):
for x in range(img.shape[1]):
M[y][x] = 255 - img[y][x]
cv.imshow('image1',M)
cv.imshow('image',img)
cv.waitKey(0)