import cv2
image = cv2.imread("D:/Anaconda/envs/1.jpg")
cv2.namedWindow("Image")
cv2.imshow("Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2 as cv
import numpy as np
def video_demo():
capture = cv.VideoCapture(0)
while(True):
ret, frame = capture.read()
frame = cv.flip(frame, 1)
cv.imshow("video", frame)
c = cv.waitKey(50)
if c == 27: #esc退出
break
def get_image_info(image):
print(type(image))
print(image.shape)
print(image.size)
print(image.dtype)
pixel_data = np.array(image)
print(pixel_data)
print("-------hello python------")
src = cv.imread("1.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
get_image_info(src)
#video_demo()
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
cv.imwrite("D:/result.png", gray)
cv.waitKey(0)
cv.destroyAllWindows()
import cv2 as cv
import numpy as np
def access_pixels(image):
print(image.shape)
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2]
print("width:%s,height:%s channels:%s"%(width,height,channels))
print("---------hello python-------")
src = cv.imread("1.jpg")
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image",src)
access_pixels(src)
cv.waitKey(0)
cv.destroyAllWindows()
import cv2 as cv
def color_space_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow("gray", gray)
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
cv.imshow("hsv", hsv)
yuv = cv.cvtColor(image, cv.COLOR_BGR2YUV)
cv.imshow("yuv", yuv)
Ycrcb = cv.cvtColor(image, cv.COLOR_BGR2YCrCb)
cv.imshow("ycrcb", Ycrcb)
print("--------hello python--------")
src = cv.imread("1.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
color_space_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()
import cv2 as cv
import numpy as np
"""
读取视频文件
"""
def extract_object_demo():
capture = cv.VideoCapture("1.mp4")
while(True):
ret, frame = capture.read()
if ret == False:
break
hsv = cv.cvtColor(frame,cv.COLOR_BGR2HSV)
lower_hsv = np.array([37, 43, 46])
upper_hsv = np.array([77, 255, 255])
mask = cv.inRange(hsv, lowerb=lower_hsv, upperb=upper_hsv) #二值图像
cv.imshow("video", frame)
cv.imshow("mask", mask)
c = cv.waitKey(40)
if c == 27:
break
print("--------hello python--------")
src = cv.imread("1.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
b, g, r = cv.split(src) #通道分离
cv.imshow("blue", b)
cv.imshow("green", g)
cv.imshow("red", r)
src[:, :, 2] = 0
src = cv.merge([b, g, r])
cv.imshow("changed image", src)
#extract_object_demo()
cv.waitKey(0)
cv.destroyAllWindows()
import cv2 as cv
def add_demo(m1, m2):#像素相加
dst = cv.add(m1, m2)
cv.imshow("add_demo", dst)
def subtract_demo(m1, m2):#相减
dst = cv.subtract(m1, m2)
cv.imshow("subtract_demo", dst)
def divide_demo(m1, m2):
dst = cv.divide(m1, m2)
cv.imshow("divide_demo", dst)
def multiply_demo(m1, m2):
dst = cv.multiply(m1, m2)
cv.imshow("multiply_demo", dst)
def logic_demo(m1, m2):
dst = cv.bitwise_and(m1, m2)#与运算
"""
#或运算
#image = cv.imread("1.jpg")
#dst = cv.bitwise_not(image)
"""
cv.imshow("logic_demo", dst)
def others(m1, m2):#均值运算
M1 = cv.mean(m1) #M1, dev1 = cv.meanStdDec(m1)#标准方差
M2 = cv.mean(m2) #M2, dev2 = cv.meanStdDev(m2)
print(M1)
print(M2)
#print(dev1)
#print(dev2)
print("--------hello python--------")
src1 = cv.imread("2.jpg")
src2 = cv.imread("3.jpg")
print(src1.shape)
print(src2.shape)
cv.namedWindow("image1", cv.WINDOW_NORMAL)
cv.imshow("image1", src1)
cv.imshow("image2", src2)
#others(src1, src2)
#add_demo(src1, src2)
#subtract_demo(src1,src2)
#divide_demo(src1, src2)
#multiply_demo(src1, src2)
logic_demo(src1, src2)
cv.waitKey(0)
cv.destroyAllWindows()
ROI
import cv2 as cv
import numpy as np
print("--------hello python--------")
src = cv.imread("D:/PyCharm Community Edition 2017.3.1/my project/1.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
face = src[50: 100,50: 100]
gray = cv.cvtColor(face, cv.COLOR_GRAY2GRAY)
backface = cv.cvtColor(gray,cv.COLOR_GRAY2BGR)
src[50:250, 100: 300] = backface
cv.imshow("face", src)
cv.waitKey(0)
cv.destroyAllWindows()
泛洪填充
import cv2 as cv
import numpy as np
def fill_color_demo(image):
copyImage = image.copy()
h, w = image.shape[ : 2]
mask = np.zeros([h+2, w+2], np.uint8)
cv.floodFill(copyImage,mask, (30, 30), (0, 255, 255), (100, 100, 100),(50, 50, 50), cv.FLOODFILL_FIXED_RANGE)
cv.imshow("fill_color_demo", copyImage)
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),(0,0,255),cv.FLOODFILL_MASK_ONLY)
cv.imshow("fill binary", image)
print("--------hello python--------")
src = cv.imread("D:/PyCharm Community Edition 2017.3.1/my project/1.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
#fill_color_demo(src)
fill_binary()
cv.waitKey(0)
cv.destroyAllWindows()
cv.FLOODFILL_FIXED_RANGE 改变图像,泛洪填充
cv.FLOODFILL_MASK_ONLY 不改变图像,只填充遮罩层本身,忽略新的颜色值参数
import cv2 as cv
import numpy as np
def blur_demo(image): #均值模糊
dst = cv.blur(image, (1, 15))
cv.imshow("blur_demo", dst)
def median_demo(image): #中值模糊
dst = cv.medianBlur(image, 5)
cv.imshow("median_demo", dst)
def custom_blur_demo(image): #自定义模糊
kernel = np.ones([5, 5], np.float32)/25 #5*5=25
#kernel = np.array([1, 1, 1],[1, 1,1],[1, 1, 1], np.float32)/9 #3*3=9 另一种定义方式
dst = cv.filter2D(image, -1, kernel=kernel)
cv.imshow("custom_blur_demo", dst)
print("---------hello python-------")
src = cv.imread("1.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
custom_blur_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()
import cv2 as cv
import numpy as np
def clamp(pv):
if pv > 255:
return 255
if pv < 0:
return 0
else:
return pv
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)
b = image[row, col, 0] #blue
g = image[row, col, 1]#green
r = image[row, col, 2]#red
image[row, col, 0] = clamp(b+s[0])
image[row, col, 1] = clamp(b+s[1])
image[row, col, 2] = clamp(r+s[2])
cv.imshow("noise image", image)
print("---------hello python-------")
src = cv.imread("1.jpg")
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image",src)
gaussian_noise(src)
cv.waitKey(0)
cv.destroyAllWindows()
计算运行时间方法
print("---------hello python-------")
src = cv.imread("1.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
t1 = cv.getTickCount()
gaussian_noise(src)
t2 = cv.getTickCount()
time = (t2 - t1)/cv.getTickFrequency()
print("time consumer: %s" % (time*1000))
cv.waitKey(0)
cv.destroyAllWindows()
源码来源视频资料