In the picture below there are some regions which are very bright (i.e. more white). Some bright regions are wide and some are narrow or thin. The red box covers one such wide bright spot, and blue box covers one thin bright spot. Thin bright spots are called edges and wide bright spots are called hot-spots.
I want to remove all the hot-spots from the image (i.e. make them black), but no edge should be removed.
I've written Python code (using OpenCV) to remove all hot-spots but not the edges.
My code:
import cv2 import numpy as np # Load the image image1 = cv2.imread('orange.jpg', cv2.IMREAD_GRAYSCALE) original_image = image1 # Otsu's thresholding _, image2 = cv2.threshold(image1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # Erosion kernel = np.ones((5, 5), np.uint8) image3 = cv2.erode(image2, kernel, iterations=1) # Define the threshold distance K K = 2 # Create the circular mask mask = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * K, 2 * K)) # Iterate over image1 pixels and generate the final image final_image = np.copy(image1) for y in range(image1.shape[0]): for x in range(image1.shape[1]): # Check if any illuminated pixel exists within K distance in image3 if image2[y, x] > 0: neighborhood = image3[max(y - K, 0):min(y + K + 1, image3.shape[0]), max(x - K, 0):min(x + K + 1, image3.shape[1])] if np.sum(neighborhood) > 0: final_image[y, x] = 0 # Display the original and final image cv2.imshow('Original', original_image) cv2.imshow('Final Image', final_image) cv2.waitKey(0) cv2.destroyAllWindows() My question:
How can I reduce the computational complexity of my code? My code's complexity is O(n²) because of the two nested for loops; how can I make it O(n) or O(n log n)?

