Object counting software using opencv - python

I am working on counting software. I have successfully detected the products and want to count them. For counting, I have defined two-line and if centroid is between two lines the product is counted. Two problems I am facing. First, since my product is coming very fast some product is not falling between lines so, I increased the linespace. Second, after increasing the linespace if the products are falling two times then it is counted twice. I tried to put centroid in a list and if products are counted once, I deleted elements of the list. Count is twice or wrong. Please help.
Here is the video link
https://drive.google.com/file/d/1Gqp937OlDpF4_Nx2kSOzHw3A85Xch-HX/view?usp=sharing
import cv2
import math
import numpy as np
width = 0
height = 0
EntranceCounter = 0
ExitCounter = 0
OffsetRefLines = 120
QttyOfContours = 0
area = 0
detect = []
area_min = 100
area_max = 4000
BinarizationThreshold = 70
#Check if an object in entering in monitored zone
##def CheckEntranceLineCrossing(CoordYCentroid, CoorYEntranceLine,CoorYExitLine,area):
## AbsDistance = abs(CoordYCentroid - CoorYEntranceLine)
## if ((y>CoorYEntranceLine) and (y < CoorYExitLine) ):#and area>=5000):
## return 1
## #else:
## #return 0
cap = cv2.VideoCapture('testvideo.avi')
while(cap.isOpened()):
ret, frame = cap.read()
height = np.size(frame,0)
width = np.size(frame,1)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
_, threshold = cv2.threshold(frame, 100, 255, cv2.THRESH_BINARY)
low = np.array([0,0,0])
high = np.array([60, 60, 60])
image_mask = cv2.inRange(threshold,low, high)
contours,_= cv2.findContours(image_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(frame, contours, -1, (0,255,0), 3)
CoorYEntranceLine = int(150-OffsetRefLines)
CoorYExitLine = int(150+OffsetRefLines)
cv2.line(threshold, (0,CoorYEntranceLine), (width,CoorYEntranceLine), (0, 255, 0), 2)
cv2.line(threshold , (0,CoorYExitLine), (width,CoorYExitLine), (0, 0, 255), 2)
for (i,c) in enumerate(contours):
new = True
QttyOfContours = QttyOfContours+1
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(threshold, (x, y), (x + w, y + h), (0, 0, 255), 2)
area = cv2.contourArea(c)
if area< area_min:
continue
#find object's centroid
CoordXCentroid = int((x+x+w)/2)
CoordYCentroid = int((y+y+h)/2)
ObjectCentroid = (CoordXCentroid,CoordYCentroid)
#center = pega_centro(x, y, w, h)
center = (CoordXCentroid,CoordYCentroid)
detect.append(center)
for (x,y) in detect:
if ((y < CoorYExitLine) and (y>CoorYEntranceLine) and (area>area_min) and (area<area_max)):
if new ==True:
EntranceCounter += 1
detect.clear()
break
cv2.putText(frame, str(EntranceCounter), ((CoordXCentroid),(CoordYCentroid-50)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
cv2.putText(threshold, "Entrances: {}".format(str(EntranceCounter)), (10, 50),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (250, 0, 1), 2)
cv2.imshow('fram1',threshold)
cv2.imshow('frame',frame)
if cv2.waitKey(1)==27:
break
cap.release()
cv2.destroyAllWindows()

Just check intersection of counting line and line between current and previous object position points. And, as extra guaranty, use "counted" flag for tracked objects.

Related

Detect white label boundary opencv python

I'm having a series of images with shipping labels on boxes and I need to extract the whole white area of the label.
I'm extremely new to opencv and using these answers (detect rectangle in image and crop) i managed to put together the following code(it extracts only the top most part of the label):
import cv2
import numpy as np
#
path_to_image = 'IMG_0184b.jpg'
#
img = cv2.imread(path_to_image)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 0, 17, 17)
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(gray,kernel,iterations = 2)
kernel = np.ones((4,4),np.uint8)
dilation = cv2.dilate(erosion,kernel,iterations = 2)
edged = cv2.Canny(dilation, 30, 200)
cnt, h = cv2.findContours(edged,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
largestArea = []
for contour in cnt:
largestArea.append(cv2.contourArea(contour))
print(sorted(largestArea, reverse=True)[0:3])
for contour in cnt:
approx = cv2.approxPolyDP(contour, 0.01* cv2.arcLength(contour, True), True)
area = cv2.contourArea(contour)
if area == 612144.5:
cv2.drawContours(img, [approx], 0, (0, 0, 0), 5)
x = approx.ravel()[0]
y = approx.ravel()[1] - 5
#
if len(approx) == 4 :
x, y , w, h = cv2.boundingRect(approx)
aspectRatio = float(w)/h
#print(aspectRatio)
if aspectRatio >= 0.95 and aspectRatio < 1.05:
cv2.putText(img, "square", (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0))
else:
cv2.putText(img, "rectangle", (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0))
cv2.namedWindow('custom window', cv2.WINDOW_KEEPRATIO)
cv2.imshow('custom window', img)
cv2.resizeWindow('custom window', 800, 800)
cv2.waitKey(0)
cv2.destroyAllWindows()
How do I capture the whole white area of the label just like in the example below?
Original picture
Desired result
Many thanks

getting error : contours is not defined for my hand-web-browser project

I am trying to build a project "hand_web_browser", the primary objective of this project is to open web pages using hand-gestures. But, I am getting an error at line 55 which says
cnt = max(contours, key=lambda x: cv2.contourArea(x))
NameError: name 'contours' is not defined
If anyone could help me with the issue???
import cv2
import numpy as np
import math
import webbrowser as wb
import os
print("Enter full website for")
print("\n2 fingers")
fingers2 = input()
print("\n3 fingers")
fingers3 = input()
print("\n4 fingers")
fingers4 = input()
tabs = 0
count = 0
cap = cv2.VideoCapture(0)
while (cap.isOpened()):
# read image
ret, img = cap.read()
# get hand data from the rectangle sub window on the screen
cv2.rectangle(img, (400, 400), (100, 100), (0, 255, 0), 0)
crop_img = img[100:400, 100:400]
# convert to grayscale
grey = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
# applying gaussian blur
value = (35, 35)
blurred = cv2.GaussianBlur(grey, value, 0)
# thresholdin: Otsu's Binarization method
_, thresh1 = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# show thresholded image, not necessary and can be skipped
cv2.imshow('Thresholded', thresh1)
# check OpenCV version to avoid unpacking error
(version, _, _) = cv2.__version__.split('.')
if version == '3':
image, contours, hierarchy = cv2.findContours(thresh1.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
elif version == '2':
contours, hierarchy = cv2.findContours(thresh1.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# find contour with max area
cnt = max(contours, key=lambda x: cv2.contourArea(x))
# create bounding rectangle around the contour (can skip below two lines)
x, y, w, h = cv2.boundingRect(cnt)
cv2.rectangle(crop_img, (x, y), (x + w, y + h), (0, 0, 255), 0)
# finding convex hull
hull = cv2.convexHull(cnt)
# drawing contours
drawing = np.zeros(crop_img.shape, np.uint8)
cv2.drawContours(drawing, [cnt], 0, (0, 255, 0), 0)
cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 0)
# finding convex hull
hull = cv2.convexHull(cnt, returnPoints=False) # return point false to find convexity defects
# finding convexity defects
defects = cv2.convexityDefects(cnt, hull)
count_defects = 0
cv2.drawContours(thresh1, contours, -1, (0, 255, 0), 3) # to draw all contours pass -1
# applying Cosine Rule to find angle for all defects (between fingers)
# with angle > 90 degrees and ignore defects
for i in range(defects.shape[0]):
s, e, f, d = defects[i, 0] # [ start point, end point, farthest point, approximate distance to farthest point ]
start = tuple(cnt[s][0])
end = tuple(cnt[e][0])
far = tuple(cnt[f][0])
# find length of all sides of triangle
a = math.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2)
b = math.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2)
c = math.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2)
# apply cosine rule here
angle = math.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)) * 57
# ignore angles > 90 and highlight rest with red dots
if angle <= 90:
count_defects += 1
cv2.circle(crop_img, far, 1, [0, 0, 255], -1)
# dist = cv2.pointPolygonTest(cnt,far,True)
# draw a line from start to end i.e. the convex points (finger tips)
# (can skip this part)
cv2.line(crop_img, start, end, [0, 255, 0], 2)
# cv2.circle(crop_img,far,5,[0,0,255],-1)
if count == 0:
cv2.putText(img, "Wait for it :p", (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 3, 3)
# define actions required
if count_defects == 1 and count != 2 and tabs <= 8:
wb.open_new_tab('http://www.' + fingers2 + '.com')
tabs = tabs + 1
cv2.putText(img, "2." + fingers2, (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 3, (255, 0, 0), 3)
count = 2
elif count_defects == 2 and count != 3 and tabs <= 8:
wb.open_new_tab('http://www.' + fingers3 + '.com')
tabs = tabs + 1
cv2.putText(img, "3." + fingers3, (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 255), 3)
count = 3
elif count_defects == 3 and count != 4 and tabs <= 8:
wb.open_new_tab('http://www.' + fingers4 + '.com')
cv2.putText(img, "4." + fingers4, (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 3, (255, 165, 0), 3)
tabs = tabs + 1
count = 4
elif count_defects == 4 and count != 5:
cv2.putText(img, "5.Close Web browser", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 3, 3)
os.system("taskkill /im chrome.exe /f")
tabs = 0
count = 5
else:
cv2.putText(img, "", (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 3, 3)
if count == 2:
cv2.putText(img, "2." + fingers2, (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 3, (255, 0, 0), 3)
elif count == 3:
cv2.putText(img, "3." + fingers3, (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 255), 3)
elif count == 4:
cv2.putText(img, "4." + fingers4, (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 3, (255, 165, 0), 3)
elif count == 5:
cv2.putText(img, "5.WebBrowser close", (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 3, 3)
# show appropriate images in windows
cv2.imshow('Gesture', img)
all_img = np.hstack((drawing, crop_img))
# not necessary to show contours and can be skipped
cv2.imshow('Contours', all_img)
k = cv2.waitKey(10)
if k == 27:
break
I presume you are using a latest version of OpenCV. If you are using something like 4.x.x, try the following code.
if version == '4':
contours, hierarchy = cv2.findContours(thresh1.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
elif version == '2':
contours, hierarchy = cv2.findContours(thresh1.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
The reason for the error is that your code is not extracting the contours at all, because the statement (version, _, _) = cv2.__version__.split('.') returns 4 and your "if and else" both fail.
Please note the following line of code as well.
contours, hierarchy = cv2.findContours(thresh1.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
This is changed because
Since OpenCV 3.2, findContours() no longer modifies the source image.
I hope this will solve your issue.

Fitting ellipse to random distributed uniform regular shapes

We can think of the shapes in the representative picture as randomly scattered pencils or sticks on a table. I've been trying to find the areas of each shape by fitting ellipses, but I haven't been able to fit ellipses properly. Can you help me? Thanks.
First image is : input image
The code that I tried,
import cv2
import numpy as np
import random as rng
import math
img = cv2.imread('sticks.png', 1)
imge= cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
gray = cv2.cvtColor(imge, cv2.COLOR_BGR2GRAY)
blur = cv2.blur(gray, (2,2), 3)
rng.seed(1)
def thresh_callback(val):
threshold = val
canny_output = cv2.Canny(blur, threshold, threshold * 4)
contours, _ = cv2.findContours(canny_output, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
minRect = [None]*len(contours)
minEllipse = [None]*len(contours)
for i, c in enumerate(contours):
minRect[i] = cv2.minAreaRect(c)
if c.shape[0] > 5:
minEllipse[i] = cv2.fitEllipse(c)
(x,y),(minor_axis,major_axis),angle = minEllipse[i]
half_major= major_axis/2
half_minor= minor_axis/2
pixel= 37.795275591
half_major1= half_major/pixel
half_minor1= half_minor/pixel
area= math.pi * half_major1 * half_major1
print(area)
drawing = np.zeros((canny_output.shape[1], canny_output.shape[1], 3), dtype=np.uint8)
for i, c in enumerate(contours):
color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256))
cv2.drawContours(drawing, contours, i, color)
if c.shape[0] > 5:
cv2.ellipse(drawing, minEllipse[i], color, 1)
cv2.imshow('Fitting Ellips', drawing)
source_window = 'Source'
cv2.namedWindow(source_window)
cv2.imshow(source_window, img)
max_thresh = 255
thresh = 100
cv2.createTrackbar('Canny Thresh:', source_window,thresh, max_thresh, thresh_callback)
thresh_callback(thresh)
cv2.waitKey()
Second image is: expected result (fitting ellipse each line like this)
This is not the final result and definitely has errors. You need to take the time to achieve the desired result. But it can be a good idea to start with:
import sys
import cv2
import math
import numpy as np
# Check it there is a black area in specific position of an image
def checkPointArea(im, pt):
x, y = pt[0], pt[1]
return im[y, x, 0] == 0 or im[y, x+1, 0] == 0 or im[y, x-1, 0] == 0 or im[y+1, x, 0] == 0 or im[y-1, x, 0] == 0
# Load image
pth = sys.path[0]
im = cv2.imread(pth+'/im.jpg')
H, W = im.shape[:2]
# Make grayscale and black and white versions
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
bw = cv2.threshold(im, 110, 255, cv2.THRESH_BINARY)[1]
# Try to clear the parts of the image that are stuck together
bw = cv2.dilate(bw, np.ones((5, 5), np.uint8))
# Convert im back to BGR
im = cv2.cvtColor(im, cv2.COLOR_GRAY2BGR)
# Make some copies
org = im.copy()
empty = im.copy()
empty[:] = 255
# Find contours and sort them by position
cnts, _ = cv2.findContours(bw, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnts.sort(key=lambda x: cv2.boundingRect(x)[0])
# Thikness of random lines
thickness = 5
# Find and draw ellipses
for cnt in cnts:
x, y, w, h = cv2.boundingRect(cnt)
if w < W:
cv2.rectangle(im, (x, y), (x+w, y+h), (10, 230, 0)
if w < h else (200, 0, 128), 1)
hw, hh = w//2, h//2
cx, cy = x+hw, y+hh
r = int(math.sqrt(w**2+h**2))
t, c = math.atan(hw/hh), (255, 0, 0)
if checkPointArea(org, (x, y)) and checkPointArea(org, (x+w-1, y+h-1)):
t, c = math.atan(hw/-hh), (100, 0, 200)
deg = math.degrees(t)
if w <= thickness*2:
deg = 0
if h <= thickness*2:
deg = 90
cv2.ellipse(im, (x, y), (1, 1), 0, 0, 360, c, 4)
cv2.ellipse(im, (cx, cy), (thickness, r//2),
deg, 0, 360, (40, 0, 255), 2, lineType=cv2.LINE_AA)
#cv2.ellipse(empty, (x, y), (1, 1), 0, 0, 360, c, 2)
cv2.ellipse(empty, (cx, cy), (thickness, r//2),
deg, 0, 360, c, 2, lineType=cv2.LINE_AA)
# Save output
bw = cv2.cvtColor(bw, cv2.COLOR_GRAY2BGR)
top = np.hstack((org, empty))
btm = np.hstack((bw, im))
cv2.imwrite(pth+'/im_.png', np.vstack((top, btm)))
Each section:
Final Result:
Errors:
You have to spend more time for these two parts, the first is due to my weak code. Removable with more time. The second is due to the overlap of two lines. Clearing the image did not help this part. You may be able to prevent such interference from occurring later.

How to check if a bounding box is over a AOI and when it leaves. OpenCV python

So what I am trying to do is use OpenCV to detect the boxes and make and Area of Interest. When a box goes over a AOI it will count it as one use. This works fine but it keeps adding the usage for each frame. I only want to do this once while there is a usage detected.
So what I do here is:
Find the contour from background subtraction. Works
Get the bounding boxes for each blob detected. Works
Loop over each AOI box and use a Intersection over Union to see if
there is any overlap.
I set the counted boolean in the AOI list to True. Only if it is
False to begin with.
I ignore it if its been counted
If there is no intersection over union then I set the counted to
False.
What goes wrong here is that it either keeps counting usage for every frame or it will only count it once and never again. I want to count it for each unique usage and not for every frame. This is what I have so far.
# # loop over the contours
for c in contours:
# Filter out the blobs that are too small to be considered cars.
contours = filter(lambda cont: cv2.contourArea(cont) > 30, contours)
# compute the bounding box for the contour
(x, y, w, h) = cv2.boundingRect(c)
#Find each Area Of Interest usage
#detectUsage(area_of_interest, c, frame)
for aoi in area_of_interest:
#Check if there is a overlap between the detected contour and AOI
if gvp.machineUseCheck(c, aoi[1]):
print("{} in use".format(aoi[0]))
cv2.rectangle(frame, (x, y), (x + w, y + h), (222, 150, 1), 2)
if aoi[2] == False:
aoi[2] = True
print("{} set to True".format(aoi[0]))
elif aoi[2] == True:
print("{} Already true".format(aoi[0]))
elif gvp.machineUseCheck(c, aoi[1]) == False:
aoi[2] = False
print("{} not in use".format(aoi[0]))
#Get all the centers for the bounding boxes
center = (int(x + w / 2), int(y + h / 2))
cv2.circle(frame, center, 4, (0, 0, 255), -1)
I figured it out all I needed to do was reorganize the for loop a bit.
for aoi in area_of_interest:
cv2.putText(frame, "AOI Value:{}".format(aoi[2]), (10, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.6,
(0, 255, 0), 2)
cv2.putText(frame, "{} Count:{}".format(aoi[0], str(aoi[3])), (10, aoi[4]), cv2.FONT_HERSHEY_SIMPLEX,
0.6, (0, 255, 0), 2)
i = 0
for c in contours:
# Filter out the blobs that are too small to be considered cars.
contours = filter(lambda cont: cv2.contourArea(cont) > 30, contours)
# compute the bounding box for the contour
(x, y, w, h) = cv2.boundingRect(c)
center = (int(x + w / 2), int(y + h / 2))
cv2.circle(frame, center, 4, (0, 0, 255), -1)
if gvp.machineUseCheck(c, aoi[1]):
if not aoi[2]:
aoi[2] = True
aoi[3] += 1
cv2.putText(frame, "Machine IN USE:", (10, 130), cv2.FONT_HERSHEY_SIMPLEX, 0.6,
(0, 255, 0), 2)
cv2.rectangle(frame, (x, y), (x + w, y + h), (222, 150, 1), 2)
break
elif i == len(c):
aoi[2] = False
break
i += 1

Labeled unique contours (or draw independient contours)

Im working in python and opencv library.
I can threshold a camera capture, find contours (more than one) and draw.
But I have a problem. I try to identify those contours with an unique id or tag. (for example: Id: 1 , Id:2) to track them.
I need this contours use a persistent id.
The goal is draw a line and count more than one contour and sometimes more than one near contours converts in a big one.
Note: Im working with a depth camera and my image its an array of depth.
add a piece of code.
Thanks in advance.
countours = cv2.findContours(mask, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)[1]
# only proceed if at least one contour was found
if len(countours) > 0:
# find the largest contour in the mask, then use
# it to compute the minimum enclosing circle and
# centroid
for (i,c) in enumerate(countours):
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
if M["m00"] > 0:
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
centerString = str(center)
x = (int(M["m10"] / M["m00"]))
y = (int(M["m01"] / M["m00"]))
else:
center = int(x), int(y)
if radius > 10:
# draw the circle and centroid on the frame,
cv2.circle(frame, (int(x), int(y)), int(radius),
(0, 255, 255), 2)
cv2.circle(frame, center, 5, (0, 0, 255), -1)
# then update the ponter trail
if self.previous_position:
cv2.line(self.trail, self.previous_position, center,
(255, 255, 255), 2)
cv2.add(self.trail, frame, frame)
print center
self.previous_position = center
if len(countours) < 1:
center = 0
self.trail = numpy.zeros((self.cam_height, self.cam_width, 3),
numpy.uint8)
self.previous_position = None
Two options. First off, the contours are already in a Python list, so the indices of that list can be used to enumerate them. In fact you're already doing that in some sense with (i,c) in enumerate(countours). You can just use the index i to 'color' each contour with the value i drawing on a blank image, and then you'll know which contour is which just by examining the image. The other, probably better option IMO, is to use cv2.connectedComponents() to label the binary images instead of the contours of the binary image. Also pre-labeling you might try morphological operations to close up blobs.
EDIT: I follow your recommendation and I have new problems :)
Label is not persistent. When I move both contours, numbers of labels change.
Add some pictures and my code.
Hand label 0 and circle 1
Hand label 1 and circle 0
while True:
cnt += 1
if (cnt % 10) == 0:
now = time.time()
dt = now - last
fps = 10/dt
fps_smooth = (fps_smooth * smoothing) + (fps * (1.0-smoothing))
last = now
c = dev.color
cad = dev.cad
dev.wait_for_frames()
c = cv2.cvtColor(c, cv2.COLOR_RGB2GRAY)
depth = dev.depth * dev.depth_scale * 1000
print depth
#d = cv2.applyColorMap(depth.astype(np.uint8), cv2.COLORMAP_SUMMER)
print depth
res = np.float32(dev.depth)
depth = 255 * np.logical_and(depth >= 100, depth <= 500)
res2 = depth.astype(np.uint8)
kernel = np.ones((3, 3), np.uint8)
#res2 = cv2.blur(res2,(15,15))
res2 = cv2.erode(res2, kernel, iterations=4)
res2 = cv2.dilate(res2, kernel, iterations=8)
im_floodfill = res2.copy()
h, w = res2.shape[:2]
mask2 = np.zeros((h + 2, w + 2), np.uint8)
cv2.floodFill(im_floodfill, mask2, (0, 0), 255)
im_floodfill_inv = cv2.bitwise_not(im_floodfill)
im_out = res2 | im_floodfill_inv
im_out = cv2.blur(im_out,(5,5))
label = cv2.connectedComponentsWithStats(im_out)
n = label[0] - 1
cog = np.delete(label[3], 0, 0)
for i in xrange(n):
# im = cv2.circle(im,(int(cog[i][0]),int(cog[i][1])), 10, (0,0,255), -1)
cv2.putText(im_out, str(i), (int(cog[i][0]), int(cog[i][1])), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
cv2.imshow("Depth", res)
cv2.imshow("OUT", im_out)
cv2.imshow( "C", res2)

Categories