Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
umar07 committed Apr 15, 2021
1 parent b0338e4 commit d611734
Show file tree
Hide file tree
Showing 9 changed files with 500 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}
438 changes: 438 additions & 0 deletions Background-color-detector/Color-detection-KMeans.ipynb

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions Background-color-detector/color_detection.py
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()
Binary file added Background-color-detector/picture1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Background-color-detector/picture2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Background-color-detector/picture3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Background-color-detector/picture4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions Background-color-detector/readme.md
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)


1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ A collection of mini OpenCV projects purely for learning purposes and enhancing
* BarCode scanner
* Air-Drum system
* Air-Piano
* Background Color Detection

0 comments on commit d611734

Please sign in to comment.