import CV2
創新互聯建站堅信:善待客戶,將會成為終身客戶。我們能堅持多年,是因為我們一直可值得信賴。我們從不忽悠初訪客戶,我們用心做好本職工作,不忘初心,方得始終。十多年網站建設經驗創新互聯建站是成都老牌網站營銷服務商,為您提供網站設計制作、網站建設、網站設計、H5網站設計、網站制作、成都品牌網站建設、成都微信小程序服務,給眾多知名企業提供過好品質的建站服務。
import copy
import numpy as np
import random
使用的是pycharm
因為最近看了《銀翼殺手2049》,里面Joi實在是太好看了所以原圖像就用Joi了
要求是灰度圖像,所以第一步先把圖像轉化成灰度圖像
# 讀入原始圖像
img = CV2.imread('joi.jpg')
# 灰度化處理
gray = CV2.cvtColor(img, CV2.COLOR_BGR2GRAY)
CV2.imwrite('img.png', gray)
第一個任務是利用分段函數增強灰度對比,我自己隨便寫了個函數大致是這樣的
def chng(a):
if a 255/3:
b = a/2
elif a 255/3*2:
b = (a-255/3)*2 + 255/6
else:
b = (a-255/3*2)/2 + 255/6 +255/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
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)+1 img.shape[0]
or m-int(step/2) 0 or m+int(step/2)+1 img.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)+1 img.shape[0]
or m-int(step/2) 0 or m+int(step/2)+1 img.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代碼如下:
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)
方法一:在使用OpenCV讀取圖片的同時將圖片轉換為灰度圖:
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,先讀取圖片,然后在轉換為灰度圖:
img = cv2.imread(imgfile)
#print(img.shape)
#print(img)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #Y = 0.299R + 0.587G + 0.114B
print("cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)結果如下:")
print('大小:{}'.format(gray_img.shape))
print("類型:%s" % type(gray_img))
print(gray_img)
運行結果如下:
方法三:使用PIL庫中的Image模塊:
img = np.array(Image.open(imgfile).convert('L'), 'f') #讀取圖片,灰度化,轉換為數組,L = 0.299R + 0.587G + 0.114B。'f'為float類型
print("Image方法的結果如下:")
print('大小:{}'.format(img.shape))
print("類型:%s" % type(img))
print(img)
圖像的灰度處理:
CV_LOAD_IMAGE_GRAYSCALE,這是最簡單之間的辦法,在加載圖像時直接處理
IplImage* Igray=cvLoadImage("test.jpg",CV_LOAD_IMAGE_GRAYSCALE);
得到的圖像就是單通道的,也能夠用這個函數:CVAPI(void) cvCvtColor( const CvArr* src, CvArr* dst, int code );
code=CV_BGR2GRAY;
opencv還提供了非常多方式,我這邊就不一一舉例了。
標題名稱:灰度函數python opencv python 灰度
當前URL:http://vcdvsql.cn/article16/doieddg.html
成都網站建設公司_創新互聯,為您提供全網營銷推廣、App設計、ChatGPT、企業網站制作、網站導航、標簽優化
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯