def dxdy_hist(img_gray, num_bins): assert len(img_gray.shape) == 2, 'image dimension mismatch' assert img_gray.dtype == 'float', 'incorrect image type' dx, dy = gauss_module.gaussderiv(img_gray, 3.0) generatedbins = np.linspace(-6, 6, num=num_bins + 1) #Define a 2D histogram with "num_bins^2" number of entries hists = np.zeros((num_bins, num_bins)) dxdy = np.clip( np.stack((dx, dy), axis=2), -6, 6) # cap the values in the range [-6, 6] with np.clip function fimg = dxdy.reshape(dxdy.shape[0] * dxdy.shape[1], 2) # reshape the arrays for i in range(dxdy.shape[0] * dxdy.shape[1]): dx_bin = np.where(generatedbins <= fimg[i, 0])[0][-1] if fimg[ i, 0] != 6 else generatedbins.size - 2 # choose bins.size - 2 if current pixel is != 6 dy_bin = np.where( generatedbins <= fimg[i, 1] )[0][-1] if fimg[i, 1] != 6 else generatedbins.size - 2 hists[dx_bin, dy_bin] += 1 # Normalize the histogram such that its integral (sum) is equal 1 hists = hists / hists.sum() #Return the histogram as a 1D vector hists = hists.reshape(hists.size) return hists
def dxdy_hist(img_gray, num_bins): assert len(img_gray.shape) == 2, 'image dimension mismatch' assert img_gray.dtype == 'float', 'incorrect image type' #Define a 2D histogram with "num_bins^2" number of entries hists = np.zeros((num_bins, num_bins)) [imgDx, imgDy] = gauss_module.gaussderiv(img_gray, sigma=3) bins = np.linspace(-6, 6, num_bins) imgDx = np.clip(imgDx, -6, 6) imgDy = np.clip(imgDy, -6, 6) imgDx = imgDx.reshape(imgDx.size) imgDy = imgDy.reshape(imgDy.size) for i in range(min(len(imgDx), len(imgDy))): counts = np.zeros(len(img_gray.shape), dtype='int') for number, bin_ in enumerate(bins[:-1]): if imgDx[i] >= bin_ and imgDx[i] < bins[number + 1]: counts[0] = number if imgDy[i] >= bin_ and imgDy[i] < bins[number + 1]: counts[1] = number hists[counts[0], counts[1]] += 1 hists = np.divide(hists, np.sum(hists)) #Return the histogram as a 1D vector hists = hists.reshape(hists.size) return hists
def dxdy_hist(img_gray, num_bins): assert len(img_gray.shape) == 2, 'image dimension mismatch' assert img_gray.dtype == 'float', 'incorrect image type' # compute the first derivatives # ... [imgDx, imgDy] = gauss_module.gaussderiv(img_gray, 7) # quantize derivatives to "num_bins" number of values # ... dx_bin_length = (np.max(imgDx) - np.min(imgDx)) / num_bins dy_bin_length = (np.max(imgDy) - np.min(imgDy)) / num_bins # define a 2D histogram with "num_bins^2" number of entries hists = np.zeros((num_bins, num_bins)) # ... for i in range(img_gray.shape[0]): for j in range(img_gray.shape[1]): # increment a histogram bin which corresponds to the value of pixel i,j; h(R,G,B) # ... [dx_bin, dy_bin] = [ int(imgDx[i][j] / dx_bin_length), int(imgDy[i][j] / dy_bin_length) ] hists[dx_bin][dy_bin] += 1 hists = hists.reshape(hists.size) return hists
def dxdy_hist(img_gray, num_bins): assert len(img_gray.shape) == 2, 'image dimension mismatch' assert img_gray.dtype == 'float', 'incorrect image type' # compute the first derivatives sigma = 7.0 img_dx, img_dy = gauss_module.gaussderiv(img_gray, sigma) # quantize derivatives to "num_bins" number of values min_a = -32 max_a = 32 t = (max_a - min_a + 1) / num_bins # define a 2D histogram with "num_bins^2" number of entries hists = np.zeros((num_bins, num_bins)) # ... for i in range(img_gray.shape[0]): for j in range(img_gray.shape[1]): x = math.floor((img_dx[i, j] + 32) / t) y = math.floor((img_dy[i, j] + 32) / t) hists[x, y] += 1 hists = hists.reshape(hists.size) hists = hists / (img_gray.shape[0] * img_gray.shape[1]) total = hists.sum() hists = hists / total return hists
def dxdy_hist(img_gray, num_bins): assert len(img_gray.shape) == 2, 'image dimension mismatch' assert img_gray.dtype == 'float', 'incorrect image type' min_interval = -6 max_interval = 6 # defining the x and y derivatives of the image and # defining the minimum and maximum range of the derivatives derivx, derivy = gauss_module.gaussderiv(img_gray, 3) derivx = np.clip(derivx, min_interval, max_interval) derivy = np.clip(derivy, min_interval, max_interval) # stacking the derivatives to iterate over them stacked = list(zip(derivx.reshape(-1), derivy.reshape(-1))) # the bin size is equal to the difference of the extremes # divided by the input number of bins bin_size = (max_interval - min_interval) / num_bins # filling the list's values with the equal-distanced bins bins = [min_interval for _ in range(num_bins + 1)] previous = min_interval for i in range(num_bins): bin_ = previous + bin_size bins[i + 1] = bin_ previous = bin_ # defining a 2D histogram with "num_bins^2" number of entries hists = np.zeros((num_bins, num_bins)) # filling the array's values with the frequencies of the # pixels in the bins intervals for i in range(len(stacked)): deriv_xy = [0, 0] for k in range(len(bins)): if bins[k - 1] <= stacked[i][0] < bins[k]: deriv_xy[0] = k - 1 if bins[k - 1] <= stacked[i][1] < bins[k]: deriv_xy[1] = k - 1 hists[deriv_xy[0], deriv_xy[1]] += 1 hists = hists / np.sum(hists) # return the histogram as a 1D vector hists = hists.reshape(hists.size) return hists
def dxdy_hist(img_gray, num_bins): assert len(img_gray.shape) == 2, 'image dimension mismatch' assert img_gray.dtype == 'float', 'incorrect image type' lower = -6 upper = 6 # We call the gaussderiv function in order to get the partial derivatives for x and y # Cap the range of derivative values is in the range [-6, 6] Dx, Dy = gauss_module.gaussderiv(img_gray, 3) Dx = np.clip(Dx, lower, upper) Dy = np.clip(Dy, lower, upper) # We stack them in order to simplify the iteration stacked_deriv = list(zip(Dx.reshape(-1), Dy.reshape(-1))) bin_size = (upper - lower)/num_bins # We fill up the list with bins of equal length bins = [lower for x in range(num_bins+1)] previous = lower for i in range(num_bins): bin = previous + bin_size bins[i+1] = bin previous = bin # defining a 2D histogram with "num_bins^2" number of entries hists = np.zeros((num_bins, num_bins)) # filling the array's values with the frequencies of the # pixels in the bins intervals for i in range(len(stacked_deriv)): Dxy = [0,0] for k in range(len(bins)): if bins[k-1] <= stacked_deriv[i][0] < bins[k]: Dxy[0] = k-1 if bins[k-1] <= stacked_deriv[i][1] < bins[k]: Dxy[1] = k-1 hists[Dxy[0],Dxy[1]] += 1 hists = hists/np.sum(hists) # Return the histogram as a 1D vector hists = hists.reshape(hists.size) return hists
def dxdy_hist(img_gray, num_bins): assert len(img_gray.shape) == 2, 'image dimension mismatch' assert img_gray.dtype == 'float', 'incorrect image type' # compute the first derivatives imgderiv = gauss_module.gaussderiv(img_gray, 7.0) # quantize derivatives to "num_bins" number of values bin_len = 60.0 / num_bins # define a 2D histogram with "num_bins^2" number of entries hists = np.zeros((num_bins, num_bins)) for i in range(imgderiv[0].shape[0]): for j in range(imgderiv[0].shape[1]): derx = (imgderiv[0][i, j] + 30) // bin_len dery = (imgderiv[1][i, j] + 30) // bin_len hists[int(derx), int(dery)] += 1 hists = hists * 1.0 / np.sum(hists) hists = hists.reshape(hists.size) return hists
def dxdy_hist(img_gray, num_bins): assert len(img_gray.shape) == 2, 'image dimension mismatch' assert img_gray.dtype == 'float', 'incorrect image type' # compute the first derivatives imgDx, imgDy = gauss_module.gaussderiv(img_gray, 7) # quantize derivatives to "num_bins" number of values imgDx = np.around(((imgDx + 30) / 60) * (num_bins - 1)).astype(int) imgDy = np.around(((imgDy + 30) / 60) * (num_bins - 1)).astype(int) # define a 2D histogram with "num_bins^2" number of entries hists = np.zeros((num_bins, num_bins)) # execute the loop for each pixel in the image for i in range(imgDx.shape[0]): for j in range(imgDx.shape[1]): hists[imgDx[i, j], imgDy[i, j]] += 1 hists = hists.reshape(hists.size) return hists
def dxdy_hist(img_gray, num_bins): assert len(img_gray.shape) == 2, 'image dimension mismatch' assert img_gray.dtype == 'float', 'incorrect image type' [imgDx, imgDy] = gauss_module.gaussderiv(img_gray, 3.0) imgDx = np.matrix.flatten(np.clip(imgDx, -6, 6)) imgDy = np.matrix.flatten(np.clip(imgDy, -6, 6)) # Define a 2D histogram with "num_bins^2" number of entries hists = np.zeros((num_bins, num_bins)) step_unit = 13 / num_bins for i in range(len(imgDx)): hists[int((imgDx[i] + 6) / step_unit), int((imgDy[i] + 6) / step_unit)] += 1 # Normalize the histogram such that its integral (sum) is equal 1 hists = hists / np.sum(hists) # Return the histogram as a 1D vector hists = hists.reshape(hists.size) return hists
def dxdy_hist(img_gray, num_bins): assert len(img_gray.shape) == 2, 'image dimension mismatch' assert img_gray.dtype == 'float', 'incorrect image type' bin_size = 256/num_bins #Define a 2D histogram with "num_bins^2" number of entries hists = np.zeros((num_bins, num_bins)) [imgDx, imgDy] = gauss_module.gaussderiv(img_gray, 3.0) imgDx = 12*(imgDx-np.min(imgDx))/(np.max(imgDx)-np.min(imgDx)) - 6 imgDy = 12*(imgDy-np.min(imgDy))/(np.max(imgDy)-np.min(imgDy)) - 6 imgDx = imgDx.ravel() imgDy = imgDy.ravel() for i in range(img_gray.shape[0]*img_gray.shape[1]): x = int(np.floor(imgDx[i]/bin_size)) + 1 y = int(np.floor(imgDy[i]/bin_size)) + 1 hists[x-1,y-1] = hists[x-1,y-1] + 1 #Return the histogram as a 1D vector hists = hists.reshape(hists.size) return hists
def dxdy_hist(img_gray, num_bins): assert len(img_gray.shape) == 2, 'image dimension mismatch' assert img_gray.dtype == 'float', 'incorrect image type' bin_size = 255 / num_bins Dx, Dy = gauss_module.gaussderiv(img_gray, 3) Dx = np.floor(Dx.flatten() / bin_size).astype(np.int8) Dy = np.floor(Dy.flatten() / bin_size).astype(np.int8) Dx[Dx > 6] = 6 Dx[Dx < -6] = -6 Dy[Dy > 6] = 6 Dy[Dy < -6] = -6 #Define a 2D histogram with "num_bins^2" number of entries hists = np.zeros((num_bins, num_bins)) # Loop for each pixel i in the image for i in range(img_gray.shape[0] * img_gray.shape[1]): # Increment the histogram bin which corresponds to the R,G,B value of the pixel i hists[Dx[i], Dy[i]] += 1 pass #Normalize the histogram such that its integral (sum) is equal 1 n_pixels = img_gray.shape[0] * img_gray.shape[1] hists = hists / n_pixels #Return the histogram as a 1D vector hists = hists.reshape(hists.size) return hists
def dxdy_hist(img_gray, num_bins): assert len(img_gray.shape) == 2, 'image dimension mismatch' assert img_gray.dtype == 'float', 'incorrect image type' sigma = 3.0 img_dx, img_dy = gm.gaussderiv(img_gray, sigma) imgx = np.reshape(img_dx, (len(img_dx[0]) * len(img_dx))) imgy = np.reshape(img_dy, (len(img_dy[0]) * len(img_dy))) #Define a 2D histogram with "num_bins^2" number of entries hists = np.zeros((num_bins, num_bins)) bin_array = np.linspace(-255, 255, num_bins) for i in range(len(imgx)): in_x = imgx[i] in_y = imgy[i] i_x = 0 i_y = 0 for j in range(len(bin_array) - 1): if ((in_x >= bin_array[j]) & (in_x < bin_array[j + 1])): i_x = np.where(bin_array == bin_array[j])[0][0] elif ((in_y >= bin_array[j]) & (in_y < bin_array[j + 1])): i_y = np.where(bin_array == bin_array[j])[0][0] hists[i_x][i_y] += 1 #Normalize the histogram such that its integral (sum) is equal 1 hists /= np.sum(hists) #Return the histogram as a 1D vector hists = hists.reshape(hists.size) return hists
plt.imshow(conv2(conv2(img_imp, Dx.T, 'same'), Gx, 'same'), cmap='gray') plt.subplot(2, 3, 4) plt.imshow(conv2(conv2(img_imp, Dx, 'same'), Dx.T, 'same'), cmap='gray') plt.subplot(2, 3, 5) plt.imshow(conv2(conv2(img_imp, Dx, 'same'), Gx.T, 'same'), cmap='gray') plt.subplot(2, 3, 6) plt.imshow(conv2(conv2(img_imp, Gx.T, 'same'), Dx, 'same'), cmap='gray') plt.show() ## function gaussderiv (Question 1.e) img_c = np.array( Image.open( "/Users/edoardogabrielli/Documents/Università/ComputerScience/FoundationsOfDataScience/fds-2021/A1/Filtering/graf.png" )).astype('double') img = rgb2gray(img_c) [imgDx, imgDy] = gauss_module.gaussderiv(img, 7.0) plt.figure(8) ax1 = plt.subplot(1, 3, 1) ax2 = plt.subplot(1, 3, 2) ax3 = plt.subplot(1, 3, 3) plt.sca(ax1) plt.imshow(imgDx, cmap='gray') plt.sca(ax2) plt.imshow(imgDy, cmap='gray') plt.sca(ax3) imgmag = np.sqrt(imgDx**2 + imgDy**2) plt.imshow(imgmag, cmap='gray') plt.show()