色彩变换
from PIL import Image
img_ori = Image. open ( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0630(01)(strawberries_fullcolor).tif' )
img_cmyk = img_ori. convert( "CMYK" )
img_temp = np. array( img_cmyk)
img_c = img_temp[ : , : , 0 ]
img_m = img_temp[ : , : , 1 ]
img_y = img_temp[ : , : , 2 ]
img_k = img_temp[ : , : , 3 ]
plt. figure( figsize= ( 20 , 25 ) )
plt. subplot( 541 ) , plt. imshow( img_cmyk) , plt. title( 'Original CMYK' )
plt. subplot( 545 ) , plt. imshow( img_c, 'gray' ) , plt. title( 'Cyan' )
plt. subplot( 546 ) , plt. imshow( img_m, 'gray' ) , plt. title( 'Magenta' )
plt. subplot( 547 ) , plt. imshow( img_y, 'gray' ) , plt. title( 'Yellow' )
plt. subplot( 5 , 4 , 8 ) , plt. imshow( img_k, 'gray' ) , plt. title( 'Black' )
img_rgb = np. array( img_ori)
plt. subplot( 5 , 4 , 9 ) , plt. imshow( img_rgb[ : , : , 0 ] , 'gray' ) , plt. title( 'Red' )
plt. subplot( 5 , 4 , 10 ) , plt. imshow( img_rgb[ : , : , 1 ] , 'gray' ) , plt. title( 'Green' )
plt. subplot( 5 , 4 , 11 ) , plt. imshow( img_rgb[ : , : , 2 ] , 'gray' ) , plt. title( 'Blue' )
img_hsi = img_ori. convert( "HSV" )
img_hsi = np. array( img_hsi)
plt. subplot( 5 , 4 , 13 ) , plt. imshow( img_hsi[ : , : , 0 ] , 'gray' ) , plt. title( 'Hue' )
plt. subplot( 5 , 4 , 14 ) , plt. imshow( img_hsi[ : , : , 1 ] , 'gray' ) , plt. title( 'Saturation' )
plt. subplot( 5 , 4 , 15 ) , plt. imshow( img_hsi[ : , : , 2 ] , 'gray' ) , plt. title( 'Intensity' )
plt. tight_layout( )
plt. show( )
from PIL import Image
img_ori = Image. open ( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0630(01)(strawberries_fullcolor).tif' )
img_cmyk = img_ori. convert( "CMYK" )
img_temp = np. array( img_cmyk)
img_c = img_temp[ : , : , 0 ]
img_m = img_temp[ : , : , 1 ]
img_y = img_temp[ : , : , 2 ]
img_k = img_temp[ : , : , 3 ]
plt. figure( figsize= ( 20 , 25 ) )
plt. subplot( 541 ) , plt. imshow( img_cmyk) , plt. title( 'Original CMYK' )
plt. subplot( 545 ) , plt. imshow( img_c, 'gray' ) , plt. title( 'Cyan' )
plt. subplot( 546 ) , plt. imshow( img_m, 'gray' ) , plt. title( 'Magenta' )
plt. subplot( 547 ) , plt. imshow( img_y, 'gray' ) , plt. title( 'Yellow' )
plt. subplot( 548 ) , plt. imshow( img_k, 'gray' ) , plt. title( 'Black' )
img_k_new = img_k * 1 + 150
img_cmyk_new = np. dstack( ( img_c, img_m, img_y, img_k_new) )
plt. subplot( 549 ) , plt. imshow( img_cmyk_new, 'gray' ) , plt. title( 'New CMYK' )
plt. tight_layout( )
plt. show( )
def gamma_img ( img, c, gamma) :
img = np. array( img) . astype( float )
output_img = c * img ** gamma
img_scale = np. uint8( ( output_img / output_img. max ( ) ) * 255 )
return img_scale
def sigmoid_plot ( img, scale) :
x = np. linspace( img. min ( ) , img. max ( ) , 500 )
x1 = x - 125
y = 1 / ( 1 + np. exp( - x1 / scale) )
return x, y
plt. plot( x, y)
plt. grid( )
def sigmoid_transform ( img, scale) :
img = np. array( img) . astype( float )
img_temp = ( img - 125 . )
img_new = 1 / ( 1 + np. exp( - img_temp / scale) )
img_new = np. uint8( normalize( img_new) * 255 )
return img_new
from PIL import Image
img_ori = Image. open ( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0635(top_ left_flower).tif' )
plt. figure( figsize= ( 15 , 5 ) )
plt. subplot( 131 ) , plt. imshow( img_ori) , plt. title( 'Original' )
img_colour = sigmoid_transform( img_ori, 30 )
plt. subplot( 132 ) , plt. imshow( img_colour) , plt. title( 'Colour Correct' )
x, y = sigmoid_plot( np. array( img_ori) , 40 )
plt. axes( [ 0.68 , 0.15 , 0.15 , 0.3 ] ) , plt. plot( x, y) , plt. title( "Transform" ) , plt. grid( )
plt. show( )
from PIL import Image
img_ori = Image. open ( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0635(middle_row_left_chalk ).tif' )
plt. figure( figsize= ( 10 , 5 ) )
plt. subplot( 121 ) , plt. imshow( img_ori) , plt. title( 'Original' )
img_colour = gamma_img( img_ori, 1 , 1.5 )
plt. subplot( 1 , 2 , 2 ) , plt. imshow( img_colour) , plt. title( 'Colour Correct' )
plt. tight_layout( )
plt. show( )
from PIL import Image
img_ori = Image. open ( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0635(bottom_left_stream).tif' )
plt. figure( figsize= ( 10 , 5 ) )
plt. subplot( 121 ) , plt. imshow( img_ori) , plt. title( 'Original' )
img_colour = gamma_img( img_ori, 1 , 0.5 )
plt. subplot( 1 , 2 , 2 ) , plt. imshow( img_colour) , plt. title( 'Colour Correct' )
plt. tight_layout( )
plt. show( )
彩色图像平滑和锐化
import numpy as np
def arithmentic_mean ( image, kernel) :
"""
:param image: input image
:param kernel: input kernel
:return: image after convolution
"""
img_h = image. shape[ 0 ]
img_w = image. shape[ 1 ]
m = kernel. shape[ 0 ]
n = kernel. shape[ 1 ]
padding_h = int ( ( m - 1 ) / 2 )
padding_w = int ( ( n - 1 ) / 2 )
image_pad = np. pad( image. copy( ) , ( padding_h, padding_w) , mode= "constant" , constant_values= 0 )
image_convol = image. copy( )
for i in range ( padding_h, img_h + padding_h) :
for j in range ( padding_w, img_w + padding_w) :
temp = np. sum ( image_pad[ i- padding_h: i+ padding_h+ 1 , j- padding_w: j+ padding_w+ 1 ] * kernel)
image_convol[ i - padding_h] [ j - padding_w] = 1 / ( m * n) * temp
image_convol = np. uint8( normalize( image_convol) * 255 )
return image_convol
from PIL import Image
img_ori = cv2. imread( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0638(a)(lenna_RGB).tif' )
img_ori = img_ori[ : , : , : : - 1 ]
plt. figure( figsize= ( 10 , 10 ) )
img_rgb = np. array( img_ori)
plt. subplot( 2 , 2 , 1 ) , plt. imshow( img_rgb) , plt. title( 'RGB' )
plt. subplot( 2 , 2 , 2 ) , plt. imshow( img_rgb[ : , : , 0 ] , 'gray' ) , plt. title( 'Red' )
plt. subplot( 2 , 2 , 3 ) , plt. imshow( img_rgb[ : , : , 1 ] , 'gray' ) , plt. title( 'Green' )
plt. subplot( 2 , 2 , 4 ) , plt. imshow( img_rgb[ : , : , 2 ] , 'gray' ) , plt. title( 'Blue' )
plt. tight_layout( )
plt. show( )
plt. figure( figsize= ( 15 , 5 ) )
img_hsi = cv2. cvtColor( np. array( img_ori) , cv2. COLOR_RGB2HSV)
img_hsi = np. array( img_hsi)
plt. subplot( 1 , 3 , 1 ) , plt. imshow( img_hsi[ : , : , 0 ] , 'gray' ) , plt. title( 'Hue' )
plt. subplot( 1 , 3 , 2 ) , plt. imshow( img_hsi[ : , : , 1 ] , 'gray' ) , plt. title( 'Saturation' )
plt. subplot( 1 , 3 , 3 ) , plt. imshow( img_hsi[ : , : , 2 ] , 'gray' ) , plt. title( 'Intensity' )
plt. tight_layout( )
plt. show( )
mean_kernal = np. ones( [ 5 , 5 ] )
mean_kernal = mean_kernal / ( mean_kernal. size)
img_rgb_new = np. zeros( img_rgb. shape, np. uint8)
for i in range ( 3 ) :
img_temp = img_rgb[ : , : , i]
img_dst = arithmentic_mean( img_temp, kernel= mean_kernal)
img_rgb_new[ : , : , i] = img_dst
img_hsi_new = np. zeros( img_rgb. shape, np. uint8)
for i in range ( 3 ) :
if i == 2 :
img_temp = img_hsi[ : , : , i]
img_dst = arithmentic_mean( img_temp, kernel= mean_kernal)
img_hsi_new[ : , : , i] = img_dst
else :
img_hsi_new[ : , : , i] = img_hsi[ : , : , i]
img_hsi_rgb = cv2. cvtColor( img_hsi_new, cv2. COLOR_HSV2RGB)
img_diff = img_rgb_new - img_hsi_rgb
plt. figure( figsize= ( 15 , 5 ) )
plt. subplot( 1 , 3 , 1 ) , plt. imshow( img_rgb_new) , plt. title( 'RGB' )
plt. subplot( 1 , 3 , 2 ) , plt. imshow( img_hsi_rgb) , plt. title( 'HSI RGB' )
plt. subplot( 1 , 3 , 3 ) , plt. imshow( img_diff) , plt. title( 'Differenc' )
plt. tight_layout( )
plt. show( )
def laplacian_img ( img_gray) :
kernel_laplacian = np. array( (
[ 0 , 1 , 0 ] ,
[ 1 , - 4 , 1 ] ,
[ 0 , 1 , 0 ] ) , np. int8)
imgkernel_laplacian = cv2. filter2D( img_gray, - 1 , kernel_laplacian)
laplacian_img = np. uint8( normalize( img_gray + imgkernel_laplacian) * 255 )
return laplacian_img
img_rgb_new = np. zeros( img_rgb. shape, np. uint8)
for i in range ( 3 ) :
img_temp = img_rgb[ : , : , i]
img_dst = laplacian_img( img_temp)
img_rgb_new[ : , : , i] = img_dst
img_hsi_new = np. zeros( img_rgb. shape, np. uint8)
for i in range ( 3 ) :
if i == 2 :
img_temp = img_hsi[ : , : , i]
img_dst = laplacian_img( img_temp)
img_hsi_new[ : , : , i] = img_dst
else :
img_hsi_new[ : , : , i] = img_hsi[ : , : , i]
img_hsi_rgb = cv2. cvtColor( img_hsi_new, cv2. COLOR_HSV2RGB)
img_diff = img_rgb_new - img_hsi_rgb
plt. figure( figsize= ( 15 , 5 ) )
plt. subplot( 1 , 3 , 1 ) , plt. imshow( img_rgb_new) , plt. title( 'RGB' )
plt. subplot( 1 , 3 , 2 ) , plt. imshow( img_hsi_rgb) , plt. title( 'HSI RGB' )
plt. subplot( 1 , 3 , 3 ) , plt. imshow( img_diff) , plt. title( 'Differenc' )
plt. tight_layout( )
plt. show( )
使用彩色分割图像
HSI 彩色空间中的分割
img_ori = cv2. imread( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0642(a)(jupiter_moon_original).tif' )
img_ori = img_ori[ : , : , : : - 1 ]
plt. figure( figsize= ( 14 , 20 ) )
img_hsi = cv2. cvtColor( np. array( img_ori) , cv2. COLOR_RGB2HSV)
plt. subplot( 4 , 2 , 1 ) , plt. imshow( img_ori) , plt. title( 'Ori' )
plt. subplot( 4 , 2 , 2 ) , plt. imshow( img_hsi[ : , : , 0 ] , 'gray' ) , plt. title( 'Hue' )
plt. subplot( 4 , 2 , 3 ) , plt. imshow( img_hsi[ : , : , 1 ] , 'gray' ) , plt. title( 'Saturation' )
plt. subplot( 4 , 2 , 4 ) , plt. imshow( img_hsi[ : , : , 2 ] , 'gray' ) , plt. title( 'Intensity' )
img_s = normalize( img_hsi[ : , : , 1 ] )
thresh = 0.255
print ( thresh)
img_thresh = img_s. copy( )
img_thresh = np. where( img_thresh <= thresh, img_thresh, 1 )
img_thresh = np. where( img_thresh > thresh, img_thresh, 0 )
plt. subplot( 4 , 2 , 5 ) , plt. imshow( img_thresh, 'gray' ) , plt. title( 'Binary Thred of Saturation' )
img_thred_hue = img_hsi[ : , : , 0 ] * img_thresh
plt. subplot( 4 , 2 , 6 ) , plt. imshow( img_thred_hue, 'gray' ) , plt. title( 'Hue X Binary Thred' )
plt. subplot( 4 , 2 , 7 ) , plt. hist( img_thred_hue. flatten( ) , bins= 256 ) , plt. title( 'Hue X Binary Thred' )
img_binary = img_thred_hue. copy( )
img_binary = np. where( img_binary <= 125 , img_binary, 255 )
img_binary = np. where( img_binary > 125 , img_binary, 0 )
plt. subplot( 4 , 2 , 8 ) , plt. imshow( img_binary, 'gray' ) , plt. title( 'Binary' )
plt. tight_layout( )
plt. show( )
0.255
RGB空间中的分割
欧氏距离 协方差矩阵 边界盒
def rgb_segment ( img_rgb, img_roi, d0) :
"""
RGB spatial domain sementation base of ROI
param: img_rgb: input image, RGB channel
param: img_roi: region of interesting of the image where you want to be seperated
param: d0: the Euculidean distance of the ROI region against others
return: img_dst, a mask image range [0, 1]
"""
mean = np. mean( img_roi, axis= ( 0 , 1 ) )
sigma = np. std( img_roi, axis= ( 0 , 1 ) )
img_dst = np. zeros( img_rgb. shape[ : 2 ] )
height, width = img_dst. shape
for h in range ( height) :
for w in range ( width) :
temp = img_rgb[ h, w]
if np. linalg. norm( temp - mean) <= d0:
img_dst[ h, w] = 1
else :
img_dst[ h, w] = 0
return img_dst
img_ori = cv2. imread( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0642(a)(jupiter_moon_original).tif' )
img_rgb = cv2. cvtColor( img_ori, cv2. COLOR_BGR2RGB)
plt. figure( figsize= ( 14 , 20 ) )
plt. subplot( 4 , 2 , 1 ) , plt. imshow( img_rgb) , plt. title( 'Ori' )
roi = img_rgb[ 240 : 315 , 60 : 98 , : ]
mean = np. mean( roi, axis= ( 0 , 1 ) )
sigma = np. std( roi, axis= ( 0 , 1 ) )
print ( f"RGB mean -> {mean}" )
print ( f"RGB sigma -> {sigma}" )
plt. subplot( 4 , 2 , 3 ) , plt. imshow( roi) , plt. title( 'ROI' )
img_dst = rgb_segment( img_rgb, roi, d0= 38 )
plt. subplot( 4 , 2 , 4 ) , plt. imshow( img_dst, 'gray' ) , plt. title( 'Segment' )
plt. tight_layout( )
plt. show( )
RGB mean -> [146.81298246 40.47473684 42.62385965]
RGB sigma -> [23.60878011 25.67369246 17.97835714]
彩色边缘检测
img1_r = cv2. imread( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0645(a)(RGB1-red).tif' , - 1 )
img1_g = cv2. imread( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0645(b)(RGB1-green).tif' , - 1 )
img1_b = cv2. imread( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0645(c)(RGB1-blue).tif' , - 1 )
img1_rgb = np. dstack( ( img1_r, img1_g, img1_b) )
img2_r = cv2. imread( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0645(e)(RGB2_red).tif' , - 1 )
img2_g = cv2. imread( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0645(f)(RGB2_green).tif' , - 1 )
img2_b = cv2. imread( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0645(g)(RGB2_blue).tif' , - 1 )
img2_rgb = np. dstack( ( img2_r, img2_g, img2_b) )
plt. figure( figsize= ( 20 , 10 ) )
plt. subplot( 2 , 4 , 1 ) , plt. imshow( img1_r, 'gray' ) , plt. title( 'R channel' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 2 , 4 , 2 ) , plt. imshow( img1_g, 'gray' ) , plt. title( 'G channel' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 2 , 4 , 3 ) , plt. imshow( img1_b, 'gray' ) , plt. title( 'B channel' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 2 , 4 , 4 ) , plt. imshow( img1_rgb) , plt. title( 'RGB' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 2 , 4 , 5 ) , plt. imshow( img2_r, 'gray' ) , plt. title( 'R channel' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 2 , 4 , 6 ) , plt. imshow( img2_g, 'gray' ) , plt. title( 'G channel' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 2 , 4 , 7 ) , plt. imshow( img2_b, 'gray' ) , plt. title( 'B channel' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 2 , 4 , 8 ) , plt. imshow( img2_rgb) , plt. title( 'RGB' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. tight_layout( )
plt. show( )
彩色图像中的噪声
img1_r = cv2. imread( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0648(a)(lenna-noise-R-gauss-mean0-var800).tif' , 0 )
img1_g = cv2. imread( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0648(b)(lenna-noise-G-gauss-mean0-var800).tif' , 0 )
img1_b = cv2. imread( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0648(c)(lenna-noise-B-gauss-mean0-var800).tif' , 0 )
img1_rgb = np. dstack( ( img1_r, img1_g, img1_b) )
plt. figure( figsize= ( 10 , 10 ) )
plt. subplot( 2 , 2 , 1 ) , plt. imshow( img1_r, 'gray' ) , plt. title( 'R channel' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 2 , 2 , 2 ) , plt. imshow( img1_g, 'gray' ) , plt. title( 'G channel' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 2 , 2 , 3 ) , plt. imshow( img1_b, 'gray' ) , plt. title( 'B channel' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 2 , 2 , 4 ) , plt. imshow( img1_rgb) , plt. title( 'RGB' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. tight_layout( )
plt. show( )
img1_hsi = cv2. cvtColor( img1_rgb, cv2. COLOR_RGB2HSV_FULL)
plt. figure( figsize= ( 15 , 5 ) )
plt. subplot( 1 , 3 , 1 ) , plt. imshow( img1_hsi[ : , : , 0 ] , 'gray' ) , plt. title( 'Hue' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 1 , 3 , 2 ) , plt. imshow( img1_hsi[ : , : , 1 ] , 'gray' ) , plt. title( 'Saturation' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 1 , 3 , 3 ) , plt. imshow( img1_hsi[ : , : , 2 ] , 'gray' ) , plt. title( 'Intensity' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. tight_layout( )
plt. show( )
img1_ori = cv2. imread( 'DIP_Figures/DIP3E_Original_Images_CH06/Fig0650(a)(rgb_image_G_saltpep_pt05).tif' )
img1_rgb = img1_ori[ : , : , : : - 1 ]
plt. figure( figsize= ( 20 , 10 ) )
img1_hsi = cv2. cvtColor( img1_rgb, cv2. COLOR_RGB2HSV_FULL)
plt. subplot( 2 , 4 , 1 ) , plt. imshow( img1_rgb) , plt. title( 'RGB' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 2 , 4 , 2 ) , plt. imshow( img1_hsi[ : , : , 0 ] , 'gray' ) , plt. title( 'Hue' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 2 , 4 , 3 ) , plt. imshow( img1_hsi[ : , : , 1 ] , 'gray' ) , plt. title( 'Saturation' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 2 , 4 , 4 ) , plt. imshow( img1_hsi[ : , : , 2 ] , 'gray' ) , plt. title( 'Intensity' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 2 , 4 , 5 ) , plt. imshow( img1_rgb[ : , : , 0 ] , 'gray' ) , plt. title( 'R channel' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 2 , 4 , 6 ) , plt. imshow( img1_rgb[ : , : , 1 ] , 'gray' ) , plt. title( 'G channel' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. subplot( 2 , 4 , 7 ) , plt. imshow( img1_rgb[ : , : , 2 ] , 'gray' ) , plt. title( 'B channel' ) , plt. xticks( [ ] ) , plt. yticks( [ ] )
plt. tight_layout( )
plt. show( )