Python opencv操作深入详解

时间:2022-03-11 22:43:38

直接读取图片

  1. def display_img(file="p.jpeg"):
  2. img = cv.imread(file)
  3. print (img.shape)
  4. cv.imshow('image',img)
  5. cv.waitKey(0)
  6. cv.destroyAllWindows()

读取灰度图片

  1. def display_gray_img(file="p.jpeg"):
  2. img = cv.imread(file,cv.IMREAD_GRAYSCALE)
  3. print (img.shape)
  4. cv.imshow('image',img)
  5. cv.waitKey(0)
  6. cv.destroyAllWindows()
  7. cv.imwrite("gray_img.png",img)

读取视频

  1. def display_video(file="sj.mp4"):
  2. v = cv.VideoCapture(file)
  3. if v.isOpened():
  4. open,frame = v.read()
  5. else:
  6. open=False
  7.  
  8. while open:
  9. ret,frame = v.read()
  10. if frame is None:
  11. break
  12.  
  13. if ret == True:
  14. gray = cv.cvtColor(frame,cv.COLOR_BGR2GRAY)
  15. cv.imshow("result",gray)
  16. if cv.waitKey(10) & 0xFF == 27:
  17. break
  18. v.release()
  19. v.waitKey(0)
  20. v.destroyAllWindows()

截取图片

  1. def get_frame_img(file="p.jpeg"):
  2. img = cv.imread(file)
  3. print (img.shape)
  4. cat = img[0:200,0:200]
  5. cv.imshow('get_frame_img',cat)
  6. cv.waitKey(0)
  7. cv.destroyAllWindows()

提取rgb通道

  1. def extrats_rgb_img(file="p.jpeg"):
  2. img = cv.imread(file)
  3. b,g,r = cv.split(img)
  4. print (b.shape,g.shape,r.shape)
  5. new_img = cv.merge((b,g,r))
  6. print (new_img.shape)
  7.  
  8. copy_img_r = img.copy()
  9. copy_img_r[:,:,0]=0
  10. copy_img_r[:,:,1]=0
  11. cv.imshow("r_img",copy_img_r)
  12.  
  13. copy_img_g = img.copy()
  14. copy_img_g[:,:,0]=0
  15. copy_img_g[:,:,2]=0
  16. cv.imshow("g_img",copy_img_g)
  17.  
  18. copy_img_b = img.copy()
  19. copy_img_b[:,:,1]=0
  20. copy_img_b[:,:,2]=0
  21. cv.imshow("b_img",copy_img_b)

边界填充

  1. def border_fill_img(file="p.jpeg"):
  2. border_type = [
  3. cv.BORDER_REPLICATE,#复制法,复制边缘
  4. cv.BORDER_REFLECT, #反射法,对感兴趣的图像中的像素在两边进行复制
  5. cv.BORDER_REFLECT_101,#反射法,以边缘像素为轴,对称
  6. cv.BORDER_WRAP,#外包装法
  7. cv.BORDER_CONSTANT#常量法,常量填充
  8. ]
  9. border_title = [
  10. "REPLICATE",
  11. "REFLECT",
  12. "REFLECT_101",
  13. "WRAP",
  14. "CONSTANT"
  15. ]
  16. img = cv.imread(file)
  17. top_size,bottom_size,left_size,right_size = (50,50,50,50)
  18. plt.subplot(231)
  19. plt.imshow(img,"gray")#原始图像
  20. plt.title("ORIGNAL")
  21.  
  22. for i in range(len(border_type)):
  23. result = cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,border_type[i])
  24. plt.subplot(232+i)
  25. plt.imshow(result,"gray")
  26. plt.title(border_title[i])
  27.  
  28. plt.show()

Python opencv操作深入详解

图像融合,变换

  1. def img_compose(file1="tu.jpeg",file2="gui.jpeg"):
  2. img_1 = cv.imread(file1)
  3. img_2 = cv.imread(file2)
  4. print (img_1.shape)
  5. print (img_2.shape)
  6. img_1= cv.resize(img_1,(500,500))
  7. img_2= cv.resize(img_2,(500,500))
  8. print (img_1.shape)
  9. print (img_2.shape)
  10. res = cv.addWeighted(img_1,0.4,img_2,0.6,0)
  11. plt.imshow(res)
  12. plt.show()
  13.  
  14. res = cv.resize(img_1,(0,0),fx=3,fy=1)
  15. plt.imshow(res)
  16. plt.show()
  17.  
  18. res = cv.resize(img_2,(0,0),fx=1,fy=3)
  19. plt.imshow(res)
  20. plt.show()

Python opencv操作深入详解

二值化处理

  1. def Binarization(filepath):
  2. img = cv2.imread(filepath,0)
  3. limit = 120
  4. ret,thresh=cv2.threshold(img,limit,255,cv2.THRESH_BINARY_INV)
  5. plt.imshow(thresh,'gray')
  6. plt.show()
  7. return thresh
  8. Binarization('t1.jpg')

到此这篇关于Python opencv操作深入详解的文章就介绍到这了,更多相关Python opencv操作内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_38641985/article/details/115023738