【问题标题】:How do I crop the solar panels captured by drone?如何裁剪无人机捕获的太阳能电池板?
【发布时间】:2018-12-27 03:07:46
【问题描述】:

我目前正在从无人机拍摄的图像中裁剪太阳能电池板(附上示例图像)。我曾尝试使用轮廓,但没有正确的结果。它没有检测到图像中的所有太阳能电池板,其中一些太阳能电池板丢失了。我自己击中了这里。我该如何进一步进行?请帮我解决这个问题。

谢谢,

示例代码:

import cv2
import numpy as np
img = cv2.imread('D:\\SolarPanel Images\\solarpanel.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
edges = cv2.Canny(blur,100,200)    
th3 = cv2.adaptiveThreshold(edges,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)

im2, contours, hierarchy = cv2.findContours(th3, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print("Len of contours",len(contours)
try: hierarchy = hierarchy[0]
except: hierarchy = []

height, width,  = edges.shape
min_x, min_y = width, height
max_x = max_y = 0

# computes the bounding box for the contour, and draws it on the image,
for contour, hier in zip(contours, hierarchy):
    area = cv2.contourArea(contour)

    if area > 10000 and area < 250000:
        (x,y,w,h) = cv2.boundingRect(contour)
        min_x, max_x = min(x, min_x), max(x+w, max_x)
        min_y, max_y = min(y, min_y), max(y+h, max_y)
        if w > 80 and h > 80:
            cv2.rectangle(img, (x,y), (x+w,y+h), (255, 0, 0), 2)

            cv2.imshow('cont imge', img)
            cv2.waitKey(0)

【问题讨论】:

    标签: python-3.x opencv image-processing machine-learning computer-vision


    【解决方案1】:

    要在重要对象与背景明显区分开来的图像中找到轮廓,您始终可以尝试将图像转换为 HSV 格式,然后再转换为轮廓。我做了以下事情:

    import cv2
    import numpy as np
    img = cv2.imread('panel.jpg')
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 
    ret,thresh1 = cv2.threshold(hsv[:,:,0],100,255,cv2.THRESH_BINARY)
    im2, contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, 
    cv2.CHAIN_APPROX_SIMPLE)
    try: hierarchy = hierarchy[0]
    except: hierarchy = []
    for contour, hier in zip(contours, hierarchy):
        area = cv2.contourArea(contour)
        if area > 10000 and area < 250000:
           rect = cv2.minAreaRect(contour)
           box = cv2.boxPoints(rect)
           box = np.int0(box)
           cv2.drawContours(img,[box],0,(0,0,255),2)
           cv2.imshow('cont imge', img)
           cv2.waitKey(0)
    cv2.imwrite("result.jpg",img)
    

    结果:

    【讨论】:

    • 感谢您的帮助,但我想裁剪这些检测到的轮廓。我将如何进行?
    • 首先您必须将图像旋转到边界矩形,然后对其进行裁剪。 stackoverflow.com/questions/37177811/…
    • 非常感谢@janu777 最后,我裁剪了它们,但我还必须从裁剪后的图像中裁剪每个面板。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 2012-08-15
    相关资源
    最近更新 更多