opencv python 的基本 *** 作案例(一) 记录,可查看

opencv python 的基本 *** 作案例(一) 记录,可查看,第1张

import cv2




####图像的读取显示和写入:
# cv2.IMREAD_COLOR:读入一副彩色图像。图像的透明度会被忽略,
# 这是默认参数。
# • cv2.IMREAD_GRAYSCALE:以灰度模式读入图像
# cv2.IMREAD_UNCHANGED:读入一幅图像,并且包括图像的 alpha 通道
# img = cv2.imread('d:/pictures/1.jpg',cv2.IMREAD_UNCHANGED)  ###BGR读入
# cv2.imshow("img",img)
# print(img.shape)
# cv2.waitKey(10000)
# cv2.destroyAllWindows()


# ######建立窗口#####
# cv2.namedWindow('img',cv2.WINDOW_FULLSCREEN)
# img = cv2.imread('d:/pictures/1.jpg')
# cv2.imshow('img', img)
# cv2.waitKey(10000)
# cv2.destroyAllWindows()
#
#
# #####保存######
#
# img = cv2.imread('d:/pictures/1.jpg')
# cv2.imwrite('d:/pictures/3.jpg',img)




#####窗口destory###


# img = cv2.imread('d:/pictures/1.jpg')
# cv2.imshow('img', img)
# if  cv2.waitKey(10000) &  0xff == ord('q'):   #q destory
#     cv2.destroyAllWindows()




#####matplotlib #####

# import matplotlib.pyplot as plt
#
# import cv2
# import numpy as   np
# img = cv2.imread('d:/p/1.jpg')
# plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
# plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
# plt.show()


#######cv2  video ########



# video = cv2.VideoCapture('d:/zzc/002.avi')
#
# while video.isOpened():
#
#
#     ret ,frame = video.read()
#     print(video.get(propId=0))
#     if ret:
#         frame  = cv2.cvtColor(frame , cv2.COLOR_BGR2GRAY)
#         cv2.imshow('img', frame)
#
#     if  cv2.waitKey(1)  &   0xff == ord('q'):
#         break
#
# video.release()
# cv2.destroyAllWindows()


###绘图####

import cv2
import numpy  as  np



# img = cv2.imread('d:/p/1.jpg')
# cv2.line(img,(0,0),(200,300),(255,0,0),5)   ###zhixian
#
# cv2.rectangle(img ,(0,0),(300,300),(255,255,0),5)
#
# cv2.circle(img, (150,150),30,(255,255,255),3)  #circle
#
#
# cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)
#
#
#
# cv2.putText(img, 'hah',(100,100),cv2.FONT_HERSHEY_SIMPLEX,4,(255,255,127),2)
# cv2.imshow('img',img)
# cv2.waitKey(10000)
#
#
# import  cv2 , numpy as np
#
#
# img = cv2.imread('d:/p/1.jpg')
# img[100,100] = [255,255,255]#bgr  #3ge channel
# img[100,100,0] = 155 # b channel
# cv2.imshow('img',img)
#
#
#
# print(img.item(10,10,2))
# print(img.itemset((10,10,2),100) )
# print(img.item(10,10,2))
#
#
# #  上述方法等同
# cv2.waitKey(100000)



# #####获取属性###
#
# import   cv2  ,numpy  as  np
#
#
#
# img = cv2.imread('d:/p/1.jpg', cv2.IMREAD_GRAYSCALE)
#
# cv2.imshow('img',img)
#
#
#
# print(img.shape)  ### hwc --------> 100*100*3   gray ----->   100*100
# print(img.size) #--------------size ==100*100
#
# print(img.dtype)   ######leixing uint8
# cv2.waitKey(10000)


# roi  copy


# import   cv2  ,numpy  as  np
# img = cv2.imread('d:/p/1.jpg', cv2.IMREAD_COLOR)
# imgs = img[200:250,200:400]
# img[50:100,100:300] =imgs  ###copy
#
# cv2.imshow('img',img)
# cv2.waitKey(1000000)



###########合并
#
# import   cv2  ,numpy  as  np
#
#
# img = cv2.imread('d:/p/1.jpg', cv2.IMREAD_COLOR)
#
# # b,g,r= cv2.split(img)
# # print(b)
#
# img[:,:,2] = 0
# img[:,:,1] = 0
# cv2.imshow('img',img)
# cv2.waitKey(1000000)




############填充

#
# import   cv2  ,numpy  as  np
# import matplotlib.pyplot as plt
# img = cv2.imread('d:/p/2.jpg', cv2.IMREAD_COLOR)
# cv2.copyMakeBorder(img,150,150,150,150,cv2.BORDER_REFLECT101)
# # cv2.imshow('img',img)
# # cv2.waitKey(1000000)
#
#
# plt.imshow(img,cmap = None ,interpolation='bicubic')
# plt.xticks([]),plt.yticks([])
#
# plt.show()


###########add +





# import   cv2  ,numpy  as  np
#
# img1=  cv2.imread('d:/p/1.jpg',cv2.IMREAD_COLOR)
# # cv2.resize(img1, (500,500))
# img2=  cv2.imread('d:/p/4.jpg',cv2.IMREAD_COLOR)
# # cv2.resize(img2, (500,500))
# # cv2.imshow('img',img1+img2)
# cv2.imshow('img',cv2.add(img1,img2))
# cv2.imshow('img',cv2.addWeighted(img1,0.7,img2,0.7,0))
# cv2.waitKey(1000000)

###########按位运算


# import cv2
# import numpy as np
# # 加载图像
# img1 = cv2.imread('d:/p/1.jpg')
# img2 = cv2.imread('d:/p/4.jpg')
# # I want to put logo on top-left corner, So I create a ROI
# rows,cols,channels = img2.shape
# roi = img1[0:rows, 0:cols ]
# # Now create a mask of logo and create its inverse mask also
# img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
# ret, mask = cv2.threshold(img2gray, 175, 255, cv2.THRESH_BINARY)
# mask_inv = cv2.bitwise_not(mask)
# # Now black-out the area of logo in ROI
# # 取 roi 中与 mask 中不为零的值对应的像素的值,其他值为 0
# # 注意这里必须有 mask=mask 或者 mask=mask_inv, 其中的 mask= 不能忽略
# img1_bg = cv2.bitwise_and(roi,roi,mask = mask)
# # 取 roi 中与 mask_inv 中不为零的值对应的像素的值,其他值为 0。
# # Take only region of logo from logo image.
# img2_fg = cv2.bitwise_and(img2,img2,mask = mask_inv)
# # Put logo in ROI and modify the main image
# dst = cv2.add(img1_bg,img2_fg)
# img1[0:rows, 0:cols ] = dst
# cv2.imshow('res',img1)
# cv2.waitKey(0)
# cv2.destroyAllWindows()


######性能

# import cv2
# import numpy as np
# img1 = cv2.imread('d:/p/1.jpg')
# e1 = cv2.getTickCount()
# for i in range(5,49,2):
#     img1 = cv2.medianBlur(img1,i)
# e2 = cv2.getTickCount()
# t = (e2 - e1)/cv2.getTickFrequency()
# print(t)

# ##################性能优化
# import cv2
#
# print(cv2.setUseOptimized(False))

###BGR↔Gray 和 BGR↔HSV。




######物体追踪

# import cv2
# import numpy as np
# cap=cv2.VideoCapture(0)
# while(1):
#     # 获取每一帧
#     ret,frame=cap.read()
#     # 转换到 HSV
#     hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
#     # 设定蓝色的阈值
#     lower_blue=np.array([20,100,100])
#     upper_blue=np.array([120 ,255, 255])
#     # 根据阈值构建掩模
#     mask=cv2.inRange(hsv,lower_blue,upper_blue)
#     # 对原图像和掩模进行位运算
#     res=cv2.bitwise_and(frame,frame,mask=mask)
#     # 显示图像
#     cv2.imshow('frame',frame)
#     cv2.imshow('mask',mask)
#     cv2.imshow('res',res)
#     k=cv2.waitKey(5)&0xFF
#     if k==27:
#         break
# # 关闭窗口
# cv2.destroyAllWindows()



###############求hsv

# import cv2
# import numpy as np
# green=np.uint8([[[255,0,0]]])
# hsv_green=cv2.cvtColor(green,cv2.COLOR_BGR2HSV)
# print(hsv_green)



####  几何变换  cv2.getPerspectiveTransform。resize缩放


# import cv2
# import numpy  as  np
#
# img = cv2.imread('d:/p/1.jpg', cv2.IMREAD_COLOR)
#
# res =  cv2.resize(img , None  ,fx=  1  , fy = 2 , interpolation=cv2.INTER_CUBIC)
#
# h,w = img.shape[:2]
# res=cv2.resize(img,(2*w,2*h),interpolation=cv2.INTER_CUBIC)
# while(1):
#     cv2.imshow('res',res)
#     cv2.imshow('img',img)
#     if cv2.waitKey(1) & 0xFF == 27:
#         break
# cv2.destroyAllWindows()


####平移

# import cv2
# import numpy as np
# cap=cv2.VideoCapture(0)
# while(1):
#         # 获取每一帧
#         ret,frame=cap.read()
#         # 转换到 HSV
#         hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
#         # 设定蓝色的阈值
#         lower_blue=np.array([110,50,50])
#         upper_blue=np.array([130,255,255])
#         # 根据阈值构建掩模
#         mask=cv2.inRange(hsv,lower_blue,upper_blue)
#         # 对原图像和掩模进行位运算
#         res=cv2.bitwise_and(frame,frame,mask=mask)
#         # 显示图像
#         cv2.imshow('frame',frame)
#         cv2.imshow('mask',mask)
#         cv2.imshow('res',res)
#         k=cv2.waitKey(5)&0xFF
#         if k==27:
#             break
# # 关闭窗口
# cv2.destroyAllWindows()

#######旋转


# import cv2
# import numpy as np
# img=cv2.imread('d:/p/1.jpg',0)
# rows,cols=img.shape
# # 这里的第一个参数为旋转中心,第二个为旋转角度,第三个为旋转后的缩放因子
# # 可以通过设置旋转中心,缩放因子,以及窗口大小来防止旋转后超出边界的问题
# M=cv2.getRotationMatrix2D((cols/2,rows/2),90,0.6)
# # 第三个参数是输出图像的尺寸中心
# dst=cv2.warpAffine(img,M,(2*cols,2*rows))
# while(1):
#     cv2.imshow('img',dst)
#     if cv2.waitKey(1)&0xFF==27:
#         break
# cv2.destroyAllWindows()

#######仿射变换
# import cv2
# import numpy as np
# from matplotlib import pyplot as plt
# img=cv2.imread('d:/p/1.jpg')
# rows,cols,ch=img.shape
# pts1=np.float32([[50,50],[200,50],[50,200]])
# pts2=np.float32([[10,100],[200,50],[100,250]])
# M=cv2.getAffineTransform(pts1,pts2)
# dst=cv2.warpAffine(img,M,(cols,rows))
# plt.subplot(121,plt.imshow(img),plt.title('Input'))
# plt.subplot(121,plt.imshow(img),plt.title('Output'))
# plt.show()



######透视变换
# import cv2
# import numpy as np
# from matplotlib import pyplot as plt
# img=cv2.imread('d:/p/1.jpg')
# rows,cols,ch=img.shape
# pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
# pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
# M=cv2.getPerspectiveTransform(pts1,pts2)
# dst=cv2.warpPerspective(img,M,(300,300))
# plt.subplot(121,plt.imshow(img),plt.title('Input'))
# plt.subplot(121,plt.imshow(img),plt.title('Output'))
# plt.show()

######金字塔

# import cv2
# import numpy as np,sys
# A = cv2.imread('d:/p/1.jpg')
# B = cv2.imread('d:/p/2.jpg')
# # generate Gaussian pyramid for A
# G = A.copy()
# gpA = [G]
# for i in range(6):
#     G = cv2.pyrDown(G)
#     gpA.append(G)
# # generate Gaussian pyramid for B
# G = B.copy()
# gpB = [G]
# for i in range(6):
#     G = cv2.pyrDown(G)
#     gpB.append(G)
# # generate Laplacian Pyramid for A
# lpA = [gpA[5]]
# for i in range(5,0,-1):
#     GE = cv2.pyrUp(gpA[i])
#     L = cv2.subtract(gpA[i-1],GE)
#     lpA.append(L)
# # generate Laplacian Pyramid for B
# lpB = [gpB[5]]
# for i in range(5,0,-1):
#     GE = cv2.pyrUp(gpB[i])
#     L = cv2.subtract(gpB[i-1],GE)
#     lpB.append(L)
# # Now add left and right halves of images in each level
# #numpy.hstack(tup)
# #Take a sequence of arrays and stack them horizontally
# #to make a single array.
# LS = []
# for la,lb in zip(lpA,lpB):
#     rows,cols,dpt = la.shape
#     ls = np.hstack((la[:,0:cols/2], lb[:,cols/2:]))
#     LS.append(ls)
# # now reconstruct
# ls_ = LS[0]
# for i in range(1,6):
#     ls_ = cv2.pyrUp(ls_)
#     ls_ = cv2.add(ls_, LS[i])
# # image with direct connecting each half
# real = np.hstack((A[:,:cols/2],B[:,cols/2:]))
# cv2.imwrite('Pyramid_blending2.jpg',ls_)
# cv2.imwrite('Direct_blending.jpg',real)
# import cv2
# img = cv2.imread('d:/p/5.jpg',cv2.IMREAD_GRAYSCALE)
# img1 = cv2.imread('d:/p/6.jpg',cv2.IMREAD_GRAYSCALE)
# img = cv2.resize(img,(320,320))
# print(img.shape)
# cv2.imshow('img',cv2.add(img,img1))
#
#
# cv2.waitKey(100000)


###Canny 边界检测
# import cv2
# import numpy as np
# from matplotlib import pyplot as plt
# img = cv2.imread('d:/p/1.jpg',0)
# edges = cv2.Canny(img,100,200)
# plt.subplot(121),plt.imshow(img,cmap = 'gray')
# plt.title('Original Image'), plt.xticks([]), plt.yticks([])
# plt.subplot(122),plt.imshow(edges,cmap = 'gray')
# plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
# plt.show()



####图像梯度
# import  cv2
# import numpy  as  np
#
# from matplotlib import pyplot  as plt
#
#
# img = cv2.imread('d:/p/1.jpg',  0)
# la = cv2.Laplacian(img , cv2.CV_64F)
#
# sox =  cv2.Sobel(img ,  cv2.CV_64F,1,0,ksize= 5 )
#
# soy = cv2.Sobel(img , cv2.CV_64F , 0,1 ,ksize= 5)
#
#
# plt.subplot(2,2,1)  ,plt.imshow(img , cmap = 'gray')
# plt.title('o') , plt.xticks([]) , plt.yticks([])
# # plt.show()
# plt.subplot(2,2,2)  ,plt.imshow(la , cmap = 'gray')
# plt.title('o') , plt.xticks([]) , plt.yticks([])
# # plt.show()
#
# plt.subplot(2,2,3)  ,plt.imshow(sox , cmap = 'gray')
# plt.title('o') , plt.xticks([]) , plt.yticks([])
# # plt.show()
# plt.subplot(2,2,4)  ,plt.imshow(soy , cmap = 'gray')
# plt.title('o') , plt.xticks([]) , plt.yticks([])
# plt.show()

#######透视变换?
#
# import cv2
#
# import  numpy  as  np
#
# import matplotlib.pyplot as plt
#
#
# img  =  cv2.imread('d:/p/1.jpg')
# r, h,c = img.shape
#
# pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
# pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
#
# m = cv2.getPerspectiveTransform(pts1,pts2)
# ds = cv2.warpPerspective(img , m ,(300,300))
# plt.subplot(121,plt.imshow(img),plt.title('a'))
# plt.subplot(121,plt.imshow(img),plt.title('a'))
# plt.show()


#####阈值

# import  cv2
# import matplotlib.pyplot as plt
#
#
# img = cv2.imread('d:/p/1.jpg', 0)
# ret ,rel  = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
# plt.subplot(121), plt.imshow(img)
# plt.show()

#######自适应阈值####

# import  cv2
# import matplotlib.pyplot as plt
# img = cv2.imread('d:/p/1.jpg', 0)
#
# img = cv2.medianBlur(img,5)
#
# ret , rel =  cv2.threshold(img,127,255 , cv2.THRESH_BINARY)
#
# rets = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
# res2 = cv2.adaptiveThreshold(img , 255 ,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 11,2)
#
#
# img = [rel , rets, res2]
# lists = [0,1,2]
#
# for i in range(3):
#     plt.subplot(1,3,i+1), plt.imshow(img[i])
# plt.show()
#
#####图像平滑
###LPf
# import  cv2
# import matplotlib.pyplot as plt
# import numpy  as  np
# img = cv2.imread('d:/p/1.jpg', 0)
#
# x = np.ones((5,5) , np.float32 ) /255.
#
#
# ds = cv2.filter2D(img,0,x)
#
# plt.subplot(121), plt.imshow(ds)
# plt.show()


##HPF

# import  cv2
# import matplotlib.pyplot as plt
# import numpy  as  np
# img = cv2.imread('d:/p/1.jpg', 0)
# ds1 =cv2.boxFilter(img,-1,(5,5),normalize=True)
# # ds= cv2.blur(img,(5,5))
# plt.subplot(121), plt.imshow(ds1) ,plt.show()


####gaosi
# import cv2
# import matplotlib.pyplot as plt
#
# img = cv2.imread('d:/p/1.jpg', 0)
#
# ds = cv2.medianBlur(img ,5) #zhongzhi
# ds1 = cv2.GaussianBlur(img ,(5,5), 0)
# ds2 = cv2.bilateralFilter(img,9,75,75) #shaungbian
#
# plt.subplot(121) , plt.imshow(ds2) ,plt.show()



###jinzita#######
未完。。。。。。。。。。。。。。。。。。。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/870132.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存