【发布时间】:2015-12-02 12:30:12
【问题描述】:
我正在尝试制作一个计算机视觉程序,它可以在嘈杂的背景(例如海滩)中检测垃圾和随机垃圾(由于沙子而嘈杂)。
原图:
无需任何图像处理的 Canny 边缘检测:
我意识到图像处理技术的某种组合将帮助我实现我的目标,即忽略嘈杂的沙地背景并检测地面上的所有垃圾和物体。
我尝试进行中值模糊、调整和调整参数,它给了我这个:
它在忽略沙地背景方面表现良好,但它无法检测到地面上的其他许多物体,可能是因为它被模糊了(不太确定)。
有没有什么方法可以改进我的算法或图像处理技术,从而忽略嘈杂的沙地背景,同时允许精巧的边缘检测找到所有对象并让程序检测并在所有对象上绘制轮廓。
代码:
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()
【问题讨论】:
-
我不确定这个问题是否离题,因为您似乎在询问使用哪种算法的建议。
-
在 HSV 图像(H 通道)上尝试模糊和精巧,而不是灰度图像。和/或使用双边滤波器而不是中值。
-
您可以在不进行任何修改和添加且不进行有损压缩的情况下以完整尺寸发布原始输入图像吗?任何想向您展示一些算法的人都会很好。
-
沙子检测示例:HALACCI、Ibrahim 等。行星探测漫游车的地形分类和分类器融合。在:航空航天会议,2007 年 IEEE。 IEEE,2007 年。 1-11。 web.mit.edu/mobility/publications/Iagnemma_Aero_07b.pdf
-
This 会有所帮助。它有点类似于下面 Ankit 建议的方法 1。我用算法测试了给定的图像,它给出了合理的分割。
标签: opencv image-processing computer-vision edge-detection opencv-contour