工作中,遇到的问题,原来论坛中是C++版本,自己写成的python版本,仅供参考。
原文链接
How to remove black/shadows regions of colored Image via OpenCV - OpenCV Q&A Forum
cvtColor(src, gray, CV_BGR2GRAY);
// get full available range
double minGray,maxGray;
cv::minMaxLoc(gray, &minGray, &maxGray);
//suppose current range is 30...220
bool useRelative = true;
if(useRelative)
{
// Relative clipping dark range to black
double clipPercent = 10;
minGray = cvRound(minGray * (1 + clipPercent/100.0) );
//all below minGray+10% will become black
}
else
{
//absolute clipping. Use a fixed lower bound for dark regions
double minGrayWanted = 50;
minGray = minGrayWanted;
//all below 50 will become black
}
// current range
float inputRange = maxGray - minGray;
alpha = 255.0 / inputRange; // alpha expands current range. MaxGray will be 255
beta = -minGray * alpha; // beta shifts current range so that minGray will go to 0
src.convertTo(dst, -1, alpha, beta);
python版本
def shape_detection_v3(img):
'''识别到颜色之后,进行形状检测
c++>python
'''
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
minGray ,maxGray,_,_ = cv2.minMaxLoc(gray,mask=None)
useRelative = True
if useRelative:
# 相对裁剪暗范围到黑色
clipPercent = 10
minGray = np.round(minGray*(1+clipPercent/100.0))
else:
minGrayWanted = 100
minGray = minGrayWanted
inputRange = maxGray-minGray
alpha = 255.0/inputRange
beta = -minGray*alpha
'''
src.convertTo(dst,-1,alpha,beta)
@param参数列表
(
OutputArray,m
int rtype,
double alpha = 1 , 可选比例因子
double beta = 0 , 添加到缩放值的可选增量。
)
'''
res = cv2.convertScaleAbs(img,alpha,beta)
cv2.imshow('img',img)
cv2.imshow('res',res)
cv2.waitKey(0)
if cv2.waitKey(10)==27:
exit(0)
个人心得
想消除图片的阴影的话,在处理灰度图之前,可以尝试一下增加图片对比度。
'''转入灰度图之前增强对比度'''
Alpha = 65.6
Gamma=-8191.5
cal = cv2.addWeighted(img, Alpha,img, 0, Gamma)
gray = cv2.cvtColor(cal, cv2.COLOR_BGR2GRAY)
获取亮度和对比度数值的代码也随手分享一下
'''BrightnessContrast,controller查看对比度'''
def BrightnessContrast(brightness=0):
# getTrackbarPos returns the current
# position of the specified trackbar.
brightness = cv2.getTrackbarPos('Brightness',
'GEEK')
contrast = cv2.getTrackbarPos('Contrast',
'GEEK')
effect = controller(img, brightness,contrast)
# The function imshow displays an image
# in the specified window
cv2.imshow('test', effect)
cv2.imshow('test', effect)
if cv2.waitKey(10)==27:
exit(0)
# cv2.destoryAllWindows()
def controller(img,brightness=255,contrast=127):
# 亮度
brightness = int((brightness - 0) * (255 - (-255)) / (510 - 0) + (-255))
#对比度
contrast = int((contrast - 0) * (127 - (-127)) / (254 - 0) + (-127))
if brightness!=0:
if brightness>0:
shadow = brightness
max=255
else:
shadow = 0
max = 255+brightness
al_pha = (max-shadow)/255
ga_mma = shadow
cal = cv2.addWeighted(img,al_pha,img,0,ga_mma)
else:
cal = img
if contrast != 0:
Alpha = float(131 * (contrast + 127)) / (127 * (131 - contrast))
Gamma = 127 * (1 - Alpha)
# The function addWeighted calculates
# the weighted sum of two arrays
# print(Alpha,Gamma)
# 65.5 - 8191.5
cal = cv2.addWeighted(cal, Alpha,
cal, 0, Gamma)
# putText renders the specified text string in the image.
cv2.putText(cal, 'B:{},C:{}'.format(brightness,
contrast), (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
return cal
if __name__ =='__main__':
img = cv2.imread('color.png')
iimg = img.copy()
cv2.namedWindow('GEEK')
cv2.imshow('GEEK',img)
cv2.createTrackbar('Brightness',
'GEEK', 255, 2 * 255,
BrightnessContrast)
# Contrast range -127 to 127
cv2.createTrackbar('Contrast', 'GEEK',
127, 2 * 127,
BrightnessContrast)
BrightnessContrast(0)
cv2.waitKey(0)