def change_hue(img, hue_angle): ''' Changes Hue based on provided hue offset; Hue Angle must be provided in *radians* ! ''' # convert to HSI out_img = rgb_to_hsi(img) # get Hue angle displacement in radians; only change 0 <= x <= 2pi if (hue_angle != 0): change = abs(hue_angle) % (2 * math.pi) else: change = 0 # adjust hue for every pixel for row in range(out_img.shape[0]): for col in range(out_img.shape[1]): # making sure 0 <= hue <= 2pi if (out_img[row, col, 0] + change > 2 * math.pi): out_img[row, col, 0] = out_img[row, col, 0] + change - 2 * math.pi elif (out_img[row, col, 0] + change < 0): out_img[row, col, 0] = out_img[row, col, 0] + change + 2 * math.pi else: out_img[row, col, 0] = out_img[row, col, 0] + change # convert to RGB out_img = hsi_to_rgb(out_img) return out_img
def change_saturation(img, sat): ''' Changes saturation level of HSI image ''' # convert to HSI out_img = rgb_to_hsi(img) # Perform operation only if change isn't 0 if (sat != 0): # adjust saturation for every pixel for row in range(out_img.shape[0]): for col in range(out_img.shape[1]): result = out_img[row, col, 1] + sat # make sure that the final result stays 0 <= S <= 1 if (result > 1): out_img[row, col, 1] = 1 elif (result < 0): out_img[row, col, 1] = 0 else: out_img[row, col, 1] = result # convert to RGB out_img = hsi_to_rgb(out_img) return out_img
def change_saturation(image: str, sat: int): ''' sat can be negative''' global x, img, img_size, hsi, rgb_output ## Reading input image x = cv2.imread(image) img = np.array(x, dtype=np.double) img_size = img.shape ## Confirm and adjust so sat is greater or equal to 0 and less than or ## equal to 1 if (sat > 1): sat = sat % 1 elif (sat < 0): sat = (sat % 1) + 1 ## Converting to HSI space hsi = rgb_to_hsi(img) ## Applying change in saturation to output image for i in range(img_size[0]): for j in range(img_size[1]): hsi[i, j, 1] = (((hsi[i, j, 1] / 255.0) + sat) % 1.0) * 255.0 ## Converting back into RGB space rgb_output = hsi_to_rgb(hsi) ## Showing and writing output image cv2.imshow('rgb image with hue changed', rgb_output) cv2.imwrite('changed_sat_output.jpg', rgb_output) cv2.waitKey(0) cv2.destroyAllWindows()
def change_hue(image: str, hue_angle: int): ''' hue_angle must be given in degrees''' global x, img, img_size, hue, hsi, rgb_output ## Reading input image x = cv2.imread(image) img = np.array(x, dtype=np.double) img_size = img.shape ## Confirming and adjusting hue so it is within range of [0, 360] hue = hue_angle if (hue > 360): hue = hue % 360 elif (hue < 0): hue = (hue % 360) + 360 ## Converting to HSI space hsi = rgb_to_hsi(img) ## Applying change in hue to output image for i in range(img_size[0]): for j in range(img_size[1]): hsi[i, j, 0] = (((hsi[i, j, 0] / 255.0) + (hue / 360.0)) % 1.0) * 255.0 ## Converting back into RGB space rgb_output = hsi_to_rgb(hsi) ## Showing and writing output image cv2.imshow('rgb image with hue changed', rgb_output) cv2.imwrite('changed_hue_output.jpg', rgb_output) cv2.waitKey(0) cv2.destroyAllWindows()
def main(*arg): imgName = input("Please enter image name: ") img = cv2.imread(imgName, 1) # to run independently (from main() ), needs to have rgb_to_hsi.py to be in the same directory! img1 = rgb_to_hsi(img) out = hsi_to_rgb(img1) cv2.imshow('out', out) cv2.waitKey(0)