如何使用 rgb 差异从航拍图像中获得屋顶的表示?

how can i obtain a representation of the roofs from a aerial image using rgb differences?

所以我在 python 学习图像处理,我遇到了一个我很难解决的练习。它给出了航拍图:

Aerial Image

objective 是将一张图像中的所有屋顶个性化,将其余部分(背景)保留为黑色。练习建议使用 rgb 波段之间的差异,然后应用阈值方法,该方法使用距离连接第一个非零频率索引和直方图显着峰值的线(最大距离法)的距离更大的对应点。

该练习还显示了最终结果的示例:

Roofs

这是我到目前为止尝试过的方法:

from imageio import imread
import numpy as np
Imagem2 = imread("ik02.tif")
r2 = Imagem2[:,:,0] 
g2 = Imagem2[:,:,1] 
b2 = Imagem2[:,:,2] 
r_b = r2-b2 
rbh, rb = np.histogram(r_b, bins=256, range=(0, 256)) 

从直方图的观察可以区分两个暗峰,大约 0 个表示道路,3 个表示房屋?也许从房子的下方和上方“削减”价值?

(红带 - 蓝带)操作给了我一个很好的结果,我只是不知道如何个性化房屋。结果如下:

(Red band - Blue band)

感谢任何帮助!

您尝试做的与肤色检测器相同:

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

# Read image
img = cv2.imread('roofs.jpg')
converted = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# Reference: https://www.pyimagesearch.com/2014/08/18/skin-detection-step-step-example-using-python-opencv/
# define the upper and lower boundaries of the HSV pixel
# intensities to be considered 'skin'
lower = np.array([0, 48, 80], dtype = "uint8")
upper = np.array([12, 255, 255], dtype = "uint8")

skinMask = cv2.inRange(converted, lower, upper)
# apply a series of erosions and dilations to the mask
# using an elliptical kernel
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7, 7))
skinMask = cv2.morphologyEx(skinMask, cv2.MORPH_CLOSE, kernel, iterations = 1)
# blur the mask to help remove noise, then apply the
# mask to the img
skinMask = cv2.GaussianBlur(skinMask, (5, 5), 0)
skin = cv2.bitwise_and(img, img, mask = skinMask)
# show the skin in the image along with the mask
cv2.imshow("images", np.hstack([img, skin]))
# waits for user to press any key 
# (this is necessary to avoid Python kernel form crashing) 
cv2.waitKey(0) 
  
# closing all open windows 
cv2.destroyAllWindows()