当前位置:   article > 正文

opencv常用函数合集_opencv函数大全

opencv函数大全

1.读取图片

  1. img = cv2.imread("<img_path>") #读取图片,转为三通道
  2. img = cv2.imread("<img_path>", 0) #读取图片,转为灰度图
  3. img = cv2.imread("<img_path>", cv2.IMREAD_UNCHANGED) #保持图片原来的通道

2.保存图片

cv2.imwrite("<file_path>",img) #记得文件路径要加图片后缀

3.展示图片

  1. def cvshow(img, wname=None):
  2. if not wname:
  3. cv2.namedWindow("img", 0)
  4. cv2.resizeWindow("img", int(img.shape[1] / 2), int(img.shape[0] / 2))
  5. cv2.imshow('img', img)
  6. else:
  7. cv2.namedWindow(wname, 0)
  8. cv2.resizeWindow(wname, int(img.shape[1] / 2), int(img.shape[0] / 2))
  9. cv2.imshow(wname, img)
  10. cv2.waitKey(0)
  11. cv2.destroyAllWindows()

4.图片二值化

  1. def binary(img, th):
  2. _, result = cv2.threshold(img, th, 255, cv2.THRESH_BINARY)
  3. return result

 5.找图片轮廓

  1. def findContours(img):
  2. contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  3. x, y, w, h = (0, 0, 0, 0)
  4. for cnt in contours:
  5. area = cv2.contourArea(cnt)
  6. if area > 50:
  7. x, y, w, h = cv2.boundingRect(cnt)
  8. x = int(x)
  9. y = int(y)
  10. w = int(w)
  11. h = int(h)
  12. return x, y, w, h, area

6.填充图片 

  1. def copyMake(img, sy, sx):
  2. sy = int(sy)
  3. sx = int(sx)
  4. if len(img.shape) >= 3:
  5. img = cv2.copyMakeBorder(img, sy, sy, sx, sx, cv2.BORDER_CONSTANT, value=(255, 255, 255))
  6. else:
  7. img = cv2.copyMakeBorder(img, sy, sy, sx, sx, cv2.BORDER_CONSTANT, value=(0, 0, 0))
  8. return img

7.两个图片merge

  1. def alphaMerge(back_img, front_img, front_mask):
  2. # 3C, 1C(上衣mask),3C, 1C
  3. alpha = cv2.merge((front_mask, front_mask, front_mask))
  4. alpha = alpha.astype(float) / 255
  5. foreground = cv2.multiply(alpha, front_img.astype(float))
  6. background = cv2.multiply((1 - alpha), back_img.astype(float))
  7. outImage = foreground + background
  8. return outImage.astype(np.uint8)

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/367137
推荐阅读
相关标签
  

闽ICP备14008679号