目录
图像统计信息
常用函数及参数
代码实现
运行结果
代码实现2:
运行结果
图像几何形状绘制
基本绘制函数
计算文本区域大小函数
代码实现:
随机数与随机颜色
Numpy中的随机函数
绘制随机噪声图
多边形的填充与绘制
使用的函数及参数的解释
代码实现
均值:cv.mean(src[mask])函数
获取均值和方差:cv.meanStdDev(src[, mean[, stddev[, mask]]]) ->mean, stddev
返回最小值最大值以及他们对应的像素位置:cv.minMaxLoc(src[, mask]) ->minVal, maxVal, minLoc, maxLoc
src表示输入图像,mask表示计算区域
roi = np.array([[5,3,4],[9,6,7],[8,2,3]],dtype=np.uint8)
mask = np.array([[0,3,0],[0,6,0],[0,2,0]],dtype=np.uint8)
m1 = cv.meanStdDev(roi)
m2 = cv.meanStdDev(roi, mask=mask)
minx,maxx,minx_id,max_id = cv.minMaxLoc(roi) # 获得最大值最小值
print(minx,maxx,minx_id,max_id)
print(roi,"\n",mask)
print(m1,"\n",m2)
绘制对比度:增大或减小像素值的差异来改变对比度。
image = cv.imread(".\\data\\butterfly.jpg")
cv.imshow("butterfly",image)
bgr_m = cv.mean(image)
sub_m = np.float32(image)[:,:] - (bgr_m[0],bgr_m[1],bgr_m[2])
result = sub_m * 0.5 # 低对比度
result = result[:, :] + (bgr_m[0] , bgr_m[1] , bgr_m[2])
cv.imshow("low-contrast-butterfly",cv.convertScaleAbs(result))
result = sub_m * 2.0 # 高对比度
result = result[:, :] + (bgr_m[0], bgr_m[1], bgr_m[2])
cv.imshow("high-contrast-butterfly",cv.convertScaleAbs(result))
cv.waitKey(0)
cv.destroyAllWindows()
canvas = np.zeros((512,512,3),dtype=np.uint8)
cv.rectangle(canvas,(100,100),(300,300),(0,0,255),-2,8)
# 表示绘制正方形在图像上,给定坐标,和颜色,线宽,2表示绘制,负数代表填充
cv.circle(canvas,(250,250),50,(255,0,0),2,cv.LINE_8)
cv.line(canvas,(100,100),(300,300),(0,255,0),2,8)
cv.putText(canvas,"Opencv-Python",(100,100),cv.FONT_HERSHEY_SIMPLEX,1.0,(255,0,255),2,8)
cv.imshow("canvas",canvas)
# 动态合理显示文本区域
font_color = (140,199,0)
cv.rectangle(canvas,(100,100),(300,300),font_color,2,8)
label_txt = "Hello,Opencv Python"
font = cv.FONT_HERSHEY_SIMPLEX # 字体
font_scale = 0.5 # 字体大小
thickness = 1 # 线宽
# 动态获取他文本的坐标,返回文本区域大小和字体基线
(fw,uph),dh = cv.getTextSize(label_txt,font,font_scale,thickness)
cv.rectangle(canvas,(100,100-uph-dh),(100+fw,100),(255,255,255),-1,8) # 画文本框
cv.putText(canvas,label_txt,(100,100-dh),font,font_scale,(255,0,255),thickness)
cv.imshow("canvas",canvas)
cv.waitKey(0)
cv.destroyAllWindows()
canvas = np.zeros((512,512,3),dtype=np.uint8)
# random draw
while True:
b,g,r = np.random.randint(0,256,size=3)
x1 = np.random.randint(0,512)
x2 = np.random.randint(0,512)
y1 = np.random.randint(0,512)
y2 = np.random.randint(0,512)
cv.rectangle(canvas,(x1,y1),(x2,y2),(int(b),int(g),int(r)),-1,8) # 填充
cv.imshow("canvas",canvas)
c = cv.waitKey(50)
if c == 27:
break
#cv.rectangle(canvas,(0,0),(512,512),(0,0,0),-1,8) # 擦掉前面绘制的内容
# 绘制噪声图
cv.randn(canvas,(120,100,140),(30,50,20))
cv.namedWindow("noise image",cv.WINDOW_KEEPRATIO)
cv.imshow("noise image",canvas)
image = cv.imread(".\\data\\lena.jpg")
dst = cv.add(image,canvas) # 给图像加随机噪声
cv.imshow("add noise image",dst)
cv.waitKey(0)
cv.destroyAllWindows()
canvas = np.zeros((512,512,3),dtype=np.uint8)
pts = []
pts.append((100,100))
pts.append((200,50))
pts.append((280,100))
pts.append((290,300))
pts.append((50,300))
pts = np.asarray(pts,dtype=np.int32)
print(pts.shape)
pts2 = []
pts2.append((300,300))
pts2.append((400,250))
pts2.append((500,300))
pts2.append((500,500))
pts2.append((250,500))
pts2 = np.asarray(pts2,dtype=np.int32)
print(pts2.shape)
# 描边线,两个点集,闭合曲线选择True,颜色为(0,0,255),线宽为2
cv.polylines(canvas,[pts,pts2],True,(0,0,2255),2,8)
# 填充
cv.fillPoly(canvas,[pts,pts2],(255,0,0),8,0)
cv.imshow("ploy-demo",canvas)
cv.waitKey(0)
cv.destroyAllWindows()