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

2024-05-09 08:21:24 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我正在学习python中的图像处理,我遇到了一个我需要努力解决的练习。这是一张航空影像:

Aerial Image

其目标是在一幅图像中对所有屋顶进行个性化处理,将其其余部分(背景)保留为黑色。该练习建议使用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)

谢谢你的帮助


Tags: 方法import距离航空bandnp直方图b2
1条回答
网友
1楼 · 发布于 2024-05-09 08:21:24

您尝试执行的操作与肤色检测器相同:

Result

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() 

相关问题 更多 >