如何裁剪无人机拍摄的太阳能板?

我目前正在处理从无人机拍摄的图像中裁剪太阳能板(附件中有样本图像)。我尝试使用轮廓检测方法,但结果并不理想。有些太阳能板没有被检测到,我在这里遇到了困难。我该如何继续进行?请帮助我解决这个问题。

谢谢,

样本代码:

import cv2import numpy as npimg = 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.shapemin_x, min_y = width, heightmax_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)

enter image description here


回答:

要在图像中找到轮廓,特别是当重要对象与背景明显不同时,可以尝试将图像转换为HSV格式,然后进行轮廓检测。我做了以下操作:

import cv2import numpy as npimg = 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)

结果:enter image description here

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注