OpenCV - 使用cv2.canny和cv2.findContours查找异构图像上包含数据的区域

时间:2018-03-28 12:56:14

标签: python image opencv object-detection image-recognition

这是我的第一个问题,所以我要求理解。我必须处理数百颗卫星图像 我试图找到位于图像上的有用数据区域的轮廓 - 只有最大的一个。 enter image description here然后我想保存对应于这个轮廓的几个点(x,y)的坐标。在最简单的情况下,该区域是正方形并且可以由4个点表示,但是对于更复杂的形状,轮廓将通过更多点(优选地不超过~15个)来近似。但是我仍然无法在我的图像上找到这些区域。有时该区域接触图像的边缘。因此,在此脚本中,我放大图片并添加由背景颜色填充的其他边界。您可以在此处找到的图片示例satellite1satellite2satellite3 如您所见,图像可以具有不同的背景颜色,此外它们还包含国家边界和图例。我曾尝试使用Aidenhjj提示OpenCV - using cv2.approxPolyDP() correctly并准备好我的剧本。我尝试了很多方法,过滤和调整参数,但仍然无法成功处理我的数据。我在请你帮忙。

import numpy as np
import cv2
import matplotlib.pyplot as plt

image = cv2.imread('image1.jpg')
image = cv2.resize(image, None,fx=0.25, fy=0.25, interpolation = cv2.INTER_CUBIC)
ysize, xsize, channels = image.shape
print("Image size: {} x {}".format(xsize, ysize))

#calculate the histograms in r,g,b channels, measure background color
r, g, b = cv2.split(image)
image_data = image

histr = cv2.calcHist([r],[0],None,[256],[0,256])
for y in range(0,len(histr)):
    elem = histr[y]
    if elem == histr.max():
     break
else:
    y = none
R=y

histr = cv2.calcHist([g],[0],None,[256],[0,256])
for y in range(0,len(histr)):
    elem = histr[y]
    if elem == histr.max():
     break
else:
    y = none
G=y

histr = cv2.calcHist([b],[0],None,[256],[0,256])
for y in range(0,len(histr)):
    elem = histr[y]
    if elem == histr.max():
     break
else:
    y = none
B=y
color = (R, G, B)

#add borders around the image colorized as background. This will allow me to find closed contour around area with data.
bordersize=100
new_xsize = xsize + bordersize*2
new_ysize = ysize + bordersize*2
#image_border.show()
image_border=cv2.copyMakeBorder(image, top=bordersize, bottom=bordersize, left=bordersize, right=bordersize, borderType= cv2.BORDER_CONSTANT, value=[R,G,B] )

#ysizeb, xsizeb, channelsb = image_border.shape

# get a blank canvas for drawing contour on and convert image to grayscale
canvas = np.zeros(image_border.shape, np.uint8)
#imgc = cv2.medianBlur(img,21)
img2gray = cv2.cvtColor(image_border,cv2.COLOR_BGR2GRAY)

# filter out country borders
kernel = np.ones((5,5),np.float32)/25
img2gray = cv2.filter2D(img2gray,-1,kernel)

# threshold the image and extract contours
thresh = cv2.adaptiveThreshold(img2gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,11)
contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
plt.subplot(111),plt.imshow(thresh,'gray')
plt.show()
# find the biggest area
cnt = contours[0]

max_area = cv2.contourArea(cnt)

for cont in contours:
    if cv2.contourArea(cont) > max_area:
        cnt = cont
        max_area = cv2.contourArea(cont)
perimeter = cv2.arcLength(cnt,True)
epsilon = 0.01*cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)

hull = cv2.convexHull(cnt)

# cv2.isContourConvex(cnt)
cv2.drawContours(canvas, cnt, -1, (0, 255, 0), 3)
cv2.drawContours(canvas, approx, -1, (0, 0, 255), 3)
#cv2.drawContours(canvas, [hull], -1, (0, 0, 255), 3) 

cv2.imshow("Contour", canvas)
k = cv2.waitKey(0)

if k == 27:         # wait for ESC key to exit
    cv2.destroyAllWindows()

0 个答案:

没有答案