Opencv制作电子签名(涉及知识点:像素过滤,图片通用resize函数,像素大于某个阈值则赋值为其它的像素值)

import cv2


def resize_by_ratio(image, width=None, height=None, inter=cv2.INTER_AREA):
    img_new_size = None
    (h, w) = image.shape[:2] # 获得高度和宽度
    if width is None and height is None: # 如果输入的宽度和高度都为空
        return image # 直接返回原图
    if width is None:
        h_ratio = height / float(h) # 输入高度 / 原始高度 得到比率
        img_new_size = (int(w * h_ratio), height) # 将宽度缩放同样的比例
    else:
        w_ratio = width / float(w)
        img_new_size = (width, int(h * w_ratio))
    resized = cv2.resize(image, img_new_size, interpolation=inter)
    return resized

def guo_lv(img,threshold):
    r, c = img.shape
    print(r,c)
    for i in range(0,r):
        for j in range(0,c):
            if img[i,j]>threshold:
                img[i,j] = 255

img = cv2.imread('../img/qian_ming2.png')
img = resize_by_ratio(img,width=320)
cv2.imshow('img1',img)

img=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
threshold = 120
guo_lv(img,threshold)
cv2.imshow('img',img)





# cv2.imshow('img1',img)
# cv2.imshow('img2',img)
# ret,img_new = cv2.threshold(img, 155, 255, cv2.THRESH_BINARY)
# ret,thresh = cv2.threshold(img,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# cv2.imshow('img',img)
# cv2.imshow('img1',img)
cv2.waitKey(0)

原图:
在这里插入图片描述
执行程序后:
在这里插入图片描述

相关推荐

  1. opencv 存储图像 (.tiff)

    2023-12-08 15:16:03       35 阅读
  2. OpenCV图像统计

    2023-12-08 15:16:03       41 阅读
  3. opencv改变颜色---------c++

    2023-12-08 15:16:03       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-08 15:16:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-08 15:16:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-08 15:16:03       82 阅读
  4. Python语言-面向对象

    2023-12-08 15:16:03       91 阅读

热门阅读

  1. LeeCode每日刷题12.8

    2023-12-08 15:16:03       50 阅读
  2. 附录1、vuepress中的Markdown语法

    2023-12-08 15:16:03       63 阅读
  3. 利用 Python 进行数据分析实验(三)

    2023-12-08 15:16:03       53 阅读
  4. 利用 Python 进行数据分析实验(五)

    2023-12-08 15:16:03       57 阅读
  5. docker网络

    2023-12-08 15:16:03       46 阅读
  6. VBA 数组写入ACCESS

    2023-12-08 15:16:03       62 阅读
  7. 数据结构的存储方式

    2023-12-08 15:16:03       55 阅读