日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波4 - 分段线性变换 - 对比度拉伸

發(fā)布時間:2023/12/10 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波4 - 分段线性变换 - 对比度拉伸 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目錄

    • 分段線性變換
        • 對比度拉伸
        • 最大最小值拉伸

分段線性變換

  • 優(yōu)點(diǎn)

  • 形式可以任意復(fù)雜

  • 缺點(diǎn)

  • 要求用戶輸入很多參數(shù)

對比度拉伸

光照不足、成像傳感器的動態(tài)范圍偏小、圖像獲取過程中鏡頭孔徑的設(shè)置錯誤

點(diǎn)(r1,s1)和點(diǎn)(r2,s2)(r_1, s_1)和點(diǎn)(r_2, s_2)(r1?,s1?)點(diǎn)(r2?,s2?)的位置控制變換函數(shù)的形狀

圖3,令(r1,s1)=(rmin,0),(r2,s2)=(rmax,L?1)(r_1, s_1) = (r_{min}, 0), (r_2, s_2) = (r_{max}, L-1)(r1?,s1?)=(rmin?,0),(r2?,s2?)=(rmax?,L?1)
圖4,令(r1,s1)=(m,0),(r2,s2)=(m,L?1),m是平均灰度級(r_1, s_1) = (m, 0), (r_2, s_2) = (m, L-1),m是平均灰度級(r1?,s1?)=(m,0),(r2?,s2?)=(m,L?1)m

def stretch_3(img):"""constrast stretch, $(r_1, s_1) = (r_{min}, 0), (r_2, s_2) = (r_{max}, L-1)$return image stretchuse loop can get right image, but numpy still work, but the return image is more bright(if un normalize, then can get rightresult)"""img_min = img.min()img_max = img.max()#---------------------loop----------------------------- # img_dst = np.zeros(img.shape[:2], np.uint8) # height, width = img.shape[:2]# for h in range(height): # for w in range(width): # temp = img[h, w] # if temp <= img_min: # img_dst[h, w] = 0 # elif temp >= img_max: # img_dst[h, w] = 255 # else: # img_dst[h, w] = int(((temp - img_min) / img_max ) * 255)#-----------------------numpy-----------------------img_dst = np.piecewise(img, [img <= img_min, img <= img_max], [0, lambda x : (((x - img_min)/ img_max) * 255).astype(np.int)])return img_dst def stretch_4(img):"""constrast stretch, $(r_1, s_1) = (r_{min}, 0), (r_2, s_2) = (r_{max}, L-1)$return image stretchuse loop can get right image, but numpy still work, but the return image is more bright(if un normalize, then can get rightresult)"""img_min = np.mean(img).astype(np.int)img_max = img.max()#---------------------loop----------------------------- # img_dst = np.zeros(img.shape[:2], np.uint8) # height, width = img.shape[:2]# for h in range(height): # for w in range(width): # temp = img[h, w] # if temp <= img_min: # img_dst[h, w] = 0 # elif temp > img_min: # img_dst[h, w] = 255 # else: # img_dst[h, w] = int(((temp - img_min) / img_max ) * 255)#-----------------------numpy-----------------------img_dst = np.piecewise(img, [img >= img_min], [lambda x : 255 if x.any() < img_min else 0])return img_dst # 對比度拉伸 img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH03/Fig0310(b)(washed_out_pollen_image).tif', 0)# For ploting the stretch curve x = [0, 96, 182, 255] y = [0, 30, 220, 255]# subplot 3 img_subplot3 = stretch_3(img) img_subplot3 = np.uint8(normalize(img_subplot3) * 255) # subplot 4 img_subplot4 = stretch_4(img)plt.figure(figsize=(16, 16)) plt.subplot(2, 2, 1), plt.plot(x, y), plt.title('s=T(r)') plt.ylabel('Output Value', rotation=90) plt.xlabel('Input Value', rotation=0) plt.subplot(2, 2, 2), plt.imshow(img, cmap='gray', vmin=0, vmax=255), plt.title('Original') plt.subplot(2, 2, 3), plt.imshow(img_subplot3, cmap='gray', vmin=0, vmax=255), plt.title('Transform 3') plt.subplot(2, 2, 4), plt.imshow(img_subplot4, cmap='gray', vmin=0, vmax=255), plt.title('Transform 4') plt.tight_layout() plt.show()

def SLT(img, x1, x2, y1, y2):"""利用opencv, 實(shí)現(xiàn)對比度拉伸"""lut = np.zeros(256)for i in range(256):if i < x1:lut[i] = (y1/x1)*ielif i < x2:lut[i] = ((y2-y1)/(x2-x1))*(i-x1)+y1else:lut[i] = ((y2-255.0)/(x2-255.0))*(i-255.0)+255.0img_output = cv2.LUT(img, lut)img_output = np.uint8(img_output+0.5)return img_output # opencv 對比度拉伸 img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH03/Fig0310(b)(washed_out_pollen_image).tif').astype(np.uint8) img_x1 = 100 img_x2 = 160 img_y1 = 50 img_y2 = 255 output_img = SLT(img, img_x1, img_x2, img_y1, img_y2)plt.figure(figsize=(18, 15)) plt.subplot(1, 2, 1), plt.imshow(img, cmap='gray', vmin=0, vmax=255), plt.title('Original') plt.subplot(1, 2, 2), plt.imshow(output_img, cmap='gray', vmin=0, vmax=255), plt.title('Transform') plt.tight_layout() plt.show()

def sigmoid(x, scale):"""simgoid fuction, return ndarray value [0, 1]param: input x: array like param: input scale: scale of the sigmoid fuction, if 1, then is original sigmoid fuction, if < 1, then the values between 0, 1will be less, if scale very low, then become a binary fuction; if > 1, then the values between 0, 1 will be more, if scalevery high then become a y = x"""y = 1 / (1 + np.exp(-x / scale))return y def sigmoid_transform(image, scale):"""use sigmoid function to stretch constract of the imageparam: input image: [0, 255] uint8 grayscale imageparam: input scale: use scale to change the slope of the stretch curvereturn an [0, 255] uint8 gracyscale image"""img_temp = image.copy().astype(float)img_temp = img_temp - 127 # image.max() //2 because the max of input image might not be 255, so use fixed valueimg_dst = 1 / (1 + np.exp(- img_temp / scale))img_dst = np.uint8(normalize(img_dst) * 255.)return img_dst # 用Sigmoid函數(shù)也可以實(shí)現(xiàn)對比度的拉伸,這樣就不需要輸入過多的參數(shù) img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH03/Fig0310(b)(washed_out_pollen_image).tif', 0)# For ploting the stretch curve x = [0, 96, 182, 255] y = [0, 30, 220, 255]# sigmoid function plot scale = 20 x1 = np.arange(0, 256, 1) x2 = x1 - x1.max() // 2 # Here shift the 0 to the x center, here is 5, so x1 = [-5, 5] t_stretch = sigmoid(x2, scale)# subplot 3 use sigmoid fuction to transform image img_sigmoid = sigmoid_transform(img, scale)plt.figure(figsize=(16, 16)) plt.subplot(2, 2, 1), plt.plot(x, y), plt.title('s=T(r)') plt.ylabel('Output Value', rotation=90) plt.xlabel('Input Value', rotation=0) plt.subplot(2, 2, 2), plt.plot(x1, t_stretch), plt.title('Sigmoid') plt.ylabel('Output Value', rotation=90) plt.xlabel('Input Value', rotation=0) plt.subplot(2, 2, 3), plt.imshow(img, cmap='gray', vmin=0, vmax=255), plt.title('Original') plt.subplot(2, 2, 4), plt.imshow(img_sigmoid, cmap='gray', vmin=0, vmax=255), plt.title('Transform 3') plt.tight_layout() plt.show()

最大最小值拉伸

def max_min_strech(img):"""min max stretch"""max1 = np.max(img)min1 = np.min(img)output_img = (255.0 * (img-min1)) / (max1 - min1) # 注意255.0 而不是255 二者算出的結(jié)果區(qū)別很大output_img = np.uint8(output_img + 0.5)return output_img # 最大最小值拉伸 img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH03/Fig0310(b)(washed_out_pollen_image).tif').astype(np.uint8)output_img = max_min_strech(img)plt.figure(figsize=(20, 10)) plt.subplot(1, 2, 1), plt.imshow(img, cmap='gray'), plt.title('Original') plt.subplot(1, 2, 2), plt.imshow(output_img, cmap='gray'), plt.title('Transform') plt.tight_layout() plt.show()

# 最大最小值拉伸 img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH03/Fig0354(a)(einstein_orig).tif').astype(np.uint8)output_img = max_min_strech(img)plt.figure(figsize=(16, 10)) plt.subplot(1, 2, 1), plt.imshow(img, cmap='gray'), plt.title('Original') plt.subplot(1, 2, 2), plt.imshow(output_img, cmap='gray'), plt.title('Transform') plt.tight_layout() plt.show()

總結(jié)

以上是生活随笔為你收集整理的第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波4 - 分段线性变换 - 对比度拉伸的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。