简体   繁体   English

使用 opencv 去除眩光并保持边缘从眩光中去除

[英]Removing glare with opencv and keeping edges removed from the glare

I have this image: https://imgur.com/9A7542w我有这张图片: https://imgur.com/9A7542w

And i am trying to get the contours of the image but as we can see in this: https://imgur.com/VU7KqiS where there is glare, the contours of some circles are not drawn.我正在尝试获取图像的轮廓,但正如我们在此看到的那样: https://imgur.com/VU7KqiS在有眩光的地方,没有绘制一些圆圈的轮廓。

I assume that if i get the glare off on this photo, when i use canny, the edges will be drawn correctly?我假设如果我在这张照片上消除眩光,当我使用 canny 时,边缘会被正确绘制吗?

I am new to openCV, i've read some post on here and tried out some techniques, but it didn't work out at all.我是 openCV 的新手,我在这里阅读了一些帖子并尝试了一些技术,但根本没有成功。

Note: i am doing this in python Anyone could help?注意:我在 python 中这样做有人可以帮忙吗? Thanks a lot.非常感谢。

What i tried first:我首先尝试的是:

import cv2 as cv

img = cv.imread('Photos/board.jpg')

canny = cv.Canny(img, 125, 175)

contours, hierarchies = cv.findContours(canny, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)

blank = np.zeros(img.shape, dtype='uint8')
cv.drawContours(blank, contours, -1, (0,0,255),1)
cv.imshow('Contours drawn', blank)

results : https://imgur.com/yLVFCh2结果https://imgur.com/yLVFCh2

Second attempt a bit better but useless things appears in the result第二次尝试更好一点但结果中出现了无用的东西

adaptive_thresh = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 13, 2)
cv.imshow('Adaptive thresholding', adaptive_thresh)

canny = cv.Canny(adaptive_thresh, 125, 175)
cv.imshow('Canny edges', canny)

Results (Theres so much white pixels that appear on the photo: https://imgur.com/mqljl1m结果(照片上出现了很多白色像素: https://imgur.com/mqljl1m

I think the easiest way to do this is to change the 2nd value of your Canny detection like this:我认为最简单的方法是像这样更改 Canny 检测的第二个值:

canny = cv.Canny(img, 25, 175)

the lower threshold (second argument) is set lower then you can avoid this glare effect.较低的阈值(第二个参数)设置得较低,那么您可以避免这种眩光效果。 More info here更多信息在这里

From my point of view, you can also work in hsv space which is more confomfortable if you want to extract informations from images with effects like this.从我的角度来看,如果您想从具有此类效果的图像中提取信息,您还可以在更舒适的 hsv 空间中工作。 More info about hsv .有关hsv的更多信息。 The Fig. 3 a) speaks for itself.图 3 a) 不言自明。

Here is the full code, you had some errors in yours (and maybe you use an old opencv release)这是完整的代码,您的代码中有一些错误(也许您使用的是旧的 opencv 版本)

import numpy as np
img = cv.imread(r'C:\Users\MyUser\Desktop\board.jpeg')
canny = cv.Canny(img, 25, 175)

img2, contours, hierarchy = cv.findContours(canny, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)

blank = np.zeros(img.shape, dtype='uint8')
cv.drawContours(blank, contours, -1, (0,0,255),1)
cv.imshow('Contours drawn', blank)
cv.waitKey(0)

EDIT: I also want to tell you that it'll be difficult to use the extracted coordinates here.编辑:我还想告诉你,在这里使用提取的坐标会很困难。 You'd better use circle detection and line detection to extract and use the coordinates of the board and pucks.您最好使用 圆检测线检测来提取和使用棋盘和冰球的坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM