I tried to count buildings from gmaps datasets.
Here's an example of dataset :
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 :
The result using combined mask:
How do I get the correct result the number of building from this?