opencv学习笔记(1)

#检测opencv插件安装成功否
import cv2 as cv
img=cv.imread(r'D:\DeepLearning\timg.jpg')
cv.namedWindow('Image')
cv.imshow('Image',img)
cv.waitKey(0)
cv.destroyAllWindows()

#1图片读取和展示
import cv2 as cv
img=cv.imread('kst.jpg',1)#0是灰度图像,1是彩色图像
cv.imshow('image',img)#image是窗体的名称,img是展示的图像
cv.waitKey(0)#程序的暂停

#2.图片写入
import cv2 as cv
img=cv.imread('kst.jpg',1)
cv.imwrite('kst2.jpg',img)#name.data

#3.图片质量,100k左右清晰。jpg有损压缩,0时压缩比高,100压缩比低
import cv2 as cv
img=cv.imread('kst.jpg',1)
cv.imwrite('kst3_1.jpg',img,[cv.IMWRITE_JPEG_QUALITY,50])#0-100的有损压缩

#3图片质量:png无损压缩,有图片透明度属性,0时压缩比低,9时压缩比高
import cv2 as cv
img=cv.imread('kst.jpg',1)
cv.imwrite('kst3_2.png',img,[cv.IMWRITE_PNG_COMPRESSION,0])

#4.像素操作
#像素读取
import cv2 as cv
img=cv.imread('kst.jpg',1)
(b,g,r)=img[100,100]#opencv读取像素点值返回元组,bgr
print(b,g,r)
#给图片的【10,100】到【110,100】写入蓝色
#像素写入
for i in range(100):
    img[10+i,100]=(255,0,0)#蓝色
cv.imshow('kst.jpg',img)
cv.waitKey(0)


#5.tensorflow操作
import tensorflow as tf
data1=tf.constant(2,dtype=tf.int32)#常量的定义与类型
data2=tf.Variable(10,name='var')#变量的定义与初始值
print(data1)
print(data2)
sess=tf.Session()
print(sess.run(data1))
#变量必须要初始化
init=tf.global_variables_initializer()
sess.run(init)
print(sess.run(data2))
sess.close()

#6常量加减乘除运算
import tensorflow as tf
data1=tf.constant(6)
data2=tf.constant(2)
data_add=tf.add(data1,data2)
data_sub=tf.subtract(data1,data2)
data_mul=tf.multiply(data1,data2)
data_div=tf.divide(data1,data2)
with tf.Session() as sess:
    print(sess.run(data_add))
    print(sess.run(data_sub))
    print(sess.run(data_mul))
    print(sess.run(data_div))
print('end')

#6常量与变量加减乘除运算
import tensorflow as tf
data1=tf.constant(6)
data2=tf.Variable(2)
data_add=tf.add(data1,data2)
data_copy=tf.assign(data2,data_add)#将add值赋给data2
data_sub=tf.subtract(data1,data2)
data_mul=tf.multiply(data1,data2)
data_div=tf.divide(data1,data2)
init=tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print(sess.run(data_add))
    print(sess.run(data_sub))
    print(sess.run(data_mul))
    print(sess.run(data_div))
    print('sess.run(data_copy)',sess.run(data_copy))
    print('data_copy.eval',data_copy.eval())
    print('tf.get_default_session',tf.get_default_session().run(data_copy))
print('end')

#6placeholder
import tensorflow as tf
data1=tf.placeholder(tf.float32)
data2=tf.placeholder(tf.float32)
data_add=tf.add(data1,data2)
with tf.Session() as sess:
    print(sess.run(data_add,feed_dict={data1:6,data2:2}))
print('end')

#7矩阵运算
import tensorflow as tf
mat0=tf.constant([[0,1,2],[2,3,4]])
mat1=tf.zeros([2,3])
mat2=tf.ones([3,2])
mat3=tf.fill([3,3],15)
mat4=tf.zeros_like(mat1)#与mat1同纬度的0矩阵
with tf.Session() as sess:
    print(sess.run(mat0))
    print(sess.run(mat1))
    print(sess.run(mat2))
    print(sess.run(mat3))
    print(sess.run(mat4))

#8.matplotlib
import numpy as np
import matplotlib.pyplot as plt
x=np.array([1,2,3,4,5,6,7,8])
y=np.array([3,4,5,5,6,3,7,9])
#折线图(x,y,color,linewidth)
plt.plot(x,y,'r',lw=10)
#柱状图(x,y,柱子的占比,alpha,color)
plt.bar(x,y,0.8,alpha=1,color='b')
plt.show()

#9.神经网络逼近股票收盘均价
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt 
endprice=np.array([2511.90,2538.26,2510.68,2591.66,2732.98,2701.69,2701.29,2678.67,2726.50,2681.50,2739.17,2715.07,2823.58,2864.90,2919.08])
beginprice=np.array([2438.71,2500.88,2534.95,2512.52,2594.04,2743.26,2697.47,2695.24,2678.23,2722.13,2674.93,2744.13,2717.46,2832.73,2877.40])
plt.figure()
for i in range(0,15):
    dateone=np.zeros([2])
    dateone[0]=i
    dateone[1]=i
    priceone=np.zeros([2])
    priceone[0]=beginprice[i]
    priceone[1]=endprice[i]
    if endprice[i]>beginprice[i]:
        plt.plot(dateone,priceone,'r',lw=8)
    else:
        plt.plot(dateone,priceone,'g',lw=8)
plt.show()


date=np.linspace(1,15,15)
datenormal=np.zeros([15,1])
pricenormal=np.zeros([15,1])
for i in range(15):
    #数据归一化
    datenormal[i,0]=i/14.0
    pricenormal[i,0]=endprice[i]/3000.0
x=tf.placeholder(tf.float32,[None,1])
y=tf.placeholder(tf.float32,[None,1])
#隐藏层
w1=tf.Variable(tf.random_uniform([1,10],0,1))#从0到1的1行10列随机数
b1=tf.Variable(tf.zeros([1,10]))
wb1=tf.matmul(x,w1)+b1
layer1=tf.nn.relu(wb1)
#输出层
w2=tf.Variable(tf.random_uniform([10,1],0,1))#从0到1的1行10列随机数
b2=tf.Variable(tf.zeros([15,1]))
wb2=tf.matmul(layer1,w2)+b2
layer2=tf.nn.relu(wb2)

loss=tf.reduce_mean(tf.square(y-layer2))
train_step=tf.train.GradientDescentOptimizer(0.1).minimize(loss)

init=tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    for i in range(10000):
        sess.run(train_step,feed_dict={x:datenormal,y:pricenormal})
    pred=sess.run(layer2,feed_dict={x:datenormal})
    predprice=np.zeros([15,1])
    for i in range(15):
        predprice[i,0]=(pred*3000)[i,0]
    plt.plot(date,predprice,'b',lw=1)
plt.show()

 2.图片特效

#1.图片缩放
import cv2 as cv
img=cv.imread('kst.jpg',1)
imginfo=img.shape
#1.获取图片的长宽通道信息
print(imginfo)
height=imginfo[0]
width=imginfo[1]
deep=imginfo[2]
#2.(非)等比例缩小放大
dstheight=int(height*0.5)
dstwidth=int(width*0.5)
#3.缩放方法:最近邻域插值,双线性插值(默认),像素关系重采样,立方插值
dst=cv.resize(img,(dstwidth,dstheight))
cv.imshow('kstp',dst)
cv.waitKey(0)

#2.图片剪切x:100-200,y:100-300
import cv2 as cv
img=cv.imread('kst.jpg',1)
dst=img[100:200,100:300]
cv.imshow('kstp2',dst)
cv.waitKey(0)



#3.图片移位
import cv2 as cv
import numpy as np
img=cv.imread('kst.jpg',1)
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]

matshift=np.float32([[1,0,100],[0,1,200]])#2行3列。水平移动100,垂直移动200
dst=cv.warpAffine(img,matshift,(width,height))#1data,2mat,3info
cv.imshow('dst',dst)
cv.waitKey(0)

#4.图片镜像
import cv2 as cv
import numpy as np
img=cv.imread('kst.jpg',1)
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
deep=imginfo[2]
newimginfo=(height*2,width,deep)#新图片的info元组信息
dst=np.zeros(newimginfo,np.uint8)#新图片的大小确定
for i in range(height):
    for j in range(width):
        dst[i,j]=img[i,j]#上半部分不变
        #x不变,y=2h-y-1
        dst[height*2-i-1,j]=img[i,j]#下半部分
for i in range(width):
    dst[height,i]=(0,0,255)
cv.imshow('dst',dst)
cv.waitKey(0)

#5.图片仿射:原始图片的三个点(左上,左下,右上)映射到新的图片上
import cv2 as cv
import numpy as np
img=cv.imread('kst.jpg',1)
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
#确定两图片的三个点
matsrc=np.float32([[0,0],[0,height-1],[width-1,0]])
matdst=np.float32([[50,50],[200,height-100],[width-100,50]])
#组合
mataffine=cv.getAffineTransform(matsrc,matdst)#返回仿射矩阵
dst=cv.warpAffine(img,mataffine,(width,height))#新图片的数据,矩阵,大小
cv.imshow('dst',dst)
cv.waitKey(0)

#6.图片旋转
import cv2 as  cv
import numpy as  np
img=cv.imread('kst.jpg',1)
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
#旋转矩阵
matrotate=cv.getRotationMatrix2D((height*0.5,width*0.5),30,0.5)#1center,2angle,3缩放比例
dst=cv.warpAffine(img,matrotate,(width,height))
cv.imshow('dst',dst)
cv.waitKey(0)



#7.图片灰度处理
#方法1:imread
import cv2 as cv
img0=cv.imread('kst.jpg',0)
img1=cv.imread('kst.jpg',1)
print(img0.shape)
print(img1.shape)
#方法2:cvtColor颜色转换
#dst=cv.cvtColor(img,cv.COLOR_BGR2GRAY)#1data,2转换方式
#方法3:R=G=B时是灰度图片,相加取均值
#dst=np.zeros((height,width,3),np.uint8)
#for i in range(height):
#    for j in range(width):
#        (b,g,r)=img[i,j]
#        gray=(int(b)+int(g)+int(r))/3
#        dst[i,j]=np.uint8(gray)
#方法4:心理学公式:gray=r*0.299+g*0.587+b*0.114
dst=np.zeros((height,width,3),np.uint8)
for i in range(height):
    for j in range(width):
        (b,g,r)=img[i,j]
        #gray=int(b)*0.114+int(g)*0.587+int(r)*0.299
        gray=(int(b)*1+int(g)*2+int(r)*1)/4#定点比浮点运算快
        dst[i,j]=np.uint8(gray)

cv.imshow('dst',dst)
cv.waitKey(0)

#8.颜色反转
#8.1灰度反转:255-当前
import cv2 as cv 
import numpy as np
img=cv.imread('kst.jpg',1)
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
cv.imshow('gray',gray)
dst=np.zeros((height,width,1),np.uint8)
for i in range(height):
        for j in range(width):
            graypixel=gray[i,j]
            dst[i,j]=255-graypixel
cv.imshow('dst',dst)
cv.waitKey(0)

#8.颜色反转
#8.2彩色反转:255-r/g/b=r/g/b
import cv2 as cv
import numpy as np
img=cv.imread('kst.jpg',1)
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
dst=np.zeros((height,width,3),np.uint8)
for i in range(height):
    for j in range(width):
        (b,g,r)=img[i,j]
        dst[i,j]=(255-b,255-g,255-r)
cv.imshow('dst',dst)
cv.waitKey(0)

#9.马赛克效果,每10*10个像素都用左上的像素值代替
import cv2 as cv
import numpy as np
img=cv.imread('kst.jpg',1)
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
for m in range(100,300):
    for n in range(100,200):
        #在200*100的方框内
        if m%10==0 and n%10==0:#每10*10的一个小方框内
            for i in range(10):
                for  j in range(10):
                    (b,g,r)=img[m,n]
                    img[i+m,j+n]=(b,g,r)
cv.imshow('img',img)
cv.waitKey(0)
            

#10.毛玻璃效果,每个像素随机取附近像素的值
import cv2 as cv
import numpy as np
import random
img=cv.imread('kst.jpg',1)
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
dst=np.zeros((height,width,3),np.uint8)
mn=8#随机范围不超过8
for m in range(height-mn):
    for n in range(width-mn):#防止随机取值溢出
        index=int(random.random()*8)#0-8
        (b,g,r)=img[m+index,n+index]
        dst[m,n]=(b,g,r)
cv.imshow('dst',dst)
cv.waitKey(0)


#11.图片融合:dst=src1*a+src2*(1-a)
import cv2 as cv
import numpy as np
img1=cv.imread('kst.jpg',1)
img2=cv.imread('timg.jpg',1)
imginfo=img2.shape
height=imginfo[0]
width=imginfo[1]
#ROI确定两个图片要融合的图形大小
roiH=int(height/3)
roiW=int(width/3)
img1ROI=img1[0:roiH,0:roiW]
img2ROI=img2[0:roiH,0:roiW]
#dst
dst=np.zeros((roiH,roiW,3),np.uint8)
dst=cv.addWeighted(img1ROI,0.5,img2ROI,0.5,0)#1src1,2a,3src2,41-a,
#show
cv.imshow('dst',dst)
cv.waitKey(0)

#12.图片边缘检测
#12-1.canny边缘检测:1gray,2高斯滤波去除噪声,3canny边缘检测
import cv2 as cv
img=cv.imread('timg.jpg',1)
#1gray
gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
#2GAUSS
imgG=cv.GaussianBlur(gray,(3,3),0)
#3canny
dst=cv.Canny(imgG,50,50)#1data,2图片卷积操作值后超过这个门限值就认为是边缘,没超过就不是边缘
#show
cv.imshow('dst',dst)
cv.waitKey(0)

#12.图片边缘检测
#12-2sobel算子:1算子模板,2图片卷积,3阈值判决
#  y【1,2,1      x【1,0,-1
#    0,0,0         2,0,-2
 # -1,-2,-1】       1,0,-1】
    
import cv2 as cv
import numpy as np
import math
img=cv.imread('kst.jpg',1)
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
dst=np.zeros((height,width,1),np.uint8)
for i in range(height-2):
    for j in range(width-2):
        gy=gray[i,j]*1+gray[i,j+1]*2+gray[i,j+2]*1-gray[i+2,j]*1-gray[i+2,j+1]*2-gray[i+2,j+2]*1
        gx=gray[i,j]*1-gray[i,j+2]*1+gray[i+1,j]*2-gray[i+1,j+2]*2+gray[i,j+2]*1-gray[i+2,j+2]*1
        grad=math.sqrt(gx*gx+gy*gy)
        if grad>50:
            dst[i,j]=255
        else:
            dst[i,j]=0
cv.imshow('dst',dst)
cv.waitKey(0)

#13.浮雕效果:new=相邻像素只差(体现边缘像素)+固定值(增强浮雕灰度等级)
import cv2 as cv
import numpy as np
img=cv.imread('kst.jpg',1)
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
dst=np.zeros((height,width,1),np.uint8)
for i in range(height):
    for j in range(width-1):
        grayp0=int(gray[i,j])
        grayp1=int(gray[i,j+1])
        newp=grayp0-grayp1+150
        if newp>255:
            newp=255
        if newp<0:
            newp=0
        dst[i,j]=newp
cv.imshow('dst',dst)
cv.waitKey(0)

#14.颜色映射:rgb->>new_rgb
#加深蓝色效果
import cv2 as cv 
import numpy as np
img=cv.imread('kst.jpg',1)
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
dst=np.zeros((height,width,3),np.uint8)
for i in range(height):
    for j in range(width):
        (b,g,r)=img[i,j]
        b=b*1.5
        g=g*1.3
        if b>255:
            b=255
        if g>255:
            g=255
        dst[i,j]=(b,g,r)
cv.imshow('dst',dst)
cv.waitKey(0)

#15.油画特效:1gray,2分割成n*n的小方块,3将0-255划分成几个等级,3映射,4统计替代

import cv2 as cv
import numpy as np
img=cv.imread('kst.jpg',1)
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
dst=np.zeros((height,width,3),np.uint8)
for i in range(4,height-4):
    for j in range(4,width-4):
        array1=np.zeros(8,np.uint8)#灰度等级分为8个
        for m in range(-4,4):#小方块大小是8*8
            for n in range(-4,4):
                p1=int(gray[i+m,j+n]/32)#256/8=32,i+m表示行,j+n表示列,表示每行个像素到底属于哪个灰度等级
                array1[p1]=array1[p1]+1#给array的8个等级中对应的等级加1
        currentMax=array1[0]#先获取0等级的个数
        l=0#等级从0开始
        for k in range(8):
            if currentMax<array1[k]:
                currentMax=array1[k]
                l=k#获取了当前8*8的小格子中,灰度等级个数最多的等级
        for m in range(-4,4):
            for n in range(-4,4):
                if gray[i+m,j+n]>=(l*32) and gray[i+m,j+n]<=((l+l)*32):#取灰度等级最多的像素
                    (b,g,r)=img[i+m,j+n]
        dst[i,j]=(b,g,r)
cv.imshow('dst',dst)
cv.waitKey(0)
                    
                
                


#16.图形绘制
import cv2 as cv
import numpy as np
imginfo=(500,500,3)#height,width,deep
dst=np.zeros(imginfo,np.uint8)#全黑的画布,dst目标图片
#A.绘制line
cv.line(dst,(100,100),(400,400),(0,0,255),20,cv.LINE_AA)
#1dst,2begin,3end,4(bgr),5linewidth,6linestyle
#B.绘制矩形
cv.rectangle(dst,(50,100),(200,300),(0,255,0),8)
#1dst,2左上,3右下,4bgr,5fill=-1填充,大于0=线条宽度
#C.绘制圆形
cv.circle(dst,(250,250),(50),(152,36,56),2)
#1dst,2center,3半径,4bgr,5fill=-1填充,大于0=线条宽度
#D.绘制椭圆,扇形,圆弧
cv.ellipse(dst,(256,256),(150,100),0,0,180,(255,255,0),-1)
#1dst,2center,3两个长短轴,4偏转角度,5起始角度,6终止角度,7bgr,8fill
#E.任意多边形
points=np.array([[150,50],[140,40],[200,170],[250,250],[150,50]],np.int32)
points=points.reshape((-1,1,2))
cv.polylines(dst,[points],True,(0,255,255))

cv.imshow('dst',dst)
cv.waitKey(0)


#17.绘制文字图片
import cv2 as cv
import numpy as np
img=cv.imread('kst.jpg',1)
font=cv.FONT_HERSHEY_SIMPLEX#字体类型
cv.rectangle(img,(200,100),(500,400),(0,255,0),3)
#A.写字
cv.putText(img,'this is kst',(100,300),font,1,(200,100,255),2,cv.LINE_8)
#1dst,2text,3起始坐标,4字体类型,5字体大小,6bgr,7字体的粗细,8线条类型
#B.放图片
height=int(img.shape[0]*0.5)
width=int(img.shape[1]*0.5)
imgresize=cv.resize(img,(width,height))#将图片缩小
for i in range(height):
    for j in range(width):
        img[i+100,j+200]=imgresize[i,j]
        

cv.imshow('src',img)
cv.waitKey(0)

 3.图片美化

#1.彩色图片直方图
import cv2 as cv
import numpy as np
def ImageHist(image,type):
    #color=(255,255,255)
    #windowname='gray'
    if type==31:
        color=(255,0,0)
        windowname='B hist'
    elif type==32:
        color=(0,255,0)
        windowname='G hist'
    elif type==33:
        color=(0,0,255)
        windowname='R hist'
    hist=cv.calcHist([image],[0],None,[256],[0.0,255.0])#统计直方图
    #1【image】,2直方图通道[0]表示灰度直方图,3mask蒙版,4 直方图分成多少分256,5 0-255表示像素等级
    minv,maxv,minl,maxl=cv.minMaxLoc(hist)#灰度值的最小值,最大值,最小值下标,最大值下标
    histimg=np.zeros([256,256,3],np.uint8)
    for h in range(256):
        intenNormal=int(hist[h]*256/maxv)#归一化hist【h】获取每一个直方图的个数
        cv.line(histimg,(h,256),(h,256-intenNormal),color)
    cv.imshow(windowname,histimg)
    return histimg
img=cv.imread('kst.jpg',1)
channels=cv.split(img)#返回3bgr个颜色通道的图像
for i in range(3):#3个颜色通道,各调用一次
    ImageHist(channels[i],31+i)
cv.waitKey(0)

#2.直方图均衡化
import cv2 as cv
import numpy as np
img=cv.imread('kst.jpg',1)
#2.1灰度图直方图均衡化
#gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
#cv.imshow('src',gray)
#dst=cv.equalizeHist(gray)


#2.2彩色图直方图均衡化
cv.imshow('src',img)
#(b,g,r)=cv.split(img)#分解通道
#bh=cv.equalizeHist(b)#分别均衡化
#gh=cv.equalizeHist(g)++
#rh=cv.equalizeHist(r)
#dst=cv.merge((bh,gh,rh))#合并


#2.3yuv直方图均衡化
imgYUV=cv.cvtColor(img,cv.COLOR_BGR2YCrCb)#图片转换成YUV通道
channelYUV=cv.split(imgYUV)#分解
channelYUV[0]=cv.equalizeHist(channelYUV[0])
#channelYUV[1]=cv.equalizeHist(channelYUV[1])
#channelYUV[2]=cv.equalizeHist(channelYUV[2])
merged=cv.merge(channelYUV)
dst=cv.cvtColor(merged,cv.COLOR_YCrCb2BGR)

cv.imshow('dst',dst)
cv.waitKey(0)



#3.图片修补
#3.1图片破损
import cv2 as cv
import numpy as np
img=cv.imread('kst.jpg',1)
for i in range(200,300):
    img[i,200]=(255,255,255)
    img[i,200+1]=(255,255,255)
    img[i,200-1]=(255,255,255)
for i in range(150,250):
    img[250,i]=(255,255,255)
    img[250+1,i]=(255,255,255)
    img[250-1,i]=(255,255,255)
cv.imwrite('damaged.jpg',img)#写入一个jpg文件
cv.imshow('damage',img)
cv.waitKey(0)

#3.2图片修补
import cv2 as cv
import numpy as np
damaged=cv.imread('damaged.jpg',1)#1破损的照片
cv.imshow('damage',damaged)
info=damaged.shape
height=info[0]
width=info[1]
paint=np.zeros((height,width,1),np.uint8)#因为是蒙板,所以cahnnnel是1
#2.修补的地方的array
for i in range(200,300):
    paint[i,200]=255
    paint[i,200+1]=255
    paint[i,200-1]=255
for i in range(150,250):
    paint[250,i]=255
    paint[250+1,i]=255
    paint[250-1,i]=255
cv.imshow('paint',paint)#3.展示修补图片的蒙板
#4.修补
result=cv.inpaint(damaged,paint,3,cv.INPAINT_TELEA)#1破损的图片,2修补的蒙板,3通道,
cv.imshow('result',result)

cv.waitKey(0)

#4.灰度直方图源码:统计每个像素的灰度值的出现概率
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img=cv.imread('kst.jpg',1)
imginfo = img.shape
height=imginfo[0]
width=imginfo[1]
gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
count=np.zeros(256,np.float)#256个灰度等级
for i in range(height):
    for j in range(width):
        pixel=gray[i,j]
        index=int(pixel)
        count[index]=count[index]+1
#计算每个等级的概率
for i in range(256):
    count[i]=count[i]/(height*width)
x=np.linspace(0,255,256)#开始,结束,一共多少份
y=count
plt.bar(x,y,0.9,alpha=1,color='r')
plt.show()
cv.waitKey(0)

#5.彩色直方图源码
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img=cv.imread('kst.jpg',1)
imginfo = img.shape
height=imginfo[0]
width=imginfo[1]
count_b=np.zeros(256,np.float)
count_g=np.zeros(256,np.float)
count_r=np.zeros(256,np.float)
for i in range(height):
    for j in range(width):
        (b,g,r)=img[i,j]
        index_b=int(b)
        index_g=int(g)
        index_r=int(r)
        count_b[index_b]=count_b[index_b]+1
        count_g[index_g]=count_g[index_g]+1
        count_r[index_r]=count_r[index_r]+1
for i in range(256):
    count_b[i]=count_b[i]/(height*width)
    count_g[i]=count_g[i]/(height*width)
    count_r[i]=count_r[i]/(height*width)
    
x=np.linspace(0,255,256)

y1=count_b
plt.figure()
plt.bar(x,y1,0.9,alpha=1,color='b')
y2=count_g
plt.figure()
plt.bar(x,y2,0.9,alpha=1,color='g')
y3=count_r
plt.figure()
plt.bar(x,y3,0.9,alpha=1,color='r')
plt.show()
cv.waitKey(0)

#6.灰度直方图均衡化:累计概率*255=new
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img=cv.imread('kst.jpg',1)
#cv.imshow('src',img)
imginfo = img.shape
height=imginfo[0]
width=imginfo[1]
gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
cv.imshow('src',gray)
count=np.zeros(256,np.float)#256个灰度等级
for i in range(height):
    for j in range(width):
        pixel=gray[i,j]
        index=int(pixel)
        count[index]=count[index]+1
#计算每个等级的概率
for i in range(256):
    count[i]=count[i]/(height*width)
    
#1计算累计概率
sum=float(0)
for i in range(256):
    sum=sum+count[i]
    count[i]=sum
#2计算映射表
map=np.zeros(256,np.uint16)
for i in range(256):
    map[i]=np.uint16(count[i]*255)
#3映射
for i in range(height):
    for j in range(width):
        pixel=gray[i,j]
        gray[i,j]=map[pixel]
cv.imshow('dst',gray)
cv.waitKey(0)

#7.彩色直方图均衡化
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img=cv.imread('kst.jpg',1)
cv.imshow('kst',img)
imginfo = img.shape
height=imginfo[0]
width=imginfo[1]
count_b=np.zeros(256,np.float)
count_g=np.zeros(256,np.float)
count_r=np.zeros(256,np.float)
for i in range(height):
    for j in range(width):
        (b,g,r)=img[i,j]
        index_b=int(b)
        index_g=int(g)
        index_r=int(r)
        count_b[index_b]=count_b[index_b]+1
        count_g[index_g]=count_g[index_g]+1
        count_r[index_r]=count_r[index_r]+1
for i in range(256):
    count_b[i]=count_b[i]/(height*width)
    count_g[i]=count_g[i]/(height*width)
    count_r[i]=count_r[i]/(height*width)
#1计算累计概率
sum_b=float(0)
sum_g=float(0)
sum_r=float(0)
for i in range(256):
    sum_b=sum_b+count_b[i]
    count_b[i]=sum_b
    sum_g=sum_g+count_g[i]
    count_g[i]=sum_g
    sum_r=sum_r+count_r[i]
    count_r[i]=sum_r
#2计算映射表
map_b=np.zeros(256,np.uint16)
map_g=np.zeros(256,np.uint16)
map_r=np.zeros(256,np.uint16)
for i in range(256):
    map_b[i]=np.uint16(count_b[i]*255)
    map_g[i]=np.uint16(count_g[i]*255)
    map_r[i]=np.uint16(count_r[i]*255)
#3映射
dst=np.zeros((height,width,3),np.uint8)
for i in range(height):
    for j in range(width):
        (b,g,r)=img[i,j]
        b=map_b[b]
        g=map_g[g]
        r=map_r[r]
        dst[i,j]=(b,g,r)
cv.imshow('dst',dst)
cv.waitKey(0)

#8.亮度增强
#8.1p=p+40
import cv2 as cv
import numpy as np
img=cv.imread('kst.jpg',1)
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
cv.imshow('src',img)
dst=np.zeros((height,width,3),np.uint8)
for i in range(height):
    for j in range(width):
        (b,g,r)=img[i,j]
        bb=int(b)+40
        gg=int(g)+40
        rr=int(r)+40
        if bb>255:
            bb=255
        if gg>255:
            gg=255
        if rr>255:
            rr=255
        dst[i,j]=(bb,gg,rr)
cv.imshow('dst',dst)
cv.waitKey(0)

#9.磨皮美白:双边滤波
import cv2 as cv
import numpy as np
img=cv.imread('kst.jpg',1)
dst=cv.bilateralFilter(img,15,35,35)
cv.imshow('img',img)
cv.imshow('dst',dst)
cv.waitKey(0)

#10.高斯滤波和均值滤波
#10.1高斯:消除椒盐噪声,图片变模糊
import cv2 as cv
import numpy as np
img=cv.imread('kst.jpg',1)
#dst=cv.GaussianBlur(img,(5,5),1.5)
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
dst=np.zeros((height,width,3),np.uint8)
#10.2均值滤波:6*6的全为1的模板,计算卷积/36=mean,图片模糊
for i in range(3,height-3):
    for j in range(3,width-3):
        sum_b=int(0)
        sum_g=int(0)
        sum_r=int(0)
        for m in range(-3,3):
            for n in range(-3,3):
                (b,g,r)=img[i+m,j+n]
                sum_b=sum_b+int(b)
                sum_g=sum_g+int(g)
                sum_r=sum_r+int(r)
        b=np.uint8(sum_b/36)
        g=np.uint8(sum_g/36)
        r=np.uint8(sum_r/36)
        dst[i,j]=(b,g,r)
cv.imshow('img',img)
cv.imshow('dst',dst)
cv.waitKey(0)

#11.中值滤波:3*3模板,排序,中间值作为像素值
import cv2 as cv
import numpy as np
img=cv.imread('kst.jpg',1)
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
cv.imshow('gray',gray)
dst=np.zeros((height,width,3),np.uint8)
collect=np.zeros(9,np.uint8)#9个邻域像素值
for i in range(1,height-1):
    for j in range(1,width-1):
        k=0#邻域下标值,从0开始
        for m in range(-1,2):
            for n in range(-1,2):
                collect[k]=gray[i+m,j+n]
                k=k+1
        #冒泡法 取中间值
        for k in range(9):
            min=collect[k]
            for t in range(k+1,9):
                if min<collect[t]:
                    min,collect[t]=collect[t],min

        dst[i,j]=collect[4]
cv.imshow('dst',dst)
cv.waitKey(0)

 4.图片与视频的检测识别

#机器学习:1样本,2特征,3分类器,4预测检验
#4.1haar+adaboost:人脸识别
#4.2hog+svm:行人检测


#1,数据:视频分解成图片
import cv2 as cv
cap=cv.VideoCapture('lz.mp4')#获取一个视频文件
isopened=cap.isOpened#判断视频是否可以打开
print(isopened)
fps=cap.get(cv.CAP_PROP_FPS)#获取视频帧率
height=int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))#获取图像height
width=int(cap.get(cv.CAP_PROP_FRAME_WIDTH))#获取图像width
print(fps,height,width)
i=0#一共要多少张图片,从第0张开始
while(isopened):#当视频可以打开时
    if i==10:
        break
    else:
        i=i+1
    (flag,frame)=cap.read()#读取每张(帧)图片的flag(是否读取成功),frame(图片内容)
    filename='image'+str(i)+'.jpg'
    print(filename)
    if flag==True:#读取图片成功的话
        cv.imwrite(filename,frame,[cv.IMWRITE_JPEG_QUALITY,100])#把如片写入本地文件:1文件名,2文件内容,图片质量
print('end!')

##1,数据:图片合成视频
import cv2 as cv
img=cv.imread('image1.jpg',3)
imginfo=img.shape
size=(imginfo[1],imginfo[0])#w,h
print(size)
#写视频的对象:1名称,2编码器,3视频帧率,4每张图片的大小
videowrite=cv.VideoWriter('lz2.mp4',-1,24,size)#对象
for i in range(1,11):
    filename='image'+str(i)+'.jpg'
    img=cv.imread(filename)
    videowrite.write(img)#对象调用写的方法,写一个视频
print('end')

#2.特征:haar特征
#特征:像素经过运算得到的结果,
#目标区分:阈值判决
#haar特征:
#3.分类器:
#haar+adaboost:人脸检测:1load xml,2load jpg,3haar gray, 4detect,5draw
import cv2 as cv
import numpy as np
#1.load xml
face_xml=cv.CascadeClassifier(r'D:\DeepLearning\haarshare\haarcascade_frontalface_default.xml')
eye_xml=cv.CascadeClassifier(r'D:\DeepLearning\haarshare\haarcascade_eye.xml')
#2.load jpg
img=cv.imread('kst.jpg',1)
cv.imshow('src',img)
#3.haar,gray
gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
#4.detect face:
faces=face_xml.detectMultiScale(gray,1.3,7)#1data,2haar模板的缩放比例,3人脸检测的最小像素
print('face=',len(faces))
#5.draw
for (x,y,w,h) in faces:
    cv.rectangle(img,(x,y),(x+w,y+h),(255,0,0),4)
    #4.2detect eye
    roi_face=gray[y:y+h,x:x+w]#人脸区域的灰度图
    roi_color=img[y:y+h,x:x+w]#人脸区域的彩色图
    eyes=eye_xml.detectMultiScale(roi_face)#在灰度图中检测眼睛
    print('eye=',len(eyes))
    #5.2draw eye
    for (e_x,e_y,e_w,e_h) in eyes:
        cv.rectangle(roi_color,(e_x,e_y),(e_x+e_w,e_y+e_h),(0,255,0),2)
cv.imshow('dst',img)
cv.waitKey(0)


#3.SVM
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
#1.准备数据
rand1=np.array([[155,48],[159,50],[164,53],[168,56],[172,60]])
rand2=np.array([[152,53],[156,55],[160,56],[172,64],[176,65]])
#2label
label=np.array([[0],[0],[0],[0],[0],[1],[1],[1],[1],[1]])
#3.data
data=np.vstack((rand1,rand2))
data=np.array(data,dtype='float32')
#4.训练svm
svm=cv.ml.SVM_create()
#设置svm属性
svm.setType(cv.ml.SVM_C_SVC)#type
svm.setKernel(cv.ml.SVM_LINEAR)#line核
svm.setC(0.01)#核属性
#训练
result=svm.train(data,cv.ml.ROW_SAMPLE,label)
#预测
pt_data=np.vstack([[167,55],[162,57]])#0,1
pt_data=np.array(pt_data,dtype='float32')
print(pt_data)
_,pt=svm.predict(pt_data)
print(pt)

#4.hog+svm:1样本,2训练,3预测,识别

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt 
PosNum=820#正样本820个
NegNum=1931#负样本1931个
winsize=(64,128)#win窗口是整张图片大小,1个
blocksize=(16,16)#blok窗口大小,105个,
blockstride=(8,8)#block滑动的步长
cellsize=(8,8)#每个block里面的cell大小,4个
nBin=9#cell里面的方向参数是九个,3780个
#创建hog对象
hog=cv.HOGDescriptor(winsize,blocksize,blockstride,cellsize,nBin)
#创建svm
svm=cv.ml.SVM_create()
#计算hog
featureNum=int(((128-16)/8+1)*((64-16)/8+1)*4*9)#hog特征的维度3780
featureArray=np.zeros(((PosNum+NegNum),featureNum),np.float32)
labelArray=np.zeros(((PosNum+NegNum),1),np.int32)
for i in range(PosNum):
    filename='pos/'+str(i+1)+'.jpg'
    img=cv.imread(filename)
    hist=hog.compute(img,(8,8))#计算hog特征
    #将hog特征装入特征向量:featureArray[i]=hist
    for j in range(featureNum):
        featureArray[i,j]=hist[j]
    labelArray[i,0]=1#正样本标签1
for i in range(NegNum):
    filename='neg/'+str(i+1)+'.jpg'
    img=cv.imread(filename)
    hist=hog.compute(img,(8,8))#计算hog特征
    #将hog特征装入特征向量:featureArray[i]=hist
    for j in range(featureNum):
        featureArray[i+PosNum,j]=hist[j]
    labelArray[i+PosNum,0]=-1
#svm属性设置
svm.setType(cv.ml.SVM_C_SVC)
svm.setKernel(cv.ml.SVM_LINEAR)
svm.setC(0.01)
#训练
ret=svm.train(featureArray,cv.ml.ROW_SAMPLE,labelArray)
#检测
#1.1计算rho
alpha = np.zeros((1),np.float32)
rho = svm.getDecisionFunction(0,alpha)
print(rho)
print(alpha)
#1.2计算resultarray
alphaArray = np.zeros((1,1),np.float32)
supportVArray = np.zeros((1,featureNum),np.float32)
resultArray = np.zeros((1,featureNum),np.float32)
alphaArray[0,0] = alpha
resultArray = -1*alphaArray*supportVArray
# 1.3detect
myDetect = np.zeros((3781),np.float32)
for i in range(0,3780):
    myDetect[i] = resultArray[0,i]
myDetect[3780] = rho[0]
# rho svm (判决)
myHog = cv.HOGDescriptor()
myHog.setSVMDetector(myDetect)
# 1.4load 
imageSrc = cv.imread('Test2.jpg',1)
# (8,8) win 
objs = myHog.detectMultiScale(imageSrc,0,(8,8),(32,32),1.05,2)
# xy wh 三维 最后一维
x = int(objs[0][0][0])
y = int(objs[0][0][1])
w = int(objs[0][0][2])
h = int(objs[0][0][3])
# 绘制展示
cv.rectangle(imageSrc,(x,y),(x+w,y+h),(255,0,0),2)
cv.imshow('dst',imageSrc)
cv.waitKey(0)

#5.KNN手写数字识别
import tensorflow as tf
import numpy as np
import random
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets('MNIST_data',one_hot=True)
#设置属性
trainNum=55000
testNum=10000
trainSize=500
testSize=5
# data 分解 1 trainSize   2范围0-trainNum 3 replace=False 
trainIndex = np.random.choice(trainNum,trainSize,replace=False)#在trainnum中不重复的选择trainsize个数据
testIndex = np.random.choice(testNum,testSize,replace=False)
trainData = mnist.train.images[trainIndex]# 训练图片
trainLabel = mnist.train.labels[trainIndex]# 训练标签
testData = mnist.test.images[testIndex]
testLabel = mnist.test.labels[testIndex]
# 28*28 = 784
print('trainData.shape=',trainData.shape)#500*784 1 图片个数 2 784?
print('trainLabel.shape=',trainLabel.shape)#500*10
print('testData.shape=',testData.shape)#5*784
print('testLabel.shape=',testLabel.shape)#5*10
print('testLabel=',testLabel)# 4 :testData [0]  3:testData[1] 6 
# tf input  784->image
trainDataInput = tf.placeholder(shape=[None,784],dtype=tf.float32)
trainLabelInput = tf.placeholder(shape=[None,10],dtype=tf.float32)
testDataInput = tf.placeholder(shape=[None,784],dtype=tf.float32)
testLabelInput = tf.placeholder(shape=[None,10],dtype=tf.float32)
#knn distance 5*785.  5*1*784
# 5 500 784 (3D) 2500*784
f1 = tf.expand_dims(testDataInput,1) # 维度扩展
f2 = tf.subtract(trainDataInput,f1)# 784 sum(784)
f3 = tf.reduce_sum(tf.abs(f2),reduction_indices=2)# 完成数据累加 784 abs
# 5*500
f4 = tf.negative(f3)# 取反
f5,f6 = tf.nn.top_k(f4,k=4) # 选取f4 最大的四个值
# f3 最小的四个值
# f6 index->trainLabelInput
f7 = tf.gather(trainLabelInput,f6)
# f8 num reduce_sum  reduction_indices=1 '竖直'
f8 = tf.reduce_sum(f7,reduction_indices=1)
# tf.argmax 选取在某一个最大的值 index
f9 = tf.argmax(f8,dimension=1)
# f9 -> test5 image -> 5 num
with tf.Session() as sess:
    # f1 <- testData 5张图片
    p1 = sess.run(f1,feed_dict={testDataInput:testData[0:5]})
    print('p1=',p1.shape)# p1= (5, 1, 784)
    p2 = sess.run(f2,feed_dict={trainDataInput:trainData,testDataInput:testData[0:5]})
    print('p2=',p2.shape)#p2= (5, 500, 784) (1,100)  
    p3 = sess.run(f3,feed_dict={trainDataInput:trainData,testDataInput:testData[0:5]})
    print('p3=',p3.shape)#p3= (5, 500)
    print('p3[0,0]=',p3[0,0]) #130.451 knn distance p3[0,0]= 155.812
    
    p4 = sess.run(f4,feed_dict={trainDataInput:trainData,testDataInput:testData[0:5]})
    print('p4=',p4.shape)
    print('p4[0,0]',p4[0,0])
    
    p5,p6 = sess.run((f5,f6),feed_dict={trainDataInput:trainData,testDataInput:testData[0:5]})
    #p5= (5, 4) 每一张测试图片(5张)分别对应4张最近训练图片
    #p6= (5, 4)
    print('p5=',p5.shape)
    print('p6=',p6.shape)
    print('p5[0,0]',p5[0])
    print('p6[0,0]',p6[0])# p6 index
    
    p7 = sess.run(f7,feed_dict={trainDataInput:trainData,testDataInput:testData[0:5],trainLabelInput:trainLabel})
    print('p7=',p7.shape)#p7= (5, 4, 10)
    print('p7[]',p7)
    
    p8 = sess.run(f8,feed_dict={trainDataInput:trainData,testDataInput:testData[0:5],trainLabelInput:trainLabel})
    print('p8=',p8.shape)
    print('p8[]=',p8)
    
    p9 = sess.run(f9,feed_dict={trainDataInput:trainData,testDataInput:testData[0:5],trainLabelInput:trainLabel})
    print('p9=',p9.shape)
    print('p9[]=',p9)
    
    p10 = np.argmax(testLabel[0:5],axis=1)
    print('p10[]=',p10)
j = 0
for i in range(0,5):
    if p10[i] == p9[i]:
        j = j+1
print('ac=',j*100/5)
    

#6.CNN手写数字识别
#cnn : 1 卷积
# ABC 
# A: 激励函数+矩阵 乘法加法
# A CNN :  pool(激励函数+矩阵 卷积 加法)
# C:激励函数+矩阵 乘法加法(A-》B)
# C:激励函数+矩阵 乘法加法(A-》B) + softmax(矩阵 乘法加法)
# loss:tf.reduce_mean(tf.square(y-layer2))
# loss:code
#1 import 
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
# 2 load data
mnist = input_data.read_data_sets('MNIST_data',one_hot = True)
# 3 input
imageInput = tf.placeholder(tf.float32,[None,784]) # 28*28 
labeInput = tf.placeholder(tf.float32,[None,10]) # knn
# 4 data reshape
# [None,784]->M*28*28*1  2D->4D  28*28 wh 1 channel 
imageInputReshape = tf.reshape(imageInput,[-1,28,28,1])
# 5 卷积 w0 : 卷积内核 5*5 out:32  in:1 
w0 = tf.Variable(tf.truncated_normal([5,5,1,32],stddev = 0.1))
b0 = tf.Variable(tf.constant(0.1,shape=[32]))
# 6 # layer1:激励函数+卷积运算
# imageInputReshape : M*28*28*1  w0:5,5,1,32  
layer1 = tf.nn.relu(tf.nn.conv2d(imageInputReshape,w0,strides=[1,1,1,1],padding='SAME')+b0)
# M*28*28*32
# pool 采样 数据量减少很多M*28*28*32 => M*7*7*32
layer1_pool = tf.nn.max_pool(layer1,ksize=[1,4,4,1],strides=[1,4,4,1],padding='SAME')
# [1 2 3 4]->[4]
# 7 layer2 out : 激励函数+乘加运算:  softmax(激励函数 + 乘加运算)
# [7*7*32,1024]
w1 = tf.Variable(tf.truncated_normal([7*7*32,1024],stddev=0.1))
b1 = tf.Variable(tf.constant(0.1,shape=[1024]))
h_reshape = tf.reshape(layer1_pool,[-1,7*7*32])# M*7*7*32 -> N*N1
# [N*7*7*32]  [7*7*32,1024] = N*1024
h1 = tf.nn.relu(tf.matmul(h_reshape,w1)+b1)
# 7.1 softMax
w2 = tf.Variable(tf.truncated_normal([1024,10],stddev=0.1))
b2 = tf.Variable(tf.constant(0.1,shape=[10]))
pred = tf.nn.softmax(tf.matmul(h1,w2)+b2)# N*1024  1024*10 = N*10
# N*10( 概率 )N1【0.1 0.2 0.4 0.1 0.2 。。。】
# label。        【0 0 0 0 1 0 0 0.。。】
loss0 = labeInput*tf.log(pred)
loss1 = 0
# 7.2 
for m in range(0,500):#  test 100
    for n in range(0,10):
        loss1 = loss1 - loss0[m,n]
loss = loss1/500

# 8 train
train = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
# 9 run
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(100):
        images,labels = mnist.train.next_batch(500)
        sess.run(train,feed_dict={imageInput:images,labeInput:labels})
        
        pred_test = sess.run(pred,feed_dict={imageInput:mnist.test.images,labeInput:labels})
        acc = tf.equal(tf.arg_max(pred_test,1),tf.arg_max(mnist.test.labels,1))
        acc_float = tf.reduce_mean(tf.cast(acc,tf.float32))
        acc_result = sess.run(acc_float,feed_dict={imageInput:mnist.test.images,labeInput:mnist.test.labels})
        print(acc_result)
        
        

#7.爬取网站图片
import urllib
import urllib3
import os
from bs4 import BeautifulSoup
# load url
html = urllib.request.urlopen('https://class.imooc.com/?c=ios&mc_marking=286b51b2a8e40915ea9023c821882e74&mc_channel=L5').read()
# parse url data 1 html 2 'html.parser' 3 'utf-8'
soup = BeautifulSoup(html,'html.parser',from_encoding='utf-8')
# img
images = soup.findAll('img')
print(images)
imageName = 0 
for image in images:
    link = image.get('src')
    print('link=',link)
    link = 'http:'+link
    fileFormat = link[-3:]
    if fileFormat == 'png' or fileFormat == 'jpg':
        fileSavePath = 'D:\\DeepLearning\\'+str(imageName)+'.jpg'
        imageName = imageName +1 
        urllib.request.urlretrieve(link,fileSavePath)

 5.实战案例

import cv2 as cv

#1.打开摄像头: capture=cv.VideoCapture(0)
def video():
    capture=cv.VideoCapture(0)
    while(True):
        flag,frame=capture.read()
        frame=cv.flip(frame,1)#左右调换,上下是-1
        cv.imshow('video',frame)
        c=cv.waitKey(50)
        if c==27:
            break
video()
#cv.waitKey(0)
cv.destroyAllWindows()

 #2彩图取反:dst=cv.bitwise_not(img)
import cv2 as cv
'''def fantu(img):
    height=img.shape[0]
    width=img.shape[1]
    channel=img.shape[2]
    for row in range(height):
        for col in range(width):
            for c in range(channel):
                pv=img[row,col,c]
                img[row,col,c]=255-pv'''
                
def fantu(img):
    dst=cv.bitwise_not(img)
    return dst           
img=cv.imread('kst.jpg',1)
cv.imshow('img',img)
t1=cv.getTickCount()#获取cpu时钟
dst=fantu(img)
t2=cv.getTickCount()
print('time:%s ms'%((t2-t1)/cv.getTickFrequency()*1000))
cv.imshow('dst',dst)
cv.waitKey(0)

#3HSV图像转换,跟踪指定颜色的物体
import cv2 as cv
import numpy as np
def hsv_demo():
    capture=cv.VideoCapture(0)
    while(True):
        flag,frame=capture.read()
        if flag==False:
            break
        hsv=cv.cvtColor(frame,cv.COLOR_BGR2HSV)
        lower_hsv=np.array([26,43,46])
        upper_hsv=np.array([34,255,255])#黄色
        mask=cv.inRange(hsv,lowerb=lower_hsv,upperb=upper_hsv)
        dst=cv.bitwise_and(frame,frame,mask=mask)
        cv.imshow('mask',dst)
        c=cv.waitKey(40)
        if c==27:
            break
hsv_demo()

![HSV.png](attachment:HSV.png)# 

#像素运算:cv.add,cv.divide,cv.multiply,cv.divide
#与运算:cv.bitwise_and,或运算:cv.bitwise_or,非运算:cv.bitwise_not
#提高图片对比度和亮度
import cv2 as cv
import numpy as np
def contrast_brighten(img,c,b):
    h,w,ch=img.shape
    blank=np.zeros([h,w,ch],img.dtype)
    dst=cv.addWeighted(img,c,blank,1-c,b)
    cv.imshow('dst',dst)
    cv.waitKey(0)
img=cv.imread('kst.jpg')
contrast_brighten(img,1.2,1.5)#1data,2对比度,3亮度

#ROI感兴趣区域,泛洪填充
import cv2 as cv
import numpy as np
def fill_color(img):
    copy=img.copy()
    h,w=copy.shape[:2]
    mask=np.zeros([h+2,w+2],np.uint8)#要求mask+2
    #在(30,30)的色素-100到+50的范围内填充黄色
    cv.floodFill(copy,mask,(30,30),(0,255,255),(100,100,100),(50,50,50),cv.FLOODFILL_FIXED_RANGE)
    cv.imshow('dst',copy)
    cv.waitKey(0)
def fill_binary():
    image=np.zeros([400,400,3],np.uint8)
    image[100:300,100:300,:]=255
    cv.imshow('fill_binary',image)
    
    mask=np.ones([402,402,1],np.uint8)
    mask[101:301,101:301]=0
    cv.floodFill(image,mask,(200,200),(100,2,255),cv.FLOODFILL_MASK_ONLY)
    cv.imshow('filled',image)
    cv.waitKey(0)
img=cv.imread('kst.jpg')
cv.imshow('img',img)
#fill_color(img)#1data,2对比度,3亮度
fill_binary()

![%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20200417093158.png](attachment:%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20200417093158.png)# 

#卷积模糊
import cv2 as cv
import numpy as np
def blur_demo(img):
    #均值模糊
    #dst=cv.blur(img,(15,15))#对image图像做(15,1)的卷积核,大小全是1
    #中值模糊
    dst=cv.medianBlur(img,15)
    #高斯模糊
    dst=cv.GaussianBlur(img,(0,0),3)
    cv.imshow('blur',dst)
def clamp(a):
    if a>255:
        return 255
    if a<0:
        return 0
    else:
        return a
#高斯噪声
def gaussian_noise(image):
    h,w,c=image.shape
    for row in range(h):
        for col in range(w):
            s=np.random.normal(0,20,3)
            image[row,col,0]=clamp(image[row,col,0]+s[0])
            image[row,col,1]=clamp(image[row,col,1]+s[1])
            image[row,col,2]=clamp(image[row,col,2]+s[2])
    cv.imshow('dst',image)
img=cv.imread('kst.jpg')
blur_demo(img)
#gaussian_noise(img)
cv.waitKey(0)
cv.destroyAllWindows()

#EPF:边缘保留滤波(高斯双边模糊【美颜效果】,均值迁移)
import cv2 as cv
import numpy as np
img=cv.imread('dz.jpg')
dst=cv.bilateralFilter(img,0,30,15)#高斯
#dst=cv.pyrMeanShiftFiltering(img,10,50)#均值迁移
cv.imshow('dst',dst)



cv.waitKey(0)
cv.destroyAllWindows()

#直方图
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img=cv.imread('dz.jpg')
#plt.hist(img.ravel(),256,[0,255])#bins256,range【0,255】
#plt.show('直方图')

def image_hist(img):
    color=('blue','green','red')
    for i,color in enumerate(color):
        hist=cv.calcHist([img],[i],None,[256],[0,256])#None是mask没有,i是图像的第几个channel
        plt.plot(hist,color=color)
        plt.xlim([0,256])
    plt.show()
#image_hist(img)
#根据图片的直方图求相似性:
def creat_rgb_hist(image):
    h,w,c=image.shape
    rgbhist=np.zeros([16*16*16,1],np.float32)
    bsize=256/16
    for row in range(h):
        for col in range(w):
            b=image[row,col,0]
            g=image[row,col,1]
            r=image[row,col,2]
            index=np.int(b/bsize)*16*16+np.int(g/bsize)*16+np.int(r/bsize)
            rgbhist[np.int(index),0]=rgbhist[np.int(index),0]+1
    return rgbhist
def hist_compare(image1,image2):
    hist1=creat_rgb_hist(image1)
    hist2=creat_rgb_hist(image2)
    match1=cv.compareHist(hist1,hist2,cv.HISTCMP_BHATTACHARYYA)
    match2=cv.compareHist(hist1,hist2,cv.HISTCMP_CORREL)
    match3=cv.compareHist(hist1,hist2,cv.HISTCMP_CHISQR)
    print('巴氏距离:%s,相关性:%s,卡方:%s'%(match1,match2,match3))
    
image1=cv.imread('dz.jpg')
image2=cv.imread('dz.jpg') 
hist_compare(image1,image2)   

#直方图反向投影跟踪
#1.图像的hsv2D直方图
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
def hist2d(image):
    hsv=cv.cvtColor(image,cv.COLOR_BGR2HSV)
    #1image,2channel,3mask,4histsize,5ranges
    hist=cv.calcHist([image],[0,1],None,[32,32],[0,180,0,256])
    plt.imshow(hist,interpolation='nearest')
    plt.title('2d histogram')
    plt.show()
#2.图像的反向投影跟踪
def back_projection(sample,target):
    sample_hsv=cv.cvtColor(sample,cv.COLOR_BGR2HSV)
    target_hsv=cv.cvtColor(target,cv.COLOR_BGR2HSV)#两个图转换成HSV
    samplehist=cv.calcHist([sample_hsv],[0,1],None,[32,32],[0,180,0,256])#计算HSV
    cv.normalize(samplehist,samplehist,0,255,cv.NORM_MINMAX)#标准化HSV
    #反向投影
    dst=cv.calcBackProject([target_hsv],[0,1],samplehist,[0,180,0,256],1)
    cv.imshow('backprojection',dst)
    cv.waitKey(0)
    
src=cv.imread('timg.jpg')
#hist2d(src)

sample=cv.imread('aim.png')
target=cv.imread('timg.jpg')
back_projection(sample,target)

#图像二值化
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
def threshold(img):
    gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    #全局阈值
    #ret,binary=cv.threshold(gray,0,255,cv.THRESH_BINARY | cv.THRESH_OTSU)
    #print('threshold value:%s'%ret)
    #局部阈值#贼清晰,有漫画效果
    dst=cv.adaptiveThreshold(gray,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,25,8)
    
    
    cv.imshow('binary',dst)
    cv.waitKey(0)

img=cv.imread('dz.jpg')
threshold(img)

#金字塔:
import cv2 as cv
import numpy as np
def pyramid_demo(image):
    level=3
    temp=image.copy()
    pyramid_images=[]
    for i in range(level):
        dst=cv.pyrDown(temp)
        pyramid_images.append(dst)
        cv.imshow('pyramid_down_'+str(i),dst)
        temp=dst.copy()
    return pyramid_images
#拉普拉斯金字塔,要用高斯金字塔
def lapalian_demo(image):
    pyramid_images=pyramid_demo(image)
    level=len(pyramid_images)
    for i in range(level-1,-1,-1):
        if (i-1)<0:
            expand=cv.pyrUp(pyramid_images[i],dstsize=image.shape[:2])
            lpls=cv.subtract(image,expand)
            cv.imshow('lapalian_down'+str(i),lpls)
        else:
            expand=cv.pyrUp(pyramid_images[i],dstsize=pyramid_images[i-1].shape[:2])
            lpls=cv.subtract(pyramid_images[i-1],expand)
            cv.imshow('lapalian_down'+str(i),lpls)
    
img=cv.imread('timg.jpg')#图像要是2的n次方的像素
#pyramid_demo(img)
lapalian_demo(img)
cv.waitKey(0)


#边缘提取,一阶导数,sobel算子,
import cv2 as cv
import numpy as np
def sobel_demo(image):
    grad_x=cv.Scharr(image,cv.CV_32F,1,0)#Scharr算子比sobel算子增强边缘,用法一样
    grad_y=cv.Scharr(image,cv.CV_32F,0,1)
    gradx=cv.convertScaleAbs(grad_x)
    grady=cv.convertScaleAbs(grad_y)
    cv.imshow('gradient_x',gradx)
    cv.imshow('gradient_y',grady)
    gradxy=cv.addWeighted(gradx,0.5,grady,0.5,0)
    cv.imshow('gradient',gradxy)
#拉普拉斯算子,二阶导数
def lapalian_demo(image):
    dst=cv.Laplacian(image,cv.CV_32F)
    lpls=cv.convertScaleAbs(dst)
    cv.imshow('dst',dst)


image=cv.imread('kst.jpg')
#sobel_demo(image)
lapalian_demo(image)
cv.waitKey(0)

#canny边缘提取:1高斯模糊,2灰度转换,3计算梯度,4非最大信号抑制,5高低阈值输出二值图像
import cv2 as cv
import numpy as np
def edge_demo(image):
    blurred=cv.GaussianBlur(image,(3,3),0)
    gray=cv.cvtColor(blurred,cv.COLOR_BGR2GRAY)
    xgrad=cv.Sobel(gray,cv.CV_16SC1,1,0)
    ygrad=cv.Sobel(gray,cv.CV_16SC1,0,1)
    edge_output=cv.Canny(xgrad,ygrad,50,150)
    cv.imshow('canny edge',edge_output)
    #边缘彩色化
    dst=cv.bitwise_and(image,image,mask=edge_output)
    cv.imshow('color edge',dst)



img=cv.imread('kst.jpg')
edge_demo(img)
cv.imshow('img',img)
cv.waitKey(0)

#基于霍夫变换的直线检测,圆检测
import cv2 as cv
import numpy as np
def line_detect(image):
    gray=cv.cvtColor(image,cv.COLOR_BGR2GRAY)#灰度图
    edges=cv.Canny(gray,50,150,apertureSize=3)#图像边缘提取
    lines=cv.HoughLinesP(edges,1,np.pi/180,100,minLineLength=30,maxLineGap=20)
    for line in lines:
        x1,y1,x2,y2=line[0]
        cv.line(image,(x1,y1),(x2,y2),(0,0,255),2)
    cv.imshow('lines',image)
def circle_detect(image):
    dst=cv.pyrMeanShiftFiltering(image,10,100)#对图像去噪
    gray=cv.cvtColor(dst,cv.COLOR_BGR2GRAY)
    circles=cv.HoughCircles(gray,cv.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=0,maxRadius=0)
    circles=np.uint16(np.around(circles))
    for i in circles[0,:]:
        cv.circle(image,(i[0],i[1]),i[2],(0,0,255),2)
        cv.circle(image,(i[0],i[1]),2,(255,0,0),2)
    cv.imshow('circle',image)
    
    
image=cv.imread('kst.jpg')
#line_detect(image)
circle_detect(image)
cv.waitKey(0)

#对象测量
import cv2 as cv
import numpy as np
def measure_object(image):
    gray=cv.cvtColor(image,cv.COLOR_BGR2GRAY)
    #cv.THRESH_BINARY_INV二值化取反
    ret,binary=cv.threshold(gray,0,255,cv.THRESH_BINARY_INV|cv.THRESH_OTSU)
    print('threshold value:%s'%ret)#二值化的阈值
    cv.imshow('binary image',binary)
    outimage,contours,hireachy=cv.findContours(binary,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
    for i,contour in enumerate(contours):
        area=cv.contourArea(contour)#轮廓面积
        x,y,w,h=cv.boundingRect(contour)#轮廓矩形
        mm=cv.moments(contour)#轮廓中心矩
        cx=mm['m10']/mm['m00']
        cy=mm['m01']/mm['m00']
         cv.circle(image,(np.int(cx),np.int(cy)),3,(0,255,255),-1)
        cv.rectangle(image,(x,y),(x+w,y+h),(0,0,255),2)
        print('countour area %s'%area)
        #多边形逼近
        approxCurve=cv.approxPolyDP(contour,4,True)
        print(approxCurve.shape)
        if approxCurve.shape[0]>6:#多边形
            cv.drawContours(image,contours,i,(0,0,255),2)
        if approxCurve.shape[0]==4:#四边形
            cv.drawContours(image,contours,i,(0,255,255),2)
        if approxCurve.shape[0]==3:#三角形
            cv.drawContours(image,contours,i,(255,0,255),2)
    cv.imshow('measure',image)
    
image=cv.imread('a.jpg')
measure_object(image)
cv.waitKey(0)

![%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20200418135620.png](attachment:%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20200418135620.png)

#分水岭算法
import cv2 as cv
import numpy as np
def watershed(image):
   # blurred=cv.pyrMeanShiftFiltering(image,10,100)
    gray=cv.cvtColor(image,cv.COLOR_BGR2GRAY)
    ret,binary=cv.threshold(gray,0,255,cv.THRESH_BINARY_INV|cv.THRESH_OTSU)
    cv.imshow('binary',binary)
    #开运算
    #kernel=cv.getStructuringElement(cv.MORPH_RECT,(3,3))
    #mb=cv.morphologyEx(binary,cv.MORPH_OPEN,kernel,iterations=2)
    #sure_bg=cv.dilate(mb,kernel,iterations=3)
    #cv.imshow('opt',sure_bg)
    #距离变换
    dist=cv.distanceTransform(binary,cv.DIST_L2,3)
    dist_output=cv.normalize(dist,0,1.0,cv.NORM_MINMAX)
    cv.imshow('distance',dist_output*50)
    ret,surface=cv.threshold(dist,dist.max()*0.6,255,cv.THRESH_BINARY)
    cv.imshow('surface',surface)
    surface_fg=np.uint8(surface)
    unknow=cv.subtract(binary,surface_fg)
    ret,markers=cv.connectedComponents(surface_fg)
    print(ret)
    markers=markers+1
    markers[unknow==255]=0
    markers=cv.watershed(image,markers=markers)
    image[markers==-1]=[0,0,255]
    cv.imshow('result',image)
    
image=cv.imread('b.jpg')
watershed(image)
cv.waitKey(0)

#人脸检测
import cv2 as cv
import numpy as np
def face_detect(image):
    gray=cv.cvtColor(image,cv.COLOR_BGR2GRAY)
    face_detect=cv.CascadeClassifier(r'D:\DeepLearning\haarshare\haarcascade_frontalface_default.xml')
    faces=face_detect.detectMultiScale(gray,1.1,2)
    for x,y,w,h in faces:
        cv.rectangle(image,(x,y),(x+w,y+h),(0,0,255),2)
    cv.imshow('result',image)
    
capture=cv.VideoCapture(0)
while(True):
    ret,frame=capture.read()
    frame=cv.flip(frame,1)
    face_detect(frame)
    c=cv.waitKey(10)
    if c==27:#esc
        break

import cv2 as cv
import numpy as np
from PIL import Image
import pytesseract as tess

#验证码识别
import cv2 as cv
import numpy as np
from PIL import Image
import pytesseract as tess
def recognize_text(image):
    gray=cv.cvtColor(image,cv.COLOR_BGR2GRAY)
    cv.imshow('gray',gray)
    ret,binary=cv.threshold(gray,0,255,cv.THRESH_BINARY_INV|cv.THRESH_OTSU)
    cv.imshow('binary',binary)
    #去掉干扰项
    kernel=cv.getStructuringElement(cv.MORPH_RECT,(2,2))
    bin1=cv.morphologyEx(binary,cv.MORPH_OPEN,kernel)
    cv.imshow('open',bin1)
    
    
    
    cv.bitwise_not(bin1,bin1)
    textImage=Image.fromarray(bin1)
    text=tess.image_to_string(textImage)
    print('识别结果%s'%text)
image=cv.imread('yzm.png')
recognize_text(image)
cv.imshow('rsc',image)
cv.waitKey(0)
cv.destroyAllWindows()

  

posted @ 2020-06-26 19:52  Turing-dz  阅读(186)  评论(0编辑  收藏  举报