Created
January 10, 2019 22:58
-
-
Save andrisgauracs/75a5d6bd73b92f39897ea660ba1f205a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Let's fetch the video frame we just captured | |
| imagePath = os.getcwd() + "/img_"+str(framesTaken)+".png" | |
| # Transform the image to an OpenCV readable image | |
| image = cv2.imread(imagePath) | |
| # Let's make a copy of this image | |
| # to use for the color palette generation | |
| image_copy = image_resize(cv2.cvtColor( | |
| image, cv2.COLOR_BGR2RGB), width=100) | |
| # Since the K-means algorithm we're about to do, | |
| # is very labour intensive, we will do it on a smaller image copy | |
| # This will not affect the quality of the algorithm | |
| pixelImage = image_copy.reshape( | |
| (image_copy.shape[0] * image_copy.shape[1], 3)) | |
| # We use the sklearn K-Means algorithm to find the color histogram | |
| # from our small size image copy | |
| clt = KMeans(n_clusters=clusters+offset) | |
| clt.fit(pixelImage) | |
| # build a histogram of clusters and then create a figure | |
| # representing the number of pixels labeled to each color | |
| hist = centroid_histogram(clt) | |
| # Let's plot the retrieved colors. See the plot_colors function | |
| # for more details | |
| bar = plot_colors(hist, clt.cluster_centers_) | |
| # Resize the color bar to be even width with the video frame | |
| barImage = image_resize( | |
| cv2.cvtColor(bar, cv2.COLOR_RGB2BGR), | |
| width=int(videoSize[0])) | |
| # This is just a whitespace to put between the image and the color bar | |
| im = np.zeros((borderSize/2, int(videoSize[0]), 3), np.uint8) | |
| cv2.rectangle(im, (0, 0), (int(videoSize[0]), borderSize/2), | |
| (255, 255, 255), -1) | |
| # Now we combine the video frame and the color bar into one image | |
| newImg = np.concatenate([image, im, barImage], axis=0) | |
| cv2.imwrite(imagePath, newImg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment