-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
500 additions
and
0 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
Background-color-detector/.ipynb_checkpoints/Color-detection-KMeans-checkpoint.ipynb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"cells": [], | ||
"metadata": {}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import cv2 | ||
import numpy as np | ||
from collections import Counter | ||
|
||
img=cv2.imread('/home/umar/ML-AI/OpenCV-Projects/Background-color-detector/picture3.jpg') | ||
img=cv2.resize(img,(800,600)) | ||
|
||
blue,green,red=cv2.split(img) | ||
blue=blue.flatten() | ||
green=green.flatten() | ||
red=red.flatten() | ||
|
||
blue_counter=Counter(blue) | ||
green_counter=Counter(green) | ||
red_counter=Counter(red) | ||
|
||
blue_most=blue_counter.most_common(10) | ||
blue_avg=[i for i,j in blue_most] | ||
blue_avg=int(np.mean(blue_avg)) | ||
|
||
green_most=green_counter.most_common(10) | ||
green_avg=[i for i,j in green_most] | ||
green_avg=int(np.mean(green_avg)) | ||
|
||
red_most=red_counter.most_common(10) | ||
red_avg=[i for i,j in red_most] | ||
red_avg=int(np.mean(red_avg)) | ||
|
||
|
||
background=[blue_avg,green_avg,red_avg] | ||
|
||
bg=np.zeros((512,512,3),np.uint8) | ||
bg_color=cv2.rectangle(bg,(0,0),(512,512),background,-1) | ||
print(background) | ||
|
||
cv2.imshow('Image',img) | ||
cv2.imshow('Background',bg_color) | ||
|
||
cv2.waitKey(0) | ||
cv2.destroyAllWindows() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
***Ever wondered how the Instagram stories pick a background automatically when you add an image?!*** Well they analyse your picture through different algorithms and produce a background matching with the image. They primarily use the "colors" present in the image to process the output. | ||
|
||
|
||
Here in this project you find 2 techniques to detect a suitable background for the input image. The first approach is to separate the RGB matrices and then do a frequency count for each pixel value in the 3 RGB matrices individually using the Counter() function. After that, you select the 10 most frequent values present and take the average of them to get the resultant pixel value. Finally, just produce a blank image using np.zeros() and fill it with the background color obtained to show the final result. | ||
This is a very naive approach but produces results in just 40 lines of code! | ||
|
||
The second approach is to use K-Means Clustering algorithm on the RGB values and find clusters of different set of colors present in the image. After that, again make use of frequency count and finally find the background color. | ||
This method involves the use of unsupervised machine learning and it's applications extend much beyond background color detection. Image segmentation tasks make heavy use of this approach of using K-Means Clustering on the image. | ||
|
||
##### Sources used to learn- | ||
|
||
1. [Using K-Means Clustering on image](https://towardsdatascience.com/color-identification-in-images-machine-learning-application-b26e770c4c71) | ||
2. [Using separate RGB values](https://medium.com/generalist-dev/background-colour-detection-using-opencv-and-python-22ed8655b243) | ||
|
||
|
This file contains 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