0

I tried to count buildings from gmaps datasets.

Here's an example of dataset :

enter image description here

I tried to count them using cv2.connectedComponents()

Here's the preprocessing code :

#2 colors
img = cv2.imread("gmap.jpg")

# convert image to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# lower = np.array([4, 0, 7])
#upper = np.array([87, 240, 255])

# define color ranges
low_yellow = (0,0,0)
high_yellow = (240,251,255)


low_gray = (0,0,0)
high_gray = (245,243,242)

# create masks
yellow_mask = cv2.inRange(img, low_yellow, high_yellow )
gray_mask = cv2.inRange(img, low_gray, high_gray)

# combine masks
combined_mask = cv2.bitwise_or(yellow_mask, gray_mask)
plt.imshow(combined_mask)

And then, the other method, I tried to preprocess them using adaptive threshold method :

img = cv2.imread('gmap.jpg',0)
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
            cv2.THRESH_BINARY,11,2)
plt.imshow(th2, 'gray')

This is how I count them :

ret, labels = cv2.connectedComponents(th2)
label_hue = np.uint8(179 * labels / np.max(labels))
blank_ch = 255 * np.ones_like(label_hue)
labeled_img = cv2.merge([label_hue, blank_ch, blank_ch])
labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)
labeled_img[label_hue == 0] = 0
plt.subplot(222)
plt.title('Objects counted:'+ str(ret-1))
plt.imshow(labeled_img)
print('objects number is:', ret-1)
plt.show()

This is the result :

The result using adaptive threshold :

enter image description here

The result using combined mask:

enter image description here

How do I get the correct result the number of building from this?

4
  • If you don't know the number of buildings in a particular image then try counting the number of buildings within the image manually or else use that target value and adjust the parameters accordingly to get some value closer to the counted number of buildings. Then try and see if it generalizes well for different images. Commented Nov 4, 2021 at 4:34
  • I already done it. What you mean by adjust the parameters? I tried to adjust the lower and upper range colors, but still didn't give the correct result. Can you be more specific about the method and what parameters? Commented Nov 4, 2021 at 4:40
  • it's a map. all colors are fixed. don't use an adaptive threshold. use a fixed threshold. -- you ask for "correct", you need to say what you consider correct. and don't post tiny thumbnails of a plot where one can't see anything. post pictures where stuff can be made out. -- the two inranges and the connectedComponents are probably the best you can do, and this should produce an accurate result, so I don't see what more you could want. Commented Nov 4, 2021 at 9:35
  • if this were produce the accurate result, why do I bother ask here. I think a 5 year old know what I mean by 'correct' number of building. If you don't provide solution, why bother to comment only to point out what I know Commented Nov 5, 2021 at 3:54

1 Answer 1

0

A method could be blob detection.

opencv provides a good range of tools for that. After not really a quick search (You need to check your opencv's version a lot) I found this example from fmw42

Threshold

Blobs

Using cnts you might be able to find your building count.

Please notice this example is not optimized and is not working on your example. The example is here only to show that blob detection can be a solution. Changing some parameters, adjusting for shapes (A very long shape can be a road and not building) can output correct results.

opencv version: 4.5.3
Sign up to request clarification or add additional context in comments.

1 Comment

I think that my solution above is better that the one that you're suggested

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.