Change of Direction

Direction Change

Instead of trying to segment the objects out of the images very clearly and then take the associated colors to classify them , We are now trying a completely different angle which is manually extracting the colors into a mask , applying Canny Edge Detection and then finding the contours of the results in order to find the insides of the cups

However this technique is quite hack and slash I believe we can upgrade this to make it more effective and for the time being I believe were going to follow this path as its the first time we have gotten decent results in the direction we need.


Although the current performance of the algorithm implemented is quite low , I plan to soon cut a lot of redundant or ineffective code therefore increasing its efficiency and giving it a more dynamic nature as a programmer wouldn’t need to manually enter in each extra color but simply add a key to a dictionary with its associated ranges of color.

In the coming days I hope to implement these changes.


# import the necessary packages:
import numpy as np
import cv2
from matplotlib import pyplot as plt
from matplotlib import image as image
import easygui
import math

# Opening an image and backing it up
img = cv2.imread("test1.jpg")
ori = img.copy()

counts = {}
total = 0
#Colours to look for
colors = ["pink", "blue", "dark blue", "green", "yellow","orange"]

for color in colors:
	counts[color] = 0

	#Define the ranges 
	if (color == str(colors[0])):
		lower = np.array([128,7,252])
		upper = np.array([223,175,254])
	elif (color == str(colors[1])):
		lower = np.array([195, 148, 13])
		upper = np.array([255, 238, 77])
	elif (color == str(colors[2])):
		lower = np.array([229, 70, 20])
		upper = np.array([255, 166, 89])
	elif (color == str(colors[3])):
		lower = np.array([12, 182, 69])
		upper = np.array([66, 254, 189])
	elif (color == str(colors[4])):
		lower = np.array([51, 209, 239])
		upper = np.array([120, 252, 252])	
	elif (color == str(colors[5])):
		lower = np.array([8, 63, 252])
		upper = np.array([84, 88, 255])

	#Create mask then apply it to the image to extract color
	mask = cv2.inRange(img, lower, upper)
	maskImg = cv2.bitwise_and(img, img, mask = mask)

	gray = cv2.cvtColor(maskImg, cv2.COLOR_BGR2GRAY)

	edge = cv2.Canny(gray, 100, 200)
	edge = cv2.dilate(edge, None, iterations = 1)

	(_, contours, _) = cv2.findContours(edge.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
	for c in contours:
		area = cv2.contourArea(c)
		#Change this to percentage/find a way tp use the largest one
		if (area < 1000):

		hull = cv2.convexHull(c)
       for i in colors:
           if(color == colors[i])
               cv2.drawContours(img, [hull], 0, (0, 0, 0))

		counts[color] += 1
		total += 1
	#Tell user how many objects of that color
	print("{} {} object(s)".format(counts[color], color))

print("{} object(s) total".format(total))

cv2.imshow("asd", img)
key = cv2.waitKey(0)

I believe this to be our current direction however I also hope to use some of the techniques we used while doing research and experimenting when further upgrading.



References (2017). OpenCV: Contour Features. [online] Available at: [Accessed 28 Oct. 2017].

henrydangprg. (2016). Color Detection in Python with OpenCV. [online] Available at: [Accessed 28 Oct. 2017].

*This one is actually really useful. Shows a nice way to do Color Detection.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s