def thresholdImage(gray,thr_type,thr,block_size=None,img=None):
""" Where thr_type in {1,2,3,4}
1: Normal threshold
2: Otsu
3: Adaptive (mean)
4: Adaptive (Gaussian)
More thresholds: Using two thresholds taking into account that most pixels are from the floor
(Trying to don't erase black cars)
5: Double threshold (using percentiles)
6: Double threshold (using manually set values)
"""
if thr_type == 1:
ret,thr = cv2.threshold(gray,thr,255,cv2.THRESH_BINARY)
return thr
elif thr_type == 2:
ret,thr = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) # Black/red cars desapeared. Good for Segmentation of background
return thr
elif thr_type == 3:
return cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,block_size,2) # Less noise, but can't recognize all cars
elif thr_type == 4:
return cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,block_size,2) # More noise, more cars
elif thr_type == 5:
firstQ = np.percentile(gray2,65) # Return de value of the pixel that corresponds to the 65 percent of all sorted pixels in grayscale
secondQ = np.percentile(gray2,50)
thirdQ = np.percentile(gray2,35)
return applyThreshold(gray,firstQ,thirdQ)
elif thr_type == 6:
return applyThreshold(gray,40,113)
elif thr_type == 7:
rows,col = img[:,:,0].shape
r1,g1,b1 = getChannels(gray) # Actually is not grayscale but a BGR image (just a name)
r2,g2,b2 = getChannels(img)
res = np.zeros((rows,col))
for i in range(0,rows):
for j in range(0,col):
rDif = abs(int(r1[i,j]) - int(r2[i,j]))
gDif = abs(int(g1[i,j]) - int(g2[i,j]))
bDif = abs(int(b1[i,j]) - int(b2[i,j]))
if rDif >= thr or gDif >= thr or bDif >= thr:
res[i,j] = 0
else:
res[i,j] = 255
return res
else:
return None
car_recognizer.py 文件源码
python
阅读 22
收藏 0
点赞 0
评论 0
评论列表
文章目录