从照片中删除眩光opencv

时间:2017-04-18 11:12:25

标签: android c++ opencv

因此,我使用opencv捕获文档,扫描并裁剪文档。当房间里没有灯光时,它可以很好地工作。当房间里有一些灯光,桌子上有眩光并且文件靠近它时,它也会抓住眩光作为矩形的一部分。

如何从照片中消除眩光?

以下是用于获取我想要的图像的代码:

 Mat &image = *(Mat *) matAddrRgba;
    Rect bounding_rect;

    Mat thr(image.rows, image.cols, CV_8UC1);
    cvtColor(image, thr, CV_BGR2GRAY); //Convert to gray
    threshold(thr, thr, 150, 255, THRESH_BINARY + THRESH_OTSU); //Threshold the gray

    vector<vector<Point> > contours; // Vector for storing contour
    vector<Vec4i> hierarchy;
    findContours(thr, contours, hierarchy, CV_RETR_CCOMP,
                 CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image
    sort(contours.begin(), contours.end(),
         compareContourAreas);            //Store the index of largest contour
    bounding_rect = boundingRect(contours[0]);

    rectangle(image, bounding_rect, Scalar(250, 250, 250), 5);

这是我正在谈论的眩光照片:

enter image description here

我发现的东西是使用inRange,找到适当的颜色标量,我们可以去除光线。这是一个代码片段,但它始终崩溃说它需要8位图像与chanels。

Mat &image = *(Mat *) matAddrRgba;

    Mat hsv, newImage, inpaintMask;
    cv::Mat lower_red_hue_range;
    inpaintMask = Mat::zeros(image.size(), CV_8U);
    cvtColor(image, hsv, COLOR_BGR2HSV);
    cv::inRange(hsv, cv::Scalar(0, 0, 215, 0), cv::Scalar(180, 255, 255, 0),
                lower_red_hue_range);
    image = lower_red_hue_range;

    inpaint(image, lower_red_hue_range, newImage, 3, INPAINT_TELEA);

2 个答案:

答案 0 :(得分:6)

之前我已经处理过这个问题,并且在计算机视觉中用于检测和描述图像时,照明的变化始终是一个问题。我实际上训练了一个分类器,用于HSV色彩空间而不是RGB / BGR,它将图像与不断变化的入射光映射到没有突然亮度/暗色块的图像(这将是标签)。这对我很有用,但是,图像总是具有相同的背景(我不知道你是否也有这个)。

当然,机器学习可以解决问题,但可能是一种矫枉过正。虽然我正在做上面提到的,但我遇到了CLAHE,这对于局部对比度增强非常有效。我建议你在检测轮廓之前尝试这个。此外,为此,您可能希望使用不同的颜色空间,例如HSV / Lab / Luv而不是RGB / BGR。您可以单独将CLAHE应用于每个通道,然后合并它们。

如果您需要其他信息,请与我们联系。我在python中使用你的图像实现了它,它工作得非常好,但我会把编码留给你。我可能会更新几天后得到的结果(希望你先得到它们;))。希望能帮助到你。

Gray image

V channel of HSV after CLAHE - clipLimit=10, TileGridSize= (16, 16)

答案 1 :(得分:3)

opencv-python 助手

input img

 import cv2
 import numpy as np
 import time


clahefilter = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(16,16))


img = cv2.imread('spects_glare.jpg')

while True:
t1 = time.time() 
img = img.copy()

## crop if required 
#FACE
x,y,h,w = 550,250,400,300
# img = img[y:y+h, x:x+w]

#NORMAL
# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
grayimg = gray


GLARE_MIN = np.array([0, 0, 50],np.uint8)
GLARE_MAX = np.array([0, 0, 225],np.uint8)

hsv_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

#HSV
frame_threshed = cv2.inRange(hsv_img, GLARE_MIN, GLARE_MAX)


#INPAINT
mask1 = cv2.threshold(grayimg , 220, 255, cv2.THRESH_BINARY)[1]
result1 = cv2.inpaint(img, mask1, 0.1, cv2.INPAINT_TELEA) 



#CLAHE
claheCorrecttedFrame = clahefilter.apply(grayimg)

#COLOR 
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
lab_planes = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=2.0,tileGridSize=(8,8))
lab_planes[0] = clahe.apply(lab_planes[0])
lab = cv2.merge(lab_planes)
clahe_bgr = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)


#INPAINT + HSV
result = cv2.inpaint(img, frame_threshed, 0.1, cv2.INPAINT_TELEA) 


#INPAINT + CLAHE
grayimg1 = cv2.cvtColor(clahe_bgr, cv2.COLOR_BGR2GRAY)
mask2 = cv2.threshold(grayimg1 , 220, 255, cv2.THRESH_BINARY)[1]
result2 = cv2.inpaint(img, mask2, 0.1, cv2.INPAINT_TELEA) 



#HSV+ INPAINT + CLAHE
lab1 = cv2.cvtColor(result, cv2.COLOR_BGR2LAB)
lab_planes1 = cv2.split(lab1)
clahe1 = cv2.createCLAHE(clipLimit=2.0,tileGridSize=(8,8))
lab_planes1[0] = clahe1.apply(lab_planes1[0])
lab1 = cv2.merge(lab_planes1)
clahe_bgr1 = cv2.cvtColor(lab1, cv2.COLOR_LAB2BGR)




# fps = 1./(time.time()-t1)
# cv2.putText(clahe_bgr1    , "FPS: {:.2f}".format(fps), (10, 180), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255))    

# display it
cv2.imshow("IMAGE", img)
cv2.imshow("GRAY", gray)
cv2.imshow("HSV", frame_threshed)
cv2.imshow("CLAHE", clahe_bgr)
cv2.imshow("LAB", lab)
cv2.imshow("HSV + INPAINT", result)
cv2.imshow("INPAINT", result1)
cv2.imshow("CLAHE + INPAINT", result2)  
cv2.imshow("HSV + INPAINT + CLAHE   ", clahe_bgr1)


# Break with esc key
if cv2.waitKey(1) & 0xFF == ord('q'): 
    break


cv2.destroyAllWindows()

output