[英]OpenCV - Find contours of area without clear borders
I'm having some images where I want to filter out the black areas using OpenCV with python. 我有一些图像,我想使用带Python的OpenCV过滤掉黑色区域。 The problem is that these images comes from satellite footage and have no clear borders.
问题在于这些图像来自卫星镜头,没有清晰的边界。
In the images added above, the first is the original image while the second is a blurred version, i'm interested in the small dark area in the middle. 在上面添加的图像中,第一个是原始图像,第二个是模糊图像,我对中间较小的深色区域感兴趣。 Ultimately I want to have this area marked as a singe contour area.
最终,我希望将此区域标记为单一轮廓区域。
My main problem seems to be that when I use the OpenCV findContours function, I get a lot of small contours instead of one (or two) big ones. 我的主要问题似乎是当我使用OpenCV findContours函数时,得到了许多小的轮廓,而不是一个(或两个)大轮廓。
I'm fairly new to OpenCV so any help would be appreciated! 我是OpenCV的新手,所以我们将不胜感激!
Here are just some rough results I have been able to obtain with a simple pipeline: 以下是我可以通过简单的管道获得的一些粗略结果:
The code is fairly self-explanatory too
该代码也是相当不言自明的
import cv2
import numpy as np
def nothing(x):
pass
cv2.namedWindow('image')
cv2.createTrackbar('high','image',0,255,nothing)
cv2.createTrackbar('low','image',0,255,nothing)
cv2.namedWindow('Edges')
while(1):
image = cv2.imread("PATH TO IMAGE HERE")
imgray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
high = cv2.getTrackbarPos('high', 'image')
low = cv2.getTrackbarPos('low', 'image')
edges = cv2.Canny(imgray, low, high)
kernel = np.ones((8, 8), np.uint8)
closing = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)
cv2.imshow('Edges', closing)
ret,thresh = cv2.threshold(closing,low,high,0)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
largest_area = 0
largest_contour_index = 0
counter = 0
for i in contours:
area = cv2.contourArea(i)
if (area > largest_area):
largest_area = area
largest_contour_index = counter
counter = counter + 1
cv2.drawContours(image, contours, largest_contour_index, (0,255,0), 1)
cv2.imshow('image', image)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
The pipeline is as follows: 管道如下:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.