如何检测与背景融合的对象?
我是一个初学者,我正在尝试将轮廓应用于左侧的白色遥控器,该遥控器与背景共享相同的颜色。
a = cv2.imread(file_name)
imgGray = cv2.cvtColor(a,cv2.COLOR_BGR2GRAY)
imgGray = cv2.GaussianBlur(imgGray,(11,11),20)
k5 = np.array([[-1,-1,-1],[-1,9,-1],[-1,-1,-1]])
imgGray = cv2.filter2D(imgGray,-1,k5)
cv2.namedWindow("Control")
cv2.createTrackbar("blocksize","Control",33,1000,f)
cv2.createTrackbar("c","Control",3,100,f)
while True:
strel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
blocksize = cv2.getTrackbarPos("blocksize","Control")
c = cv2.getTrackbarPos("c","Control")
if blocksize%2==0:
blocksize += 1
thrash = cv2.adaptiveThreshold(imgGray,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV,blockSize=blocksize,C=c)
thrash1 = cv2.adaptiveThreshold(imgGray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,blockSize=blocksize,C=c)
cv2.imshow("mean",thrash)
cv2.imshow("gaussian",thrash1)
#r,thrash = cv2.threshold(imgGray,150,255,cv2.THRESH_BINARY_INV)
key = cv2.waitKey(1000)
if key == 32 or iter == -1:
break
edges = cv2.Canny(thrash,100,200)
cv2.imshow('sharpen',sharpen)
cv2.imshow('edges',edges)
cv2.imshow('grey ',imgGray)
cv2.imshow('thrash ',thrash)
cv2.waitKey(0)
circles = cv2.HoughCircles(imgGray,cv2.HOUGH_GRADIENT,1,60,param1=240,param2=50,minRadius=0,maxRadius=0)
contours,_ = cv2.findContours(thrash,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
putlabel(circles,a,contours)
这些是我尝试的东西,我还尝试了形态学操作,例如扩张,侵蚀,打开和关闭,但我仍然无法获得结果。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为简单的图像处理无法隔离具有与背景相同颜色的对象。因此,我们必须切换到深度/机器学习。这个想法是使用u-2-net 删除图像的背景我们的前景中所有对象的面膜,然后在白色上hsv颜色阈值以隔离对象。
后的结果掩码,
这是通过U-2-NET运行它以删除背景位并隔离对象
现在我们可以使用传统的图像处理,因为我们可以区分前景和背景。接下来,我们的HSV颜色阈值具有较低/上颜色范围,以隔离白色,从而插入该面罩。您可以使用。
现在,我们只是执行一些形态操作,以清理任何噪音,查找轮廓并按最大的轮廓区域排序。最大轮廓将是我们所需的对象的假设。 这是结果
代码,我很想知道如何!
如果有人使用简单的图像处理而不是深度/机器学习,那么
I don't think simple image-processing will be able to isolate an object with the same color as the background. Therefore we have to switch to deep/machine learning. The idea is to remove the background of the image using U-2-Net which will give us a mask of all objects in the foreground then HSV color threshold on white to isolate the object.
Here's the result mask after running it through U-2-Net to remove the background
Bitwise-and to isolate objects
Now we can use traditional image-processing since we can distinguish between the foreground and background. Next we HSV color threshold with a lower/upper color range to isolate white which results in this mask. You can use a HSV color thresholder script to determine the lower/upper ranges.
Now we simply perform a bit of morphological operations to clean up any noise, find contours, and sort by largest contour area. The assumption that the largest contour will be our desired object. Here's the result
Code
If anyone has an approach using simple image processing instead of deep/machine learning, I would love to know how!
我想到了一种纯粹的图像处理方法。但是结果不如@nathancy
理论
TLDR所描绘的结果准确。我使用的是高斯(Dog)的差异,这是一个2级边缘检测器。
模糊操作通常充当高频的抑制剂。通过减去两个不同模糊操作的结果,我们获得了一个带通滤波器。我想从“从另一个中减去一个模糊的图像,保存在两个模糊图像中保存的频率范围之间的空间信息”,
我写了一个简单的函数,返回两个模糊图像的差异:
方法
注意:范围是轮廓的特性,这是<的范围,这是<范围的特性,是<强>轮廓面积与其相应的边界矩形区域。 从此处获取
代码&amp;结果
如您所见,它可以用作边缘检测器。您可以更改内核大小(
k1,k2
)和sigma值(s1,s2
)您可以看到,结果不是完美的。在边缘检测过程(高斯差异)期间,还捕获了物体的阴影。您可以尝试更改参数以检查结果是否会变得更好。
I have thought of a pure image processing approach. But the results are not as accurate as the one depicted by @nathancy
Theory
TLDR; I am using Difference of Gaussians (DoG) which is a 2-stage edge detector.
Blurring operation generally acts as a suppressor of high frequencies. By subtracting the result of two different blurring operations we get a band-pass filter. I would like to quote from this blog "Subtracting one blurred image from the other preserves spatial information that lies between the range of frequencies that are preserved in the two blurred images"
I wrote a simple function that returns the difference of two blurred images:
Approach
Note: Extent is a property of a contour which is the ration of the contour area to its corresponding bounding rectangle area. Taken from here
Code & Results
As you can see, it functions as an edge detector. You can vary the kernel sizes (
k1, k2
) and sigma values (s1, s2
)As you can see, the result is not perfect. The shadows of the objects are also captured during the edge detection process (Difference of Gaussians). You can try varying the parameters to check if the result gets better.