简体   繁体   中英

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

So i´m learning image processing in python and i came across an exercise on which i'm having strugle to solve. It's given an aerial image:

Aerial Image

The objective is to individualize all the roofs in one image leaving the rest of it (background) in a black color. The exercise suggests using the difference between the rgb bands and then apply a threshold method that uses the greater distance correspondent point from the line joining the first nonzero frequency index and the significant peak of the histogram (maximum distance method).

The exercise also shows an example of what should be the final result:

Roofs

Here's what i've tried so far:

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

From the observation of the histogram it is possible to distinguish two dark peaks, approximately 0 for roads and 3 for houses? Maybe "cut" the values below and up from the house?

(Red band - Blue band) operation gives me a good result to procceed with, i just don't know how to individualize the houses. Here´s the result:

(Red band - Blue band)

Appreciate any help!

What you are trying to do is the same as skin color detector:

结果

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM