欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python opencv操作深入详解

程序员文章站 2023-08-19 15:22:07
直接读取图片def display_img(file="p.jpeg"): img = cv.imread(file) print (img.shape) cv.imshow('image',i...

直接读取图片

def display_img(file="p.jpeg"):
  img = cv.imread(file)
  print (img.shape)
  cv.imshow('image',img)
  cv.waitkey(0)
  cv.destroyallwindows()

读取灰度图片

def display_gray_img(file="p.jpeg"):
  img = cv.imread(file,cv.imread_grayscale)
  print (img.shape)
  cv.imshow('image',img)
  cv.waitkey(0)
  cv.destroyallwindows()
  cv.imwrite("gray_img.png",img)

读取视频

def display_video(file="sj.mp4"):
  v = cv.videocapture(file)
  if v.isopened():
    open,frame = v.read()
  else:
    open=false

  while open:
    ret,frame = v.read()
    if frame is none:
      break
  
    if ret == true:
      gray = cv.cvtcolor(frame,cv.color_bgr2gray)
      cv.imshow("result",gray)
      if cv.waitkey(10) & 0xff == 27:
        break
  v.release()
  v.waitkey(0)
  v.destroyallwindows()

截取图片

def get_frame_img(file="p.jpeg"):
  img = cv.imread(file)
  print (img.shape)
  cat = img[0:200,0:200]
  cv.imshow('get_frame_img',cat)
  cv.waitkey(0)
  cv.destroyallwindows()

提取rgb通道

def extrats_rgb_img(file="p.jpeg"):
  img = cv.imread(file)
  b,g,r = cv.split(img)
  print (b.shape,g.shape,r.shape)
  new_img = cv.merge((b,g,r))
  print (new_img.shape)

  copy_img_r = img.copy()
  copy_img_r[:,:,0]=0
  copy_img_r[:,:,1]=0
  cv.imshow("r_img",copy_img_r)

  copy_img_g = img.copy()
  copy_img_g[:,:,0]=0
  copy_img_g[:,:,2]=0
  cv.imshow("g_img",copy_img_g)

  copy_img_b = img.copy()
  copy_img_b[:,:,1]=0
  copy_img_b[:,:,2]=0
  cv.imshow("b_img",copy_img_b)

边界填充

def border_fill_img(file="p.jpeg"):
  border_type = [
    cv.border_replicate,#复制法,复制边缘
    cv.border_reflect, #反射法,对感兴趣的图像中的像素在两边进行复制
    cv.border_reflect_101,#反射法,以边缘像素为轴,对称
    cv.border_wrap,#外包装法
    cv.border_constant#常量法,常量填充
    ]
  border_title = [
    "replicate",
    "reflect",
    "reflect_101",
    "wrap",
    "constant"
    ]
  img = cv.imread(file)
  top_size,bottom_size,left_size,right_size = (50,50,50,50)
  plt.subplot(231)
  plt.imshow(img,"gray")#原始图像
  plt.title("orignal")

  for i in range(len(border_type)):
    result = cv.copymakeborder(img,top_size,bottom_size,left_size,right_size,border_type[i])
    plt.subplot(232+i)
    plt.imshow(result,"gray")
    plt.title(border_title[i])

  plt.show()

Python opencv操作深入详解

图像融合,变换

def img_compose(file1="tu.jpeg",file2="gui.jpeg"):
  img_1 = cv.imread(file1)
  img_2 = cv.imread(file2)
  print (img_1.shape)
  print (img_2.shape)
  img_1= cv.resize(img_1,(500,500))
  img_2= cv.resize(img_2,(500,500))
  print (img_1.shape)
  print (img_2.shape)
  res = cv.addweighted(img_1,0.4,img_2,0.6,0)
  plt.imshow(res)
  plt.show()


  res = cv.resize(img_1,(0,0),fx=3,fy=1)
  plt.imshow(res)
  plt.show()

  res = cv.resize(img_2,(0,0),fx=1,fy=3)
  plt.imshow(res)
  plt.show()

Python opencv操作深入详解

二值化处理

def binarization(filepath):
  img = cv2.imread(filepath,0)
  limit = 120
  ret,thresh=cv2.threshold(img,limit,255,cv2.thresh_binary_inv)
  plt.imshow(thresh,'gray')
  plt.show()
  return thresh
binarization('t1.jpg')

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

相关标签: Python opencv