灰度函数python 灰度函数matlab

'求助'Python怎么判断图片是否为灰度图不知道是搜索方法的问题还是自己不愿意看文档的问题 ,
总之本来想在Python的标准库里面找到一个可以判断是否为灰度图的标准库函数,
但是死活找不到,当新问题的难度比旧问题难时,果断回来解决旧问题,
那就是自己编写一个,效率问题嘛,还是忽略一下 , 不考虑灰度函数python了
参考 怎么读取每个像素的RGB
这里判断是否为灰度图的标准是灰度函数python:每一个像素所对应的R、G、B的值是否相等 。
数字图像处理Python实现图像灰度变换、直方图均衡、均值滤波import CV2
import copy
import numpy as np
import random
使用灰度函数python的是pycharm
因为最近看灰度函数python了《银翼杀手2049》灰度函数python,里面Joi实在是太好看了所以原图像就用Joi了
要求是灰度图像,所以第一步先把图像转化成灰度图像
# 读入原始图像
img = CV2.imread('joi.jpg')
# 灰度化处理
gray = CV2.cvtColor(img, CV2.COLOR_BGR2GRAY)
CV2.imwrite('img.png', gray)
第一个任务是利用分段函数增强灰度对比,我自己随便写了个函数大致是这样灰度函数python的
def chng(a):
if a255/3:
b = a/2
elif a255/3*2:
b = (a-255/3)*2255/6
else:
b = (a-255/3*2)/2255/6255/3*2
return b
rows = img.shape[0]
cols = img.shape[1]
cover = copy.deepcopy(gray)
for i in range(rows):
for j in range(cols):
cover[i][j] = chng(cover[i][j])
CV2.imwrite('cover.png', cover)
下一步是直方图均衡化
# histogram equalization
【灰度函数python 灰度函数matlab】 def hist_equal(img, z_max=255):
H, W = img.shape
# S is the total of pixels
S = H * W * 1.
out = img.copy()
sum_h = 0.
for i in range(1, 255):
ind = np.where(img == i)
sum_h= len(img[ind])
z_prime = z_max / S * sum_h
out[ind] = z_prime
out = out.astype(np.uint8)
return out
covereq = hist_equal(cover)
CV2.imwrite('covereq.png', covereq)
在实现滤波之前先添加高斯噪声和椒盐噪声(代码来源于网络)
不知道这个椒盐噪声的名字是谁起的感觉隔壁小孩都馋哭了
用到了random.gauss()
percentage是噪声占比
def GaussianNoise(src,means,sigma,percetage):
NoiseImg=src
NoiseNum=int(percetage*src.shape[0]*src.shape[1])
for i in range(NoiseNum):
randX=random.randint(0,src.shape[0]-1)
randY=random.randint(0,src.shape[1]-1)
NoiseImg[randX, randY]=NoiseImg[randX,randY] random.gauss(means,sigma)
if NoiseImg[randX, randY] 0:
NoiseImg[randX, randY]=0
elif NoiseImg[randX, randY]255:
NoiseImg[randX, randY]=255
return NoiseImg
def PepperandSalt(src,percetage):
NoiseImg=src
NoiseNum=int(percetage*src.shape[0]*src.shape[1])
for i in range(NoiseNum):
randX=random.randint(0,src.shape[0]-1)
randY=random.randint(0,src.shape[1]-1)
if random.randint(0,1)=0.5:
NoiseImg[randX,randY]=0
else:
NoiseImg[randX,randY]=255
return NoiseImg
covereqg = GaussianNoise(covereq, 2, 4, 0.8)
CV2.imwrite('covereqg.png', covereqg)
covereqps = PepperandSalt(covereq, 0.05)
CV2.imwrite('covereqps.png', covereqps)
下面开始均值滤波和中值滤波了
就以n x n为例,均值滤波就是用这n x n个像素点灰度值的平均值代替中心点,而中值就是中位数代替中心点,边界点周围补0;前两个函数的作用是算出这个点的灰度值,后两个是对整张图片进行
#均值滤波模板
def mean_filter(x, y, step, img):
sum_s = 0
for k in range(x-int(step/2), x int(step/2) 1):
for m in range(y-int(step/2), y int(step/2) 1):
if k-int(step/2)0 or k int(step/2) 1img.shape[0]
or m-int(step/2)0 or m int(step/2) 1img.shape[1]:
sum_s= 0
else:
sum_s= img[k][m] / (step*step)
return sum_s
#中值滤波模板
def median_filter(x, y, step, img):
sum_s=[]
for k in range(x-int(step/2), x int(step/2) 1):
for m in range(y-int(step/2), y int(step/2) 1):
if k-int(step/2)0 or k int(step/2) 1img.shape[0]
or m-int(step/2)0 or m int(step/2) 1img.shape[1]:
sum_s.append(0)
else:
sum_s.append(img[k][m])
sum_s.sort()
return sum_s[(int(step*step/2) 1)]
def median_filter_go(img, n):
img1 = copy.deepcopy(img)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
img1[i][j] = median_filter(i, j, n, img)
return img1
def mean_filter_go(img, n):
img1 = copy.deepcopy(img)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
img1[i][j] = mean_filter(i, j, n, img)
return img1
完整main代码如下灰度函数python:
if __name__ == "__main__":
# 读入原始图像
img = CV2.imread('joi.jpg')
# 灰度化处理
gray = CV2.cvtColor(img, CV2.COLOR_BGR2GRAY)
CV2.imwrite('img.png', gray)
rows = img.shape[0]
cols = img.shape[1]
cover = copy.deepcopy(gray)
for i in range(rows):
for j in range(cols):
cover[i][j] = chng(cover[i][j])
CV2.imwrite('cover.png', cover)
covereq = hist_equal(cover)
CV2.imwrite('covereq.png', covereq)
covereqg = GaussianNoise(covereq, 2, 4, 0.8)
CV2.imwrite('covereqg.png', covereqg)
covereqps = PepperandSalt(covereq, 0.05)
CV2.imwrite('covereqps.png', covereqps)
meanimg3 = mean_filter_go(covereqps, 3)
CV2.imwrite('medimg3.png', meanimg3)
meanimg5 = mean_filter_go(covereqps, 5)
CV2.imwrite('meanimg5.png', meanimg5)
meanimg7 = mean_filter_go(covereqps, 7)
CV2.imwrite('meanimg7.png', meanimg7)
medimg3 = median_filter_go(covereqg, 3)
CV2.imwrite('medimg3.png', medimg3)
medimg5 = median_filter_go(covereqg, 5)
CV2.imwrite('medimg5.png', medimg5)
medimg7 = median_filter_go(covereqg, 7)
CV2.imwrite('medimg7.png', medimg7)
medimg4 = median_filter_go(covereqps, 7)
CV2.imwrite('medimg4.png', medimg4)
python io. imread如何设置参数,使读取的图片为灰度图?方法一灰度函数python:在使用OpenCV读取图片灰度函数python的同时将图片转换为灰度图:
img = cv2.imread(imgfile, cv2.IMREAD_GRAYSCALE)
print("cv2.imread(imgfile, cv2.IMREAD_GRAYSCALE)结果如下:")
print('大?。簕}'.format(img.shape))
print("类型:%s"%type(img))
print(img)
运行结果如下图所示:
方法二:使用OpenCV灰度函数python,先读取图片,然后在转换为灰度图:
img = cv2.imread(imgfile)
#print(img.shape)
#print(img)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #Y = 0.299R0.587G0.114B
print("cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)结果如下:")
print('大小:{}'.format(gray_img.shape))
print("类型:%s" % type(gray_img))
print(gray_img)
运行结果如下:
方法三:使用PIL库中灰度函数python的Image模块:
img = np.array(Image.open(imgfile).convert('L'), 'f') #读取图片,灰度化,转换为数组,L = 0.299R0.587G0.114B 。'f'为float类型
print("Image方法灰度函数python的结果如下:")
print('大?。簕}'.format(img.shape))
print("类型:%s" % type(img))
print(img)
灰度函数python的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于灰度函数matlab、灰度函数python的信息别忘了在本站进行查找喔 。

    推荐阅读