lab6python_kwh2_allenz3

.pdf

School

University of Illinois, Urbana Champaign *

*We aren’t endorsed by this school

Course

420

Subject

Electrical Engineering

Date

Jan 9, 2024

Type

pdf

Pages

9

Uploaded by halobubba2d

Report
10/3/23, 11:07 AM Lab6 file:///C:/Users/bubba/OneDrive/Desktop/academics archive/academicsFA23/ECE420/Lab6.html 1/9
10/3/23, 11:07 AM Lab6 file:///C:/Users/bubba/OneDrive/Desktop/academics archive/academicsFA23/ECE420/Lab6.html 2/9 In [32]: # ECE420 - Spring2017 # Lab6 - Part 1: Histogram Equilization import numpy from scipy import misc import matplotlib.pyplot as plt import imageio import copy # Implement This Function def histeq (pic): print (pic . shape) # Follow the procedures of Histogram Equalizaion # Modify the pixel value of pic directly hist = {} for i in range ( len (pic)): for j in range ( len (pic[ 0 ])): pixel = pic[i][j] hist[pixel] = hist . get(pixel, 0 ) + 1 cdf = {} cumulative_prob = 0 for pixel_value, count in sorted (hist . items()): cumulative_prob += count cdf[pixel_value] = cumulative_prob # Step 3: Normalize the CDF to map it to the full range of pixel values min_pixel_value = 0 max_pixel_value = 65536 normalized_cdf = {} for pixel_value, cdf_value in cdf . items(): normalized_cdf[pixel_value] = int (((cdf_value - min_pixel_value) / ( le n (pic) * len (pic[ 0 ]) - min_pixel_value)) * (max_pixel_value - 1 )) for i in range ( len (pic)): for j in range ( len (pic[ 0 ])): pixel = pic[i][j] pic[i][j] = normalized_cdf[pixel] return pic; # Histogram Equilization eco_origin = imageio . imread( 'eco.tif' ); eco_histeq = copy . deepcopy(eco_origin); # Call to histeq to perform Histogram Equilization eco_histeq = histeq(eco_histeq); # Show the result in two windows fig_eco_origin = plt . figure( 1 ); fig_eco_origin . suptitle( 'Original eco.tif' , fontsize =14 , fontweight = 'bold' ); plt . imshow(eco_origin,cmap = 'gray' ,vmin = 0 , vmax = 65535 ); fig_eco_histeq = plt . figure( 2 ) fig_eco_histeq . suptitle( 'Histrogram Equalized eco.tif' , fontsize =14 , fontweigh t = 'bold' ); plt . imshow(eco_histeq,cmap = 'gray' ,vmin = 0 , vmax = 65535 ); plt . show()
10/3/23, 11:07 AM Lab6 file:///C:/Users/bubba/OneDrive/Desktop/academics archive/academicsFA23/ECE420/Lab6.html 3/9 (512, 672)
10/3/23, 11:07 AM Lab6 file:///C:/Users/bubba/OneDrive/Desktop/academics archive/academicsFA23/ECE420/Lab6.html 4/9 In [58]: # ECE420 - Spring2017 # Lab6 - Part 2: 2D-Convolution import numpy from scipy import misc import matplotlib.pyplot as plt # Function Definition Here # Implement This funtion def conv2 (pic,kernel): kernel = numpy . flipud(numpy . fliplr(kernel)) pic_height, pic_width, num_channels = pic . shape kernel_height, kernel_width = kernel . shape # Calculate the padding required to ensure the output has the same dimensi ons pad_height = kernel_height // 2 pad_width = kernel_width // 2 # Create a new pic with same size but float type dim = numpy . shape(pic) pic_conv = numpy . zeros((dim[ 0 ] + 2* pad_height, dim[ 1 ] + 2* pad_width, dim [ 2 ])) # Iterate through each channel for channel in range (num_channels): # Iterate through the rows of the output image for i in range (pad_height, pic_height - pad_height): # Iterate through the columns of the output image for j in range (pad_width, pic_width - pad_width): # Extract the region of interest from the current channel region = pic[i - pad_height:i + pad_height + 1 , j - pad_width: j + pad_width + 1 , channel] # Perform convolution by element-wise multiplication and summa tion conv_result = np . sum(region * kernel) /255 # Store the result in the corresponding position of the output image pic_conv[i, j, channel] = conv_result return pic_conv # Gaussian Kernel Following the Descriptiong: http://www.mathworks.com/help/im ages/ref/fspecial.html def gengaussian (size =5 ,sigma =3.0 ): if size % 2 ==0 or size<2: print ( 'Size Not Valid' ); return None ; kernel = numpy . zeros((size,size)); for x in range (size): for y in range (size): kernel[x][y] = numpy . exp( - ((x - (size -1 ) /2 ) **2+ (y - (size -1 ) /2 ) **2 ) / ( 2 * sigma **2 )); kernel = kernel / numpy . sum(kernel);
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help