继续记录与分享
import numpy as np
import cv2 as cv
from tqdm import tqdm
img = cv.imread('D:/3/001.0.png')
cv.namedWindow("input image", 0)
cv.imshow("input image",img)
读取并显示
def Gray_img(img):
# img_gray = cv.cvtColor(img_2, cv.COLOR_RGB2GRAY)
print(img.shape)
(h, w, c) = img.shape
img_b = img[:, :, 0]
img_g = img[:, :, 1]
img_r = img[:, :, 2]
img_gray = img_r * 0.299 + img_g * 0.587 + img_b * 0.114
img_gray = img_gray.astype(np.uint8) # (1)
cv.namedWindow("gray",0)
cv.imshow('gray', img_gray)
#cv.waitKey(0)
return img_gray
img_gray = Gray_img(img)
灰度图生成
h = img_gray.shape[0]
w = img_gray.shape[1]
## 唯一确定阈值
a = 150
image_sharpen = np.zeros((h,w),np.uint8)
for i in tqdm(range(h)):
for j in range(w):
if(img_gray[i,j]> a ):
image_sharpen[i,j] = 255
else:
image_sharpen[i,j] = 0
#print(new_image)
cv.namedWindow("otsu",0)
cv.imshow("otsu",image_sharpen)
固定阈值二值化
可以看到效果比较差
def custom_blur_demo(image):
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32) #锐化
dst = cv.filter2D(image, -1, kernel=kernel)
cv.namedWindow("sharpen", 0)
cv.imshow("sharpen", dst)
img_sharpen = custom_blur_demo(image_sharpen)
cv.waitKey()
图像锐化