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)

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

原文地址:https://blog.csdn.net/qq_42864343/article/details/134747687

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_45420.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注