1

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!

  • Maybe this is better suited for machine learning, teaching the algorithm which images have roofs and which do not. Some tools exist like GRIP which can ease the pain as well: https://docs.wpilib.org/en/stable/docs/software/vision-processing/grip/introduction-to-grip.html – Jairus Dec 21 '20 at 19:03
  • The exercise requires me to use the bands and maximum distance threshold method to solve it. – Jamie Jones Dec 21 '20 at 19:51
  • i dont have school mister. I'm learning by myself, if you have a problem with that why bother in the first place? It appears you dont know the answer and just like to judge. – Jamie Jones Dec 21 '20 at 20:28
  • As a member we are often tasked with reviewing, so not judging, just looking to help you edit your question so it can be answered more effectively. Can you please edit your post with your code, excersize, etc so there is more information which someone can use to answer your question. I will then remove down vote. – Jairus Dec 21 '20 at 20:31
  • Your more likely to get a response if you can narrow down the scope of your question. – Jairus Dec 21 '20 at 20:35
  • I tried to make it more appropriate. Sorry for the inconveniance. – Jamie Jones Dec 21 '20 at 20:51
  • No worries. Let's delete the irrelevant comments. – Jairus Dec 21 '20 at 20:54
  • By the way the grip tool can help you quickly visualize that. The you can just plug in the values. – Jairus Dec 21 '20 at 20:56

1 Answers1

2

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

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() 
Bilal
  • 3,191
  • 4
  • 21
  • 49