图片亮度操作
import numpy as np
import cv2
peng = cv2.imread("images/4star.jpg")
cv2.imshow("peng",peng)
#创建一个增强矩阵
M = np.ones(peng.shape,dtype="uint8") * 75
added = cv2.add(peng, M) #相加生产新的图
cv2.imshow("Added",added)
subtracted = cv2.subtract(peng,M) #相减
cv2.imshow("subtracted",subtracted)
cv2.waitKey()
cv2.destroyAllWindows()
图片裁剪
import cv2
import numpy as np
imagePeng = cv2.imread("images/4star.jpg")
height, width = imagePeng.shape[:2]
#设置我们要裁剪的宽高
start_row,start_col = int(height * 0.45),int(width * 0.45) #起点
end_row,end_col = int(height * 0.75), int(width * 0.75) # 终点
#用前面的起点跟终点来裁剪我们的图片
croppedPeng = imagePeng[start_row:end_row, start_col:end_col]
# cv2.imshow("Original Image",imagePeng)
# cv2.waitKey(0)
cv2.imshow("Crop Image",croppedPeng)
cv2.waitKey(0)
cv2.destroyAllWindows()
图片旋转,图片旋转有两种方式:
方式1:
import cv2
import numpy as np
image = cv2.imread("images/4star.jpg")
height, width = image.shape[:2]
# 旋转矩阵
rotation_matrix = cv2.getRotationMatrix2D((width/2,height/2),40,1)
# 旋转之后的画面
rotation_image = cv2.warpAffine(image,rotation_matrix,(width,height))
cv2.imshow("Rotation",rotation_image)
cv2.waitKey()
cv2.destroyAllWindows()
方式二,这种方式图片尺寸不变,单图片的尺寸会裁剪,这中方式只能每次旋转90度
import cv2
import numpy as np
image = cv2.imread('images/4star.jpg')
rotated_image = cv2.transpose(image)
cv2.imshow("rotated_image",rotated_image)
cv2.waitKey()
cv2.destroyAllWindows()
图像金字塔,放到缩小
import cv2
import numpy as np
image = cv2.imread("images/4star.jpg")
# 把图片縮小 3/4.
image_scaled = cv2.resize(image,None,fx=0.75, fy=0.75)
cv2.imshow("scaling - Linear", image_scaled)
cv2.waitKey()
# 把图片放大兩倍
image_scaled2 = cv2.resize(image,None,fx=2, fy=2)
cv2.imshow("scaling - no - Cubic", image_scaled2)
cv2.waitKey()
# 把图片放大道指定的大小
image_scaled3 = cv2.resize(image,(900,400),interpolation = cv2.INTER_AREA)
cv2.imshow("scaling - AREA", image_scaled3)
cv2.waitKey()
使用提供的方法
import cv2
image = cv2.imread("images/4star.jpg")
smller = cv2.pyrDown(image)
larger = cv2.pyrUp(image)
cv2.imshow("Original",image)
cv2.imshow("Smaller",smller)
cv2.imshow("larger",larger)
cv2.waitKey()
cv2.destroyAllWindows()
import cv2
import numpy as np
image = cv2.imread('images/4star.jpg')
# 取出寬高
height, width = image.shape[:2]
# 設置平移之後的寬高
quarter_height, quarter_width = height/4 , width/4
# / 1,0,tx/
# T = / 0,1,ty/
# T 平移時候的矩陣
T = np.float32([[1,0,quarter_width],[0,1,quarter_height]])
image_translation = cv2.warpAffine(image, T, (width,height))
cv2.imshow("Translation",image_translation)
cv2.waitKey()
cv2.destroyAllWindows()
OpenCV 画图
画正方形 色值是0 黑心
import cv2
import numpy as np
# 创建一个正方形的图像
image = np.zeros((512,1612,3),np.uint8)
# 创建一个黑白的正方形
image_bw = np.zeros((512,1612),np.uint8)
cv2.imshow("Black Rectangle (color)",image)
cv2.imshow("Black Rectangle (B&W)",image_bw)
cv2.waitKey(0)
cv2.destroyAllWindows()
在这个图像上再画线,再上点颜色
#在上面代码上画先
image = np.zeros((512,512,3),np.uint8)
cv2.line(image,(0,0),(511,511),(255,127,0),5)#参数,起点、终点,颜色、粗细
cv2.imshow("Line",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
在图像上画矩形,注意线条的大小为-1时,为实心矩形
import cv2
import numpy as np
image = np.zeros((512,512,3),np.uint8)
cv2.rectangle(image,(100,100),(300,250),(127,50,127),5)
# cv2.rectangle(image,(100,100),(300,250),(127,50,127),-1) 实心矩形
cv2.imshow("Rectangle",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
画圆,-1也是实心圆
import cv2
import numpy as np
image = np.zeros((512,512,3),np.uint8)
cv2.circle(image,(350,350),100,(15,75,50),-1)
cv2.imshow("circle",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
多边形
import cv2
import numpy as np
image = np.zeros((512,512,3),np.uint8)
#给出多边形每个顶点的座标
pts = np.array([[10,50],[400,50],[90,200],[50,500]],np.int32)
pts = pts.reshape((-1,1,2)) # 形狀出來了
cv2.polylines(image,[pts],True,(0,0,255),5)
cv2.imshow("Polygon",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
加文字
import cv2
import numpy as np
image = np.zeros((512,512,3),np.uint8)
cv2.putText(image,"Hello peng",(75,200),cv2.FONT_HERSHEY_COMPLEX,2,(100,170,0),3)
## cv2.putText(在什么上面写字,"文字的内容",(文字的起始座标),字体,字体大小,(颜色),粗细)
cv2.imshow("Text",image)
cv2.waitKey(0)
cv2.destroyAllWindows()