gpt4 book ai didi

opencv - 在嘈杂的背景中难以检测到小物体。有什么方法可以解决这个问题?

转载 作者:太空宇宙 更新时间:2023-11-03 20:46:28 24 4
gpt4 key购买 nike

我正在尝试制作一个计算机视觉程序,它可以在嘈杂的背景中检测垃圾和随机垃圾,例如海滩(由于沙子而嘈杂)。

原始图片:

enter image description here

无需任何图像处理的 Canny 边缘检测:

enter image description here

我意识到图像处理技术的某种组合将帮助我实现我的目标,即忽略嘈杂的沙地背景并检测地面上的所有垃圾和物体。

我尝试预先形成中值模糊,尝试并调整参数,它给了我这个:

enter image description here

它在忽略沙质背景方面表现良好,但未能检测到地面上的其他许多物体,可能是因为它被模糊了(不太确定)。

有没有什么方法可以改进我的算法或图像处理技术来忽略嘈杂的沙质背景,同时允许精明的边缘检测找到所有对象并让程序检测所有对象并在所有对象上绘制轮廓。

代码:

from pyimagesearch.transform import four_point_transform
from matplotlib import pyplot as plt
import numpy as np
import cv2
import imutils

im = cv2.imread('images/beach_trash_3.jpg')


#cv2.imshow('Original', im)

# Histogram equalization to improve contrast




###
#im = np.fliplr(im)

im = imutils.resize(im, height = 500)

imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

# Contour detection
#ret,thresh = cv2.threshold(imgray,127,255,0)

#imgray = cv2.GaussianBlur(imgray, (5, 5), 200)
imgray = cv2.medianBlur(imgray, 11)

cv2.imshow('Blurred', imgray)

'''
hist,bins = np.histogram(imgray.flatten(),256,[0,256])
plt_one = plt.figure(1)
cdf = hist.cumsum()
cdf_normalized = cdf * hist.max()/ cdf.max()

cdf_m = np.ma.masked_equal(cdf,0)
cdf_m = (cdf_m - cdf_m.min())*255/(cdf_m.max()-cdf_m.min())
cdf = np.ma.filled(cdf_m,0).astype('uint8')
imgray = cdf[imgray]

cv2.imshow('Histogram Normalization', imgray)
'''
'''
imgray = cv2.adaptiveThreshold(imgray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,11,2)
'''

thresh = imgray

#imgray = cv2.medianBlur(imgray,5)
#imgray = cv2.Canny(imgray,10,500)
thresh = cv2.Canny(imgray,75,200)
#thresh = imgray
cv2.imshow('Canny', thresh)


contours, hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:5]

test = im.copy()
cv2.drawContours(test, cnts, -1,(0,255,0),2)
cv2.imshow('All contours', test)

print '---------------------------------------------'
##### Code to show each contour #####
main = np.array([[]])
for c in cnts:
epsilon = 0.02*cv2.arcLength(c,True)
approx = cv2.approxPolyDP(c,epsilon,True)

test = im.copy()
cv2.drawContours(test, [approx], -1,(0,255,0),2)
#print 'Contours: ', contours
if len(approx) == 4:
print 'Found rectangle'
print 'Approx.shape: ', approx.shape
print 'Test.shape: ', test.shape

# frame_f = frame_f[y: y+h, x: x+w]
frame_f = test[approx[0,0,1]:approx[2,0,1], approx[0,0,0]:approx[2,0,0]]

print 'frame_f.shape: ', frame_f.shape
main = np.append(main, approx[None,:][None,:])
print 'main: ', main


# Uncomment in order to show all rectangles in image
#cv2.imshow('Show Ya', test)


#print 'Approx: ', approx.shape
#cv2.imshow('Show Ya', frame_f)
cv2.waitKey()
print '---------------------------------------------'
cv2.drawContours(im, cnts, -1,(0,255,0),2)
print main.shape
print main
cv2.imshow('contour-test', im)
cv2.waitKey()

最佳答案

我从你的问题中得到的理解是:你想从本质上可变的背景中分割出前景对象(沙灰度取决于许多其他条件)。

有多种方法可以解决此类问题:

方法一:

从您的图像中可以清楚地看出,背景颜色像素的数量总是比前景多得多,开始初始分割的最简单方法是:

  1. 将图像转换为灰色。
  2. 创建它的直方图。
  3. 找到直方图的峰值索引,即具有最大像素的索引。

以上三个步骤让您了解了背景但是游戏并没有就此结束,现在您可以将这个索引值放在中心,并在它周围取一系列值,比如上下 25 ,例如:如果您的峰值指数是 207(如您的情况),请选择从 75 到 225 的灰度范围和阈值图像,根据您背景的性质,上述方法可用于前景对象检测,分割后您必须执行一些后处理步骤,例如形态分析以在提取对象后分割出不同的对象,您可以应用一些分类内容来进行更精细的分割以消除误报。

方法 2:

玩一些图像像素的统计数据,比如制作一个小的灰度值数据集和

  1. 将它们标记为 1 类和 2 类,例如 1 表示沙子,2 表示前景,
  2. 找出两个类的像素的均值和方差(标准差),并计算两个类的概率(num_pix_per_class/total_num_pix),现在存储这些统计数据以备后用,
  3. 现在回到图像并逐个获取每个像素并应用高斯 pdf:1/2*pisigma(exp(-(pix - mean)/2*sigma));在 mean 处放置较早计算的平均值,在 sigma 处放置较早计算的 std 偏差。
  4. 应用阶段 3 后,您将获得两个类的每个像素的两个概率值,只需选择概率较高的类即可。

方法 3:

方法 3 比以上两种更复杂:您可以使用一些基于纹理的操作来分割出沙子类型的纹理,但是对于应用基于纹理的方法,我会推荐监督分类而不是无监督分类(如 k-means)。您可以使用的不同纹理特征是:

基本:

  1. 定义邻域中的灰度范围。
  2. 局部均值和方差或熵。
  3. 灰度共生矩阵 (GLCM)。

高级:

  1. 本地二进制模式。
  2. 小波变换。
  3. Gabor 变换。等

PS:我认为您应该尝试方法 1 和 2。它可以解决很多工作。 :)

关于opencv - 在嘈杂的背景中难以检测到小物体。有什么方法可以解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32430393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com