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)
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
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);
10/3/23, 11:07 AM Lab6 file:///C:/Users/bubba/OneDrive/Desktop/academics archive/academicsFA23/ECE420/Lab6.html 5/9 return kernel # Edge Detection Kernel Source:https://alwaysbusycorner.com/2011/12/02/realbas ic-canvas-tutorial-lesson-11-edge-detection-kernel-builder/ def genxkernel (flag =1 ): if flag == 1 : kernel = numpy . array([[ -1 , 0 , 1 ]] *3 ); else : kernel = numpy . array([[ -1 , 0 , 1 ],[ -2 , 0 , -2 ],[ -1 , 0 , -1 ]]); return kernel def genykernel (flag =1 ): if flag == 1 : kernel = numpy . array([[ -1 , -1 , -1 ],[ 0 , 0 , 0 ],[ 1 , 1 , 1 ]]); else : kernel = numpy . array([[ -1 , -2 , -1 ],[ 0 , 0 , 0 ],[ 1 , 2 , 1 ]]); return kernel # Merge Detected X-Edge and Y-Edge def merge (picx,picy): picshape = numpy . shape(picx); if picshape != numpy . shape(picy): print ( 'Pic Size Not Matched!' ); return picx; picmerge = numpy . zeros(picshape); for row in range (picshape[ 0 ]): for col in range (picshape[ 1 ]): for channel in range (picshape[ 2 ]): picmerge[row][col][channel] = numpy . sqrt((picx[row][col][chann el] **2+ picy[row][col][channel] **2 ) /2 ); picmerge = picmerge . astype(picx . dtype,copy = False ); return picmerge; # Main Function Starts Here!!! # Gaussian Blur Kernel # Read Image and Display kitten_origin = imageio . imread( 'kitten.png' ); fig_kitten_origin = plt . figure( 1 ); fig_kitten_origin . suptitle( 'Original Kitten.png' , fontsize =14 , fontweight = 'bol d' ); plt . imshow(kitten_origin,vmin = 0 , vmax = 255 ); plt . show(block = False ); # Generate Kernel kernel_blur = gengaussian( 3 ); # Apply Convolution kitten_blur = conv2(kitten_origin,kernel_blur) # Display Results fig_kitten_blur = plt . figure( 2 ); fig_kitten_blur . suptitle( 'Blurred Kitten.png' , fontsize =14 , fontweight = 'bol d' ); plt . imshow(kitten_blur,vmin = 0 , vmax = 255 ); plt . show(block = False ); # Edge Detection Kernel # Read Image and Display logo_origin = imageio . imread( 'logo.png' ); fig_logo_origin = plt . figure( 3 );
10/3/23, 11:07 AM Lab6 file:///C:/Users/bubba/OneDrive/Desktop/academics archive/academicsFA23/ECE420/Lab6.html 6/9 fig_logo_origin . suptitle( 'Original Logo.png' , fontsize =14 , fontweight = 'bold' ); plt . imshow(logo_origin,vmin = 0 , vmax = 255 ); plt . show(block = False ); # X-Edge Detection kernel_xedge = genxkernel(); logo_xedge = conv2(logo_origin,kernel_xedge) fig_logo_xedge = plt . figure( 4 ); fig_logo_xedge . suptitle( 'X-Edge Detected Logo.png' , fontsize =14 , fontweight = 'b old' ); plt . imshow(logo_xedge,vmin = 0 , vmax = 255 ); plt . show(block = False ); # Y-Edge Detection kernel_yedge = genykernel(); logo_yedge = conv2(logo_origin,kernel_yedge) fig_logo_yedge = plt . figure( 5 ); fig_logo_yedge . suptitle( 'Y-Edge Detected Logo.png' , fontsize =14 , fontweight = 'b old' ); plt . imshow(logo_yedge,vmin = 0 , vmax = 255 ); plt . show(block = False ); # Merge Edges logo_fulledge = merge(logo_xedge,logo_yedge); fig_logo_fulledge = plt . figure( 6 ); fig_logo_fulledge . suptitle( 'Full-Edge Detected Logo.png' , fontsize =14 , fontwei ght = 'bold' ); plt . imshow(logo_fulledge,vmin = 0 , vmax = 255 ); plt . show();
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
10/3/23, 11:07 AM Lab6 file:///C:/Users/bubba/OneDrive/Desktop/academics archive/academicsFA23/ECE420/Lab6.html 7/9 Clipping input data to the valid range for imshow with RGB data ([0..1] for f loats or [0..255] for integers).
10/3/23, 11:07 AM Lab6 file:///C:/Users/bubba/OneDrive/Desktop/academics archive/academicsFA23/ECE420/Lab6.html 8/9 Clipping input data to the valid range for imshow with RGB data ([0..1] for f loats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for f loats or [0..255] for integers).
10/3/23, 11:07 AM Lab6 file:///C:/Users/bubba/OneDrive/Desktop/academics archive/academicsFA23/ECE420/Lab6.html 9/9 How would you like to store your intermediate results, like histogram and CDF? What could you do to make them more efficient? Why do we need to normalize CDF and how does it works? I stored my intermediate histogram and CDF as dictionaries or hashmaps. You could technically used an array/list to create the CDF but the amount of logic that using a dictionary eliminates outweighs the memory efficiency that an array would give, especially in the context of python where we are just creating a proof of concept. We normalize the cdf by following the equation given and the purpose of it is to scale the cumulative probability values back into desired pixel values. In [ ]:
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