def median_filter(img): """ Implement median filter on the given image. Steps: (1) Pad the image with zero to ensure that the output is of the same size as the input image. (2) Calculate the filtered image. Arg: Input image. Return: Filtered image. """ # TODO: implement this function. img = utils.zero_pad(img, 1, 1) l_fil = 3 temp = [] new = [] img_denoise = [] for k in range(len(img) - l_fil + 1): for l in range(len(img[0]) - l_fil + 1): for i in range(l_fil): for j in range(l_fil): temp.append(img[i + k][j + l]) temp = sorted(temp) med = temp[4] temp = [] new.append(med) img_denoise.append(new) new = [] # return img_denoise img_denoise = np.array(img_denoise) img_denoise = img_denoise.astype(np.uint8) return img_denoise
def get_grad(self, inputs, layer_err): m, n_H, n_W, n_C = layer_err.shape m, n_H_prev, n_W_prev, n_C_prev = self.inputs.shape (f, f, n_C_prev, n_C) = self.fshape dA_prev = np.zeros((m, n_H_prev, n_W_prev, n_C_prev)) A_prev_pad = zero_pad(self.inputs, self.pad) dA_prev_pad = np.zeros(A_prev_pad.shape) total_layer_err = np.sum(layer_err, axis=(0, 1, 2)) filters_err = np.zeros(self.fshape) summed_inputs = np.sum(inputs, axis=0) for i in xrange(m): a_prev_pad = A_prev_pad[i] da_prev_pad = dA_prev_pad[i] for h in xrange(n_H): for w in xrange(n_W): for c in xrange(n_C): vert_start = h * self.strides vert_end = vert_start + f horiz_start = w * self.strides horiz_end = horiz_start + f a_slice = a_prev_pad[vert_start:vert_end, horiz_start:horiz_end, :] filters_err[:, :, :, c] += a_slice * layer_err[i, h, w, c] return filters_err
def median_filter(img): """ Implement median filter on the given image. Steps: (1) Pad the image with zero to ensure that the output is of the same size as the input image. (2) Calculate the filtered image. Arg: Input image. Return: Filtered image. """ # TODO: implement this function. img2 = np.copy(img) # crop() referenced from previous project def crop(img, rmin, rmax, cmin, cmax): """Crops a given image.""" if len(img) < rmax: print('WARNING') patch = img[rmin:rmax] patch = [row[cmin:cmax] for row in patch] return patch # for kernel 3x3, padding is 3/2 = 1 padded_image = utils.zero_pad(img, 1, 1) # print(padded_image) # print(img.shape[0]) for i in range(img.shape[0]): for j in range(img.shape[1]): cropped_img = crop(padded_image, i, i + 3, j, j + 3) median_val = np.median(cropped_img) img2[i][j] = median_val return img2
def median_filter(img): """ Implement median filter on the given image. Steps: (1) Pad the image with zero to ensure that the output is of the same size as the input image. (2) Calculate the filtered image. Arg: Input image. Return: Filtered image. """ # Padding the image padded_img = utils.zero_pad(img, 1, 1) # Implementing the Median Filter for row in range(0, len(img)): for col in range(0, len(img[0])): # Finding the neighbouring pixels for the pixel at location row,col in the image filter_patch = padded_img[row:row + 3] filter_patch = [row[col:col + 3] for row in filter_patch] filter_matrix = [] filter_matrix.extend(filter_patch[0]) filter_matrix.extend(filter_patch[1]) filter_matrix.extend(filter_patch[2]) # Calculating the Median filter_matrix.sort() median_index = int((len(filter_matrix) + 1) / 2) median = filter_matrix[median_index - 1] # Replacing the selected pixel at location row,col with the Median img[row][col] = median return img
def backward(self, layer_err): m, n_H, n_W, n_C = layer_err.shape m, n_H_prev, n_W_prev, n_C_prev = self.inputs.shape (f, f, n_C_prev, n_C) = self.fshape dA_prev = np.zeros((m, n_H_prev, n_W_prev, n_C_prev)) A_prev_pad = zero_pad(self.inputs, self.pad) dA_prev_pad = np.zeros(A_prev_pad.shape) for i in xrange(m): a_prev_pad = A_prev_pad[i] da_prev_pad = dA_prev_pad[i] for h in xrange(n_H): for w in xrange(n_W): for c in xrange(n_C): vert_start = h * self.strides vert_end = vert_start + f horiz_start = w * self.strides horiz_end = horiz_start + f a_slice = a_prev_pad[vert_start:vert_end, horiz_start:horiz_end, :] da_prev_pad[ vert_start:vert_end, horiz_start: horiz_end, :] += self.filters[:, :, :, c] * layer_err[i, h, w, c] dA_prev[i, :, :, :] = da_prev_pad[self.pad:-self.pad, self.pad:-self.pad, :] return dA_prev
def ordered_fast_1d_haar_transform(signal): """ Calculate the (ordered) 1D Haar transform of a signal. Notes ----- - Copies the signal before operating on it. - Be sure to invert it using the ordered algorithm. - If the signal is not length power of two, it will be zero padded. """ # Set up overhead variables s = zero_pad(signal) num_sweeps = int(log(len(s), 2)) a = s.copy() new_a = s.copy() for _ in range(num_sweeps): calculations = [((first + second) / 2, (first - second) / 2) for first, second in pairs(new_a)] new_a, c = zip(*calculations) new_a = np.array(new_a) c = np.array(c) # New signal is [a, c] a[:len(new_a)] = new_a[:] a[len(new_a):len(new_a) + len(c)] = c[:] return a
def median_filter(img): """ Implement median filter on the given image. Steps: (1) Pad the image with zero to ensure that the output is of the same size as the input image. (2) Calculate the filtered image. Arg: Input image. Return: Filtered image. """ img = utils.zero_pad(img, 1, 1) median_list = list() temp_img = np.zeros((512, 512)) for i in range(0, 512): for j in range(0, 512): temp_list = list() part = img[i:i + 3, j:j + 3] #print ("current iteration",i,j) for x in range(0, 3): for y in range(0, 3): temp_list.append(part[x, y]) temp_list.sort() median = temp_list[4] temp_img[i, j] = median median_list.append(median) # print (temp_img.dtype) temp_img = temp_img.astype('uint8') # print (temp_img.dtype) return temp_img
def ordered_inverse_fast_1d_haar_transform(signal): """ Calculate the (ordered) inverse 1D Haar transform of a signal. Notes ----- - Copies the signal before operating on it. - Be sure that the signal was produced using the ordered algorithm. - If the signal is not length power of two, it will be zero padded. """ s = zero_pad(signal) num_sweeps = int(log(len(s), 2)) a = s.copy() # This algorithm starts by modifying 2 entries, then 4, then 8, and so on # The new entries are sums and differences of pairs of elements # BUT the pairs of elements are strided apart # The stride is 1, 2, 4, ... for stride_pow in range(num_sweeps): stride = 2**stride_pow size_a = 2 * stride new_a = a[:size_a].copy() for i in range(stride): new_a[2 * i], new_a[2 * i + 1] = a[i] + a[i + stride], a[i] - a[i + stride] a[:size_a] = new_a[:] return a
def inplace_inverse_fast_1d_haar_transform(signal): """ Calculate the (inplace) inverse 1D Haar transform of a signal. Notes ----- - Copies the signal before operating on it. - Be sure that the signal was produced using the inplace algorithm. - If the signal is not length power of two, it will be zero padded. """ # Set up overhead variables s = zero_pad(signal) n = int(log(len(s), 2)) J = len(s) I = J // 2 M = 1 # Perform the algorithm described in the text for _ in range(n): for K in range(M): s[J * K], s[J * K + I] = s[J * K] + s[J * K + I], s[J * K] - s[J * K + I] J = I I = I // 2 M *= 2 return s
def inplace_fast_1d_haar_transform(signal): """ Calculate the (inplace) 1D Haar transform of a signal. Notes ----- - Copies the signal before operating on it. - Be sure to invert it using the inplace algorithm. - If the signal is not length power of two, it will be zero padded. """ # Set up overhead variables s = zero_pad(signal) n = int(log(len(s), 2)) I = 1 J = 2 M = len(s) # Perform algorithm described by text for _ in range(n): M = M // 2 for K in range(M): s[J * K], s[J * K + I] = (s[J * K] + s[J * K + I]) / 2, (s[J * K] - s[J * K + I]) / 2 I = J J *= 2 return s
def median_filter(img): """ Implement median filter on the given image. Steps: (1) Pad the image with zero to ensure that the output is of the same size as the input image. (2) Calculate the filtered image. Arg: Input image. Return: Filtered image. """ filter_size = 3 pad = int(filter_size / 2) out_img = np.empty_like(img) # pad zeros for 3x3 filter padded_img = utils.zero_pad(img, pad, pad) # use the square median filter rows, columns = padded_img.shape for i in range(pad, rows - pad): for j in range(pad, columns - pad): out_img[i - pad, j - pad] = np.median(padded_img[i - pad:i + pad + 1, j - pad:j + pad + 1]) # print(np.sort(padded_img[i-pad:i+pad+1, j-pad:j+pad])) return out_img
def median_filter(img): img2 = img img = utils.zero_pad(img, 1, 1) for x in range(1, len(img) - 1): for y in range(1, len(img[0]) - 1): # getting median filter. arr = [ img[x - 1][y - 1], img[x][y - 1], img[x + 1][y - 1], # using 3x3 median filter here. img[x - 1][y], img[x][y], img[x + 1][y], img[x - 1][y + 1], img[x][y + 1], img[x + 1][y + 1] ] arr.sort() #img[x][y] = arr[4] #sort and getting median value. img2[x - 1][y - 1] = arr[4] # for x in range(len(img2)): # for y in range(len(img2[0])): # img2[x][y] = img[(x+1)][(y+1)] img2 = img2.astype('uint8') return img2
def convolve2d(img, kernel): """Convolves a given image and a given kernel. Steps: (1) flips the kernel (2) pads the image # IMPORTANT this step handles pixels along the border of the image, and ensures that the output image is of the same size as the input image (3) calucates the convolved image using nested for loop Args: img: nested list (int), image. kernel: nested list (int), kernel. Returns: img_conv: nested list (int), convolved image. """ # TODO: implement this function. kl = len(kernel) pad = int(len(kernel) / 2) kernel = utils.flip2d(kernel) img_padded = utils.zero_pad(img, pad, pad) for x, row in enumerate(img): for y, num in enumerate(row): if (x < len(img[0]) - (round(len(kernel) / 2)) or y < len(img) - (round(len(kernel) / 2))): imgfoc = utils.elementwise_mul( kernel, utils.crop(img_padded, x, x + kl, y, y + kl)) sumele = 0 for i in range(len(imgfoc)): sumele += sum(subl[i] for subl in imgfoc) img[x][y] = sumele return img raise NotImplementedError
def median_filter(img): """ Implement median filter on the given image. Steps: (1) Pad the image with zero to ensure that the output is of the same size as the input image. (2) Calculate the filtered image. Arg: Input image. Return: Filtered image. """ # TODO: implement this function. # padding the image padded_img = utils.zero_pad(img, 1, 1) # implementing median filter for row in range(0, len(img)): for col in range(0, len(img[0])): # getting the neighbouring pixels in the img patch = padded_img[row:row + 3] patch = [row[col:col + 3] for row in patch] filter_matrix = [] filter_matrix.extend(patch[0]) filter_matrix.extend(patch[1]) filter_matrix.extend(patch[2]) filter_matrix.sort() median_index = int((len(filter_matrix) + 1) / 2) median = filter_matrix[median_index - 1] img[row][col] = median return img
def convolve2d(img, kernel): """Convolves a given image and a given kernel. Steps: (1) flips the kernel (2) pads the image # IMPORTANT this step handles pixels along the border of the image, and ensures that the output image is of the same size as the input image (3) calucates the convolved image using nested for loop Args: img: nested list (int), image. kernel: nested list (int), kernel. Returns: img_conv: nested list (int), convolved image. """ ### Creating a output buffer ###\ # output = np.zeros_like(img) ### Padding the image with zeros on all sides ### padding_width = 1 image_array = np.array(utils.zero_pad(img, padding_width, padding_width)) # image_shape = {'width': len(image_array[0]), 'height': len(image_array)} ''' Flattening and flipping the kernel flattening the kernel so that I can perform linear convolution operation(on 1D array) on the image ''' # kernel = np.array(kernel).flatten()[::-1] # for h in range(image_shape['height']-2): # for w in range(image_shape['width']-2): # a = image_array[h:h+3,w:w+3].flatten() # # Performing linear convolution operation on image with kernel # sum = np.sum(kernel * a, dtype=np.uint8) # output[h][w] = sum # for h in range(image_shape['height']-2): # for w in range(image_shape['width']-2): # subImg = image_array[h:h+3,w:w+3] # b = utils.elementwise_mul(subImg,flip_kernel) # ### Performing linear convolution operation on image with kernel ### # output[h][w] = np.sum(b, dtype=np.uint8) output = [] flip_kernel = utils.flip2d(kernel) for h in range(len(img) - 2): w_op = [] for w in range(len(img[0]) - 2): sum_img = 0 for k in range(len(flip_kernel)): for l in range(len(flip_kernel[0])): sum_img += img[h + k][w + l] * flip_kernel[k][l] w_op.append(sum_img) output.append(w_op) return output
def median_filter(img): """ Implement median filter on the given image. Steps: (1) Pad the image with zero to ensure that the output is of the same size as the input image. (2) Calculate the filtered image. Arg: Input image. Return: Filtered image. """ # TODO: implement this function. filter_size = 3 img_padded = np.array( utils.zero_pad(img, pwx=1, pwy=1) ) #padding the image with one row and column in beginning and end filtered_img = img for row in range(img_padded.shape[0] - filter_size + 1): for col in range(img_padded.shape[1] - filter_size + 1): med_list = np.array( [] ) #list [3x3] that stores the values among which the median is found for rk in range(filter_size): for ck in range(filter_size): med_list = np.append(med_list, img_padded[row + rk][col + ck]) filtered_img[row][col] = np.median( med_list ) #median of the filter kernel is found and stored in its center return filtered_img.astype(np.uint8)
def median_filter(img): """ Implement median filter on the given image. Steps: (1) Pad the image with zero to ensure that the output is of the same size as the input image. (2) Calculate the filtered image. Arg: Input image. Return: Filtered image. """ # print(img.shape) img_padded = utils.zero_pad(img, 1, 1) # utils.cv2.imshow('res1',img) # print(img_padded) # utils.cv2.waitKey(0) # utils.cv2.destroyAllWindows() # print(img_padded.shape) temp_img = img #median_filtering for i in range(len(img_padded) - 2): for j in range(len(img_padded[1]) - 2): pixel_arr = [] for k in range(0, 3): for l in range(0, 3): pixel_arr.append(img_padded[i + k][j + l]) pixel_arr.sort() # print(pixel_arr) temp_img[i][j] = pixel_arr[4] return temp_img
def median_filter(img): """ Implement median filter on the given image. Steps: (1) Pad the image with zero to ensure that the output is of the same size as the input image. (2) Calculate the filtered image. Arg: Input image. Return: Filtered image. """ # TODO: implement this function. img_o = np.zeros((img.shape[0], img.shape[1])) img = utils.zero_pad(img, 1, 1) for y in range(img.shape[0] - 2): for x in range(img.shape[1] - 2): pix_list = img[y:y + 3, x:x + 3].flatten() pix_list.sort() list_len = len(pix_list) median = -999 if (list_len % 2 == 0): # median -> ((n/2) + (n/2+1))/2 median = ((pix_list[list_len // 2] + pix_list[(list_len // 2) - 1])) // 2 else: median = pix_list[list_len // 2] img_o[y][x] = median return np.array(img_o).astype(np.uint8)
def convolve2d(img, kernel): """Convolves a given image and a given kernel. Steps: (1) flips either the img or the kernel. (2) pads the img or the flipped img. this step handles pixels along the border of the img, and makes sure that the output img is of the same size as the input image. (3) applies the flipped kernel to the image or the kernel to the flipped image, using nested for loop. Args: img: nested list (int), image. kernel: nested list (int), kernel. Returns: img_conv: nested list (int), image. """ # TODO: implement this function. flipped_img = utils.flip2d(img) padded_img = utils.zero_pad(flipped_img, 1, 1) img_conv = np.zeros((len(img), len(img[0]))) for i in range(1, len(padded_img)-2): for j in range(1, len(padded_img[0])-2): img_sec = utils.crop(padded_img, i-1, i+2, j-1, j+2) prod = utils.elementwise_mul(kernel, img_sec) res = 0 for g in range(len(prod)): for h in range(len(prod[0])): res += prod[g][h] img_conv[i-1][j-1] = res img_conv = utils.flip2d(img_conv) # raise NotImplementedError return img_conv
def median_filter(img): """ Implement median filter on the given image. Steps: (1) Pad the image with zero to ensure that the output is of the same size as the input image. (2) Calculate the filtered image. Arg: Input image. Return: Filtered image. """ # TODO: implement this function. filter_size = 3 pw = filter_size // 2 padded_img = utils.zero_pad(img, pw, pw) output = np.zeros((len(img), len(img[0]))) neighbours = list() for i in range(1, len(output) + 1): for j in range(1, len(output[0]) + 1): for k in range(i - pw, i + pw + 1): for l in range(j - pw, j + pw + 1): neighbours.append(padded_img[k, l]) neighbours.sort() output[i - 1][j - 1] = neighbours[4] neighbours.clear() return output.astype(np.uint8)
def convolve2d(img, kernel): """Convolves a given image and a given kernel. Steps: (1) flips the either the img or the kernel. (2) pads the img or the flipped img. this step handles pixels along the border of the img, and makes sure that the output img is of the same size as the input image. (3) applies the flipped kernel to the image or the kernel to the flipped image, using nested for loop. Args: img: nested list (int), image. kernel: nested list (int), kernel. Returns: img_conv: nested list (int), image. """ # TODO: implement this function. img_conv = [[0 for i in range(len(img[0]))] for j in range(len(img))] flipped_kernel = utils.flip2d(kernel) padded_img = utils.zero_pad(img, 1, 1) #Padding = (size of kernel - 1) / 2 neighbors = [(0, 0), (0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] for r in range(1, len(padded_img) - 1): for c in range(1, len(padded_img[0]) - 1): for _r, _c in neighbors: img_conv[r - 1][c - 1] += padded_img[r + _r][ c + _c] * flipped_kernel[1 + _r][1 + _c] #raise NotImplementedError return img_conv
def convolve2d(img, kernel): """Convolves a given image and a given kernel. Steps: (1) flips the kernel (2) pads the image # IMPORTANT this step handles pixels along the border of the image, and ensures that the output image is of the same size as the input image (3) calucates the convolved image using nested for loop Args: img: nested list (int), image. kernel: nested list (int), kernel. Returns: img_conv: nested list (int), convolved image. """ # TODO: implement this function. flipped_kernel = utils.flip2d(kernel) pad_size = int((len(kernel) - 1) / 2) padded_img = utils.zero_pad(img, pad_size, pad_size) filtered_img = copy.deepcopy(img) for i, row in enumerate(img): for j, pix in enumerate(row): patch = utils.crop(padded_img, i, i + 2 * pad_size + 1, j, j + 2 * pad_size + 1) patch = utils.elementwise_mul(patch, kernel) filtered_img[i][j] = sum([sum(l) for l in patch]) return filtered_img
def convolve2d(img, kernel): """Convolves a given image and a given kernel. Steps: (1) flips the either the img or the kernel. (2) pads the img or the flipped img. this step handles pixels along the border of the img, and makes sure that the output img is of the same size as the input image. (3) applies the flipped kernel to the image or the kernel to the flipped image, using nested for loop. Args: img: nested list (int), image. kernel: nested list (int), kernel. Returns: img_conv: nested list (int), image. """ # TODO: implement this function. # raise NotImplementedError flipped_img = utils.flip2d(img) convoluted_img = copy.deepcopy(flipped_img) padded_img = utils.zero_pad(convoluted_img, 1, 1) for i in range(len(convoluted_img)): for j in range(len(convoluted_img[0])): convoluted_img[i][j] = utils.add_matrix( utils.elementwise_mul( utils.crop(padded_img, i, i + 3, j, j + 3), kernel)) return utils.flip2d(convoluted_img)
def corr2d(img, kernel): """ Args: img : nested list (int), image. kernel: nested list (int), kernel. Returns: img_conv: nested list (int), image. """ padding_layer = len(kernel) // 2 img = utils.zero_pad(img, padding_layer, padding_layer) # padding the image row_count = len(img) col_count = len(img[0]) # output image having same size as the input image img_corr = [] zeros = [0] * (col_count - 2 * padding_layer) for i in range(row_count - 2 * padding_layer): img_corr.insert(0, [0 for value in enumerate(zeros)]) kernel_h = len(kernel) kernel_w = len(kernel[0]) img = np.asarray(img, dtype=np.float32) kernel = np.asarray(kernel, dtype=np.float32) for i in range(row_count - kernel_h + 1): for j in range(col_count - kernel_w + 1): mult_result = utils.elementwise_mul( utils.crop(img, i, i + kernel_h, j, j + kernel_w), kernel) sqr_img = utils.elementwise_mul( utils.crop(img, i, i + kernel_h, j, j + kernel_w), utils.crop(img, i, i + kernel_h, j, j + kernel_w)) sqr_ker = utils.elementwise_mul(kernel, kernel) sum = 0 sum_sqr_img = 0 sum_sqr_ker = 0 for p in range(len(mult_result)): for q in range(len(mult_result[p])): sum += mult_result[p][q] sum_sqr_img += sqr_img[p][q] sum_sqr_ker += sqr_ker[p][q] img_corr[i][j] = sum / np.sqrt(sum_sqr_img * sum_sqr_ker) img_corr = [list(row) for row in img_corr] return img_corr
def test_all(self): list = [v for v in tf.global_variables() if '_t' or 'dense' in v.name] saver = tf.train.Saver(var_list=list) if (self.large): S_train = utils.read_large_data(self.train_mat) else: S_train = utils.read_data(self.train_mat) idxs = np.random.permutation(S_train.shape[0]) S_train = S_train[idxs] S_max, S_min = utils.max_min(S_train, self.n_train) del S_train print('Loading testing snapshot matrix...') if (self.large): self.S_test = utils.read_large_data(self.test_mat) else: self.S_test = utils.read_data(self.test_mat) utils.scaling(self.S_test, S_max, S_min) if (self.zero_padding): self.S_test = utils.zero_pad(self.S_test, self.p) print('Loading testing parameters...') self.params_test = utils.read_params(self.test_params) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) ckpt = tf.train.get_checkpoint_state( os.path.dirname(self.checkpoints_folder + '/checkpoint')) if ckpt and ckpt.model_checkpoint_path: print(ckpt.model_checkpoint_path) saver.restore(sess, ckpt.model_checkpoint_path) self.test_once(sess, self.init) utils.inverse_scaling(self.U_h, S_max, S_min) utils.inverse_scaling(self.S_test, S_max, S_min) n_test = self.S_test.shape[0] // self.N_t err = np.zeros((n_test, 1)) for i in range(n_test): num = np.sqrt( np.mean( np.linalg.norm( self.S_test[i * self.N_t:(i + 1) * self.N_t] - self.U_h[i * self.N_t:(i + 1) * self.N_t], 2, axis=1)**2)) den = np.sqrt( np.mean( np.linalg.norm(self.S_test[i * self.N_t:(i + 1) * self.N_t], 2, axis=1)**2)) err[i] = num / den print('Error indicator epsilon_rel: {0}'.format(np.mean(err)))
def test_zero_pad(): test_str = "1 01 001" expected = "1 01 001" result = utils.zero_pad(test_str, 1) assert result == expected, "Length of one should leave string unchanged" expected = "01 01 001" result = utils.zero_pad(test_str, 2) assert result == expected, "Length of two affects numbers lengths less than two" expected = "001 001 001" result = utils.zero_pad(test_str, 3) assert result == expected, "Length of three pads all numbers to three digits" with pytest.raises(ValueError): utils.zero_pad("", -1) pytest.fail("Negative length failed to raise error")
def remove_ramp(arr): # pad zeros around arr, to the size of 3 times (ups = 3) of arr size padded = ut.zero_pad(arr, arr.shape) data = np.fft.fftshift(np.fft.fftn(np.fft.fftshift(padded))) com = center_of_mass(np.power(np.absolute(data), 2)) - .5 sub_pixel_shifted = sub_pixel_shift(data, (com[1], com[0], com[2])) ramp_removed_padded = np.fft.fftshift(np.fft.ifftn(np.fft.fftshift(sub_pixel_shifted))) ramp_removed = ut.crop_center(ramp_removed_padded, arr.shape) return ramp_removed
def convolve2d(img, kernel): # my stuff """Convolves a given image and a given kernel. Steps: (1) flips the kernel (2) pads the image # IMPORTANT this step handles pixels along the border of the image, and ensures that the output image is of the same size as the input image (3) calucates the convolved image using nested for loop Args: img: nested list (int), image. kernel: nested list (int), kernel. Returns: img_conv: nested list (int), convolved image. """ # TODO: implement this function. width = len(img) kernelFlipped = utils.flip_x(kernel) kernelFlipped = utils.flip_y(kernel) paddedImg = utils.zero_pad(img, 1, 1) newImg = [[0 for x in range(width)] for y in range(width)] for i in range(1, width): #x for j in range(1, width): #y w = 3 h = 3 temp = [[0 for x in range(w)] for y in range(h)] # temp = [] start_x = 0 for k in range(i - 1, i + 2): start_y = 0 for l in range(j - 1, j + 2): temp[start_x][start_y] = paddedImg[k][l] start_y += 1 start_x += 1 tempConvoluted = utils.elementwise_mul(temp, kernelFlipped) total = 0 for k in range(0, 3): for l in range(0, 3): total += tempConvoluted[k][l] newImg[i][j] = total #elementwise_add to add everthing up in matrix. return newImg raise NotImplementedError
def convolve2d(img, kernel): """Convolves a given image and a given kernel. Steps: (1) flips either the img or the kernel. (2) pads the img or the flipped img. This step handles pixels along the border of the img, and makes sure that the output img is of the same size as the input image. (3) applies the flipped kernel to the image or the kernel to the flipped image,using nested for loop. Args: img : nested list (int), image. kernel: nested list (int), kernel. Returns: img_conv: nested list (int), image. """ # TODO: DONE padding_layer = len(kernel) // 2 kernel = utils.flip2d(kernel) # flip the kernel img = utils.zero_pad(img, padding_layer, padding_layer) # padding the image row_count = len(img) col_count = len(img[0]) # output image having same size as the input image img_conv = [] zeros = [0] * (col_count - 2 * padding_layer) for i in range(row_count - 2 * padding_layer): img_conv.insert(0, [0 for value in enumerate(zeros)]) kernel_h = len(kernel) kernel_w = len(kernel[0]) for i in range(row_count - kernel_h + 1): for j in range(col_count - kernel_w + 1): # multiplying the cropped portion with kernel mult_result = utils.elementwise_mul( utils.crop(img, i, i + kernel_h, j, j + kernel_w), kernel) sum = 0 for p in range(len(mult_result)): for q in range(len(mult_result[p])): sum += mult_result[p][q] img_conv[i][j] = sum #raise NotImplementedError return img_conv
def convolve2d(img, kernel): """Convolves a given image and a given kernel. Steps: (1) flips the either the img or the kernel. (2) pads the img or the flipped img. this step handles pixels along the border of the img, and makes sure that the output img is of the same size as the input image. (3) applies the flipped kernel to the image or the kernel to the flipped image, using nested for loop. Args: img: nested list (int), image. kernel: nested list (int), kernel. Returns: img_conv: nested list (int), image. """ # TODO: implement this function. # raise NotImplementedError kernel_flip = utils.flip2d(kernel) img_pad = utils.zero_pad(img,1,1) kernel_row = len(kernel) kernel_col = len(kernel[0]) # img_conv = np.zeros(np.shape(img_pad)) image_ = copy.deepcopy(img_pad) # print(kernel_flip_y) # for row_index,row_value in enumerate(img_pad[1:-1]): # for col_index, col_value in enumerate(row_value[1:-1]): # sum_ = 0 # for i in range(-1,2): # for j in range(-1,2): # sum_ += img_pad[row_index+i][col_index+j]*kernel_flip_y[1+i][1+j] # image_[row_index][col_index]= sum_ for row_index, row_value in enumerate(img_pad[:-2]): for col_index,col_val in enumerate(row_value[:-2]): img_temp = utils.crop(img_pad,row_index,(row_index+kernel_row),col_index,(col_index+kernel_col)) imp_temp_1 = utils.elementwise_mul(img_temp,kernel) img_conv_sum = pixel_conv_sum(imp_temp_1) image_[row_index+1][col_index+1] = img_conv_sum img_conv = image_ img_conv = utils.crop(img_conv,1,257,1,257) # print(f'The Type for convo is {type(img_conv)}') return img_conv
def convolve2d(img, kernel): """Convolves a given image and a given kernel. Steps: (1) flips the kernel (2) pads the image # IMPORTANT this step handles pixels along the border of the image, and ensures that the output image is of the same size as the input image (3) calucates the convolved image using nested for loop Args: img: nested list (int), image. kernel: nested list (int), kernel. Returns: img_conv: nested list (int), convolved image. """ # TODO: implement this function. # Flipping the kernel to x-axis and then y-axis flip_kernel = utils.flip2d(kernel, axis=None) # Checking the kernel matrix dimension kernel_height = len(flip_kernel) kernel_width = len(flip_kernel[0]) pwx = (kernel_height - 1) // 2 pwy = (kernel_width - 1) // 2 # Padding the image with zero padding img_padding = utils.zero_pad(img, pwx, pwy) # Checking the padded image dimension img_padding_h = len(img_padding) img_padding_w = len(img_padding[0]) # Creating a copy of input image output = copy.deepcopy(img) for x in range(img_padding_h - (pwx * 2)): for y in range(img_padding_w - (pwx * 2)): # Cropping the image as kernel dimension crop_img = utils.crop(img_padding, x, x + kernel_height, y, y + kernel_width) # Multiplying the elements of Cropped image matrix and kernel matrix img_mult = utils.elementwise_mul(crop_img, flip_kernel) sum_elem = 0 for m in range(len(img_mult)): for n in range(len(img_mult[0])): # Adding the matrix elements sum_elem = sum_elem + img_mult[m][n] output[x][y] = sum_elem return output