Reputation: 404
I'm an R user having trouble with image analysis in Python. What's an efficient way to compute the area of a building at the center in the image? The goal is to apply an edge algorithm to a Google Maps static image and compute the surface area of the rooftop of an address.
from pygeocoder import Geocoder
import urllib
import numpy as np
from scipy import ndimage
from skimage import filter, io, measure
import matplotlib.pyplot as plt
def getMap(address):
"""Geocode address and retreive image centered
around lat/long"""
results = Geocoder.geocode(address)
lat, lng = results[0].coordinates
zip_code = results[0].postal_code
map_url = 'https://maps.googleapis.com/maps/api/staticmap?center={0},{1}&size=640x640&zoom=19&sensor=false&maptype=roadmap&&style=visibility:simplified|gamma:0.1'
request_url = map_url.format(lat, lng)
req = urllib.urlopen(request_url)
return(req)
def mapEdge(req):
"""Convert img to bytearray and do edge detection
on centered building"""
img = io.imread(req.geturl(),flatten=True)
labels, numobjects = ndimage.label(img)
edges = filter.canny(img, sigma=3)
plt.imshow(edges, cmap=plt.cm.gray)
plt.show()
map_tmp = getMap('1403 Elmwood Ave., Evanston, IL')
mapEdge(map_tmp)
Upvotes: 0
Views: 1909
Reputation: 81
One way to proceed would be to use contour finding technique available in opencv and then detect the center of the contour. The code is in c++ but can be very easily be converted into python
findContours( dst4, contours2, hierarchy2, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
vector<vector<Point> > contours_poly2( contours2.size() );
vector<Rect> boundRect2( contours2.size() );
cout << contours2.size()<<endl;
Mat drawing1=Mat::zeros(dst4.rows,dst4.cols,dst4.depth());
for( int i = 0; i < contours2.size(); i++ ){
approxPolyDP( Mat(contours2[i]), contours_poly2[i], 3, true );
boundRect2[i] = boundingRect( Mat(contours_poly2[i]) );
}
Now that you have a vector of bounding rectangle and thus if the center of the bounding rectangle is very close to the center of your image then its a positive match.
Here is a tutorial for contour finding using python http://opencvpython.blogspot.in/2012/06/hi-this-article-is-tutorial-which-try.html
Hope this helps
Upvotes: 1
Reputation: 5508
I would look at one of those libraries:
Upvotes: 0