#图片修补 import cv2 import numpy as np img = cv2.imread('image0.jpg', 1) #通过代码生成损坏的图片 #画一个三像素的直线 for i in range(200, 300): img[i, 200] = (255,255,255) img[i, 200+1] = (255, 255, 255) img[i, 200-1] = (255, 255, 255) #再画一个 for i in range(150, 250): img[250, i] = (255, 255, 255) img[250+1, i] = (255, 255,255) img[250-1, i] = (255, 255, 255) cv2.imwrite('damaged.jpg', img) cv2.imshow('image', img) cv2.waitKey(0) #对破坏的图片进行修补 #1、损坏的图片2、对坏的图片损坏位置求取 import cv2 import numpy as np img = cv2.imread('damaged,jpg', 1) cv2.imshow('src', img) imgInfo = img.shape height = imgInfo[0] width = imgInfo[1] paint = np.zeros((height,width,1),np.uint8) #把修补范围标记出来 for i in range(200, 300): paint[i, 200] = 255 paint[i,200+1] = 255 paint[i, 200-1] = 255 for j in range(150, 250): paint[250, i] = 255 paint[250+1, i] = 255 paint[250-1, i] = 255 cv2.imshow('paint', paint) #完成图片修补1、损坏的图片2、设置的蒙版 imgDst = cv2.inpaint(img, paint,3, cv2.INPAINT_TELEA) cv2.imshow('image', imgDst) cv2.waitKey(0) #灰度直方图:统计每一个像素灰度出现的概率,X:0-255,Y:P import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread('image0.jpg', 1) imgInfo = img.shape height = imgInfo[0] weight = imgInfo[1] #转化为灰度图 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #count记录每个像素在每一个灰度等级下出现的概率 count = np.zeros(256, np.float) #遍历图片中的数据 for i in range(0, height): for j in range(0,width): pixel = gray[i, j] #获取像素下标 index = int(pixel) #统计每一个灰度等级出现的个数 count[index] = count[index] + 1 for i in range(0,255): #计算出现概率=某像素等级的总数/像素总数 count[i] = count[i] / (height * width) x = np.linspace(0, 255, 256) y = count #绘制直方图 plt.bar(x, y, 0.9,alpha=1, color='b') plt.show() cv2.waitKey(0)
图片处理:对图片进行简单修补、对图片灰度直方图进行绘制
最新推荐文章于 2024-01-10 09:40:08 发布