def getOtsuBinarizedImage(self,isInverted):
image = self.blurImage()
threshType = cv2.THRESH_BINARY_INV if isInverted else cv2.THRESH_BINARY
retval, threshed = cv2.threshold(image,0,255,threshType + cv2.THRESH_OTSU)
return threshed
python类THRESH_OTSU的实例源码
def detect_shirt2(self):
self.hsv=cv2.cvtColor(self.norm_rgb,cv.CV_BGR2HSV)
self.hue,s,_=cv2.split(self.hsv)
_,self.dst=cv2.threshold(self.hue,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
self.fg=cv2.erode(self.dst,None,iterations=3)
self.bg=cv2.dilate(self.dst,None,iterations=1)
_,self.bg=cv2.threshold(self.bg,1,128,1)
mark=cv2.add(self.fg,self.bg)
mark32=np.int32(mark)
cv2.watershed(self.norm_rgb,mark32)
m=cv2.convertScaleAbs(mark32)
_,m=cv2.threshold(m,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cntr,h=cv2.findContours(m,cv.CV_RETR_EXTERNAL,cv.CV_CHAIN_APPROX_SIMPLE)
print len(cntr)
#print cntr[0].shape
#cntr[1].dtype=np.float32
#ret=cv2.contourArea(np.array(cntr[1]))
#print ret
#cntr[0].dtype=np.uint8
cv2.drawContours(m,cntr,-1,(255,255,255),3)
cv2.imshow("mask_fg",self.fg)
cv2.imshow("mask_bg",self.bg)
cv2.imshow("mark",m)
def find_contours(img):
'''
:param img: (numpy array)
:return: all possible rectangles (contours)
'''
img_blurred = cv2.GaussianBlur(img, (5, 5), 1) # remove noise
img_gray = cv2.cvtColor(img_blurred, cv2.COLOR_BGR2GRAY) # greyscale image
# cv2.imshow('', img_gray)
# cv2.waitKey(0)
# Apply Sobel filter to find the vertical edges
# Find vertical lines. Car plates have high density of vertical lines
img_sobel_x = cv2.Sobel(img_gray, cv2.CV_8UC1, dx=1, dy=0, ksize=3, scale=1, delta=0, borderType=cv2.BORDER_DEFAULT)
# cv2.imshow('img_sobel', img_sobel_x)
# Apply optimal threshold by using Oslu algorithm
retval, img_threshold = cv2.threshold(img_sobel_x, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)
# cv2.imshow('s', img_threshold)
# cv2.waitKey(0)
# TODO: Try to apply AdaptiveThresh
# Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
# gaus_threshold = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 115, 1)
# cv2.imshow('or', img)
# cv2.imshow('gaus', gaus_threshold)
# cv2.waitKey(0)
# Define a stuctural element as rectangular of size 17x3 (we'll use it during the morphological cleaning)
element = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(17, 3))
# And use this structural element in a close morphological operation
morph_img_threshold = deepcopy(img_threshold)
cv2.morphologyEx(src=img_threshold, op=cv2.MORPH_CLOSE, kernel=element, dst=morph_img_threshold)
# cv2.dilate(img_threshold, kernel=np.ones((1,1), np.uint8), dst=img_threshold, iterations=1)
# cv2.imshow('Normal Threshold', img_threshold)
# cv2.imshow('Morphological Threshold based on rect. mask', morph_img_threshold)
# cv2.waitKey(0)
# Find contours that contain possible plates (in hierarchical relationship)
contours, hierarchy = cv2.findContours(morph_img_threshold,
mode=cv2.RETR_EXTERNAL, # retrieve the external contours
method=cv2.CHAIN_APPROX_NONE) # all pixels of each contour
plot_intermediate_steps = False
if plot_intermediate_steps:
plot(plt, 321, img, "Original image")
plot(plt, 322, img_blurred, "Blurred image")
plot(plt, 323, img_gray, "Grayscale image", cmap='gray')
plot(plt, 324, img_sobel_x, "Sobel")
plot(plt, 325, img_threshold, "Threshold image")
# plot(plt, 326, morph_img_threshold, "After Morphological filter")
plt.tight_layout()
plt.show()
return contours
def recognize(self, points, frame, dictionary=None, limit=0.80, side_length=28, batch_size=3):
'''
Inputs:
points is marker.points param
frame is image frame
dictionary is Dictionary object
...
Outputs:
marker_id, rotations
>>> marker_i, rotations = detector.recognize(points, frame, dictionary=dictionary)
'''
dictionary = dictionary or self.dictionary
if dictionary is None: raise TypeError('recognize nead dictionary')
# To Gray
gray = frame
if len(gray.shape) == 3: gray = bgr2gray(frame)
# Convert the points, gray to local_points, local_gray
rect = self.localRect(points)
gray = self.localFrame(rect, gray)
points = self.localCorners(rect, points)
# Define src_points and dst_points, src: 0,1,2,3 -> dst: 1,0,3,2
points_src = np.float32(points)
points_dst = np.float32([[0,side_length],[0,0],
[side_length,0],[side_length,side_length]])
# Calc transform matrix and perspective dst map
M = cv2.getPerspectiveTransform(points_src, points_dst)
dst = cv2.warpPerspective(gray, M, (side_length, side_length))
# Begin recognize
_, dst = cv2.threshold(dst, dst.mean(), 1, cv2.THRESH_OTSU)
# Probables
probables = []
marker_dict = dictionary.getDict()
# for marker_id, hash_map in dictionary.getDict():
for marker_id in marker_dict:
hash_map = marker_dict[marker_id]
if dst.shape != hash_map.shape: hash_map = cv2.resize(hash_map, dst.shape[::-1])
deviation = rotations = 0
for i in range(4):
now_deviation = np.sum((dst == hash_map).astype(int)) / (side_length**2)
if now_deviation > deviation: deviation, rotations = now_deviation, i
hash_map = np.rot90(hash_map)
if deviation > limit:
probables.append((deviation, marker_id, rotations))
if len(probables) > batch_size: break
# Best of marker_id and rotations
if len(probables) > 0:
return max(probables, key=lambda item:item[0])[1:]
def recognize(self, points, frame, dictionary=None, limit=0.80, side_length=28, batch_size=3):
'''
Inputs:
points is marker.points param
frame is image frame
dictionary is Dictionary object
...
Outputs:
marker_id, rotations
>>> marker_i, rotations = detector.recognize(points, frame, dictionary=dictionary)
'''
dictionary = dictionary or self.dictionary
if dictionary is None: raise TypeError('recognize nead dictionary')
# To Gray
gray = frame
if len(gray.shape) == 3: gray = bgr2gray(frame)
# Convert the points, gray to local_points, local_gray
rect = self.localRect(points)
gray = self.localFrame(rect, gray)
points = self.localCorners(rect, points)
# Define src_points and dst_points, src: 0,1,2,3 -> dst: 1,0,3,2
points_src = np.float32(points)
points_dst = np.float32([[0,side_length],[0,0],
[side_length,0],[side_length,side_length]])
# Calc transform matrix and perspective dst map
M = cv2.getPerspectiveTransform(points_src, points_dst)
dst = cv2.warpPerspective(gray, M, (side_length, side_length))
# Begin recognize
_, dst = cv2.threshold(dst, dst.mean(), 1, cv2.THRESH_OTSU)
# Probables
probables = []
marker_dict = dictionary.getDict()
# for marker_id, hash_map in dictionary.getDict():
for marker_id in marker_dict:
hash_map = marker_dict[marker_id]
if dst.shape != hash_map.shape: hash_map = cv2.resize(hash_map, dst.shape[::-1])
deviation = rotations = 0
for i in range(4):
now_deviation = np.sum((dst == hash_map).astype(int)) / (side_length**2)
if now_deviation > deviation: deviation, rotations = now_deviation, i
hash_map = np.rot90(hash_map)
if deviation > limit:
probables.append((deviation, marker_id, rotations))
if len(probables) > batch_size: break
# Best of marker_id and rotations
if len(probables) > 0:
return max(probables, key=lambda item:item[0])[1:]
def recognize(self, points, frame, dictionary=None, limit=0.80, side_length=28, batch_size=3):
'''
Inputs:
points is marker.points param
frame is image frame
dictionary is Dictionary object
...
Outputs:
marker_id, rotations
>>> marker_i, rotations = detector.recognize(points, frame, dictionary=dictionary)
'''
dictionary = dictionary or self.dictionary
if dictionary is None: raise TypeError('recognize nead dictionary')
# To Gray
gray = frame
if len(gray.shape) == 3: gray = bgr2gray(frame)
# Convert the points, gray to local_points, local_gray
rect = self.localRect(points)
gray = self.localFrame(rect, gray)
points = self.localCorners(rect, points)
# Define src_points and dst_points, src: 0,1,2,3 -> dst: 1,0,3,2
points_src = np.float32(points)
points_dst = np.float32([[0,side_length],[0,0],
[side_length,0],[side_length,side_length]])
# Calc transform matrix and perspective dst map
M = cv2.getPerspectiveTransform(points_src, points_dst)
dst = cv2.warpPerspective(gray, M, (side_length, side_length))
# Begin recognize
_, dst = cv2.threshold(dst, dst.mean(), 1, cv2.THRESH_OTSU)
# Probables
probables = []
marker_dict = dictionary.getDict()
# for marker_id, hash_map in dictionary.getDict():
for marker_id in marker_dict:
hash_map = marker_dict[marker_id]
if dst.shape != hash_map.shape: hash_map = cv2.resize(hash_map, dst.shape[::-1])
deviation = rotations = 0
for i in range(4):
now_deviation = np.sum((dst == hash_map).astype(int)) / (side_length**2)
if now_deviation > deviation: deviation, rotations = now_deviation, i
hash_map = np.rot90(hash_map)
if deviation > limit:
probables.append((deviation, marker_id, rotations))
if len(probables) > batch_size: break
# Best of marker_id and rotations
if len(probables) > 0:
return max(probables, key=lambda item:item[0])[1:]
def recognize(self, points, frame, dictionary=None, limit=0.80, side_length=28, batch_size=3):
'''
Inputs:
points is marker.points param
frame is image frame
dictionary is Dictionary object
...
Outputs:
marker_id, rotations
>>> marker_i, rotations = detector.recognize(points, frame, dictionary=dictionary)
'''
dictionary = dictionary or self.dictionary
if dictionary is None: raise TypeError('recognize nead dictionary')
# To Gray
gray = frame
if len(gray.shape) == 3: gray = bgr2gray(frame)
# Convert the points, gray to local_points, local_gray
rect = self.localRect(points)
gray = self.localFrame(rect, gray)
points = self.localCorners(rect, points)
# Define src_points and dst_points, src: 0,1,2,3 -> dst: 1,0,3,2
points_src = np.float32(points)
points_dst = np.float32([[0,side_length],[0,0],
[side_length,0],[side_length,side_length]])
# Calc transform matrix and perspective dst map
M = cv2.getPerspectiveTransform(points_src, points_dst)
dst = cv2.warpPerspective(gray, M, (side_length, side_length))
# Begin recognize
_, dst = cv2.threshold(dst, dst.mean(), 1, cv2.THRESH_OTSU)
# Probables
probables = []
marker_dict = dictionary.getDict()
# for marker_id, hash_map in dictionary.getDict():
for marker_id in marker_dict:
hash_map = marker_dict[marker_id]
if dst.shape != hash_map.shape: hash_map = cv2.resize(hash_map, dst.shape[::-1])
deviation = rotations = 0
for i in range(4):
now_deviation = np.sum((dst == hash_map).astype(int)) / (side_length**2)
if now_deviation > deviation: deviation, rotations = now_deviation, i
hash_map = np.rot90(hash_map)
if deviation > limit:
probables.append((deviation, marker_id, rotations))
if len(probables) > batch_size: break
# Best of marker_id and rotations
if len(probables) > 0:
return max(probables, key=lambda item:item[0])[1:]
def checkButton(self, img, x1, y1, x2, y2):
btn1 = img[y1:y2, x1:x2]
btn1 = cv2.cvtColor(btn1, cv2.COLOR_BGR2GRAY)
if self.thresh_change_trigger:
ret, mask = cv2.threshold(btn1, 0, 255, cv2.THRESH_BINARY_INV +
cv2.THRESH_OTSU)
self.thresh_val.setText(str(ret))
self.THRESH = ret
else:
ret, mask = cv2.threshold(btn1, self.THRESH, 255,
cv2.THRESH_BINARY_INV)
try:
(_, cnts, _) = cv2.findContours(mask, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
except Exception, e:
(cnts, _) = cv2.findContours(mask, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
ci = 0
max_area = 0
if cnts:
for i in range(len(cnts)):
cnt = cnts[i]
area = cv2.contourArea(cnt)
if(area > max_area):
max_area = area
ci = i
cnt = cnts[ci]
else:
cnt = None
self.flags.isSet_prev = self.flags.isSet_cur
if cnt is not None:
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 0), 1)
hull = cv2.convexHull(cnt)
cv2.drawContours(btn1, [hull], 0, (0, 0, 255), 1)
self.flags.isSet_cur = True
else:
cv2.rectangle(img, (x1, y1), (x2, y2), (188, 188, 137), 1)
self.flags.isSet_cur = False
return img
def find_black_center(cv_img, msk):
"""
Given an opencv image containing a dark object on a light background
and a mask of objects to ignore (a gripper, for instance),
return the coordinates of the centroid of the largest object
(excluding those touching edges) and its simplified contour.
If none detected or problem with centroid, return [(-1, -1), False].
"""
# Convert to black and white
(rows, cols, _) = cv_img.shape
grey_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY)
grey_img = cv2.bilateralFilter(grey_img, 11, 17, 17)
_, outlines = cv2.threshold(
grey_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# Subtract gripper
msk_out = cv2.subtract(cv2.bitwise_not(outlines), msk)
# Remove objects touching edges
flood_fill_edges(msk_out, 30)
# Find contours
_, contours, _ = cv2.findContours(
msk_out, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(contours) == 0:
return [(-1, -1), False]
# Find largest contour
max_area = 0
for cnt in contours:
area = cv2.contourArea(cnt)
if area > max_area:
contour = cnt
max_area = area
# Approximate contour
epsilon = 0.025 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)
# Find centroid
try:
M = cv2.moments(approx)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
return [(cx, cy), approx]
except ZeroDivisionError:
return [(-1, -1), False]
def binaryMask(frame, x0, y0, width, height ):
global guessGesture, visualize, mod, lastgesture, saveImg
cv2.rectangle(frame, (x0,y0),(x0+width,y0+height),(0,255,0),1)
roi = frame[y0:y0+height, x0:x0+width]
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),2)
#blur = cv2.bilateralFilter(roi,9,75,75)
th3 = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,2)
ret, res = cv2.threshold(th3, minValue, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
#ret, res = cv2.threshold(blur, minValue, 255, cv2.THRESH_BINARY +cv2.THRESH_OTSU)
if saveImg == True:
saveROIImg(res)
elif guessGesture == True:
retgesture = myNN.guessGesture(mod, res)
if lastgesture != retgesture :
lastgesture = retgesture
#print lastgesture
## Checking for only PUNCH gesture here
## Run this app in Prediction Mode and keep Chrome browser on focus with Internet Off
## And have fun :) with Dino
if lastgesture == 3:
jump = ''' osascript -e 'tell application "System Events" to key code 49' '''
#jump = ''' osascript -e 'tell application "System Events" to key down (49)' '''
os.system(jump)
print myNN.output[lastgesture] + "= Dino JUMP!"
#time.sleep(0.01 )
#guessGesture = False
elif visualize == True:
layer = int(raw_input("Enter which layer to visualize "))
cv2.waitKey(1)
myNN.visualizeLayers(mod, res, layer)
visualize = False
return res
#%%