Commit 17f1de66 authored by Caillat Michel's avatar Caillat Michel
Browse files

Fixed the bug in the contour measurements

parent e39039b0
......@@ -1100,9 +1100,9 @@ class DataBlock:
iRA1 = iRA1 if iRA1 else (self.__data.shape[2] if numDims > 2 else self.__data.shape[1])
# Get the data to be contoured
slice = self.__getSlice(iFREQ)[iDEC0:iDEC1, iRA0:iRA1]
self.__logger.debug("slice shape %r", slice.shape)
#self.__logger.debug("%r,%r" % (da.min(slice[!da.isnan(slice)]).compute(), da.max(slice[!da.isnan(slice)]).compute()))
slice = self.__getSlice(iFREQ).compute()
shape = slice.shape
self.__logger.debug("slice shape %r", shape)
# Determine the contours levels.
levels = None
......@@ -1112,31 +1112,32 @@ class DataBlock:
if key == 'levels':
levels = value
elif key == 'quantiles':
levels = [np.quantile(slice[~da.isnan(slice)].compute(), quantile) for quantile in value]
levels = [np.quantile(slice[~np.isnan(slice)], quantile) for quantile in value]
elif key == 'numberOfBins':
levels = np.histogram_bin_edges(slice[~da.isnan(slice)].compute(), value)
levels = np.histogram_bin_edges(slice[~np.isnan(slice)], value)
elif key == 'histBinsMethod':
levels = np.histogram_bin_edges(slice[~da.isnan(slice)].compute(), value)
levels = np.histogram_bin_edges(slice[~np.isnan(slice)], value)
levels = levels [-10:]
self.__logger.debug("levels = %r" % (levels))
# Produce the contours.
features = []
fig, ax = pp.subplots()
#CS = ax.contour(np.arange(slice.shape[1]), np.arange(slice.shape[0]), slice, levels)
CS = ax.contour(slice, levels)
for level, set in zip(CS.levels, CS.allsegs):
for c in set :
for level in levels:
slice_bw = np.zeros(shape, dtype=np.uint8)
slice_bw[slice >= level] = 127
contours, _ = cv2.findContours(slice_bw, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours :
pts = [x[0] for x in contour.tolist()]
#pts = [[x[0], shape[0] - x[1] -1] for x in pts]
feature = {"type":"LineString", \
"coordinates": [[round(x[0]), round(x[1])] for x in c.tolist()],\
"coordinates": pts,\
"properties": {"level" : level}}
features.append(feature)
contours_geojson = { "type": "FeatureCollection", "features" : features }
result = {"status" : True, "message" : "", "result": contours_geojson}
self.__logger.debug(contours_geojson)
self.__logger.debug("getContours: exiting")
return result
......@@ -1144,24 +1145,25 @@ class DataBlock:
self.__logger.debug("measureContour : entering")
slice = self.__getSlice(iFREQ).compute()
shape = slice.shape
theMask = np.empty(shape, dtype=bool)
bdr = cv2.boundingRect(contour)
numpix = 0
x,y,w,h = cv2.boundingRect(contour)
x,y,w,h = bdr
accum = []
for j in np.arange(y, y+h):
for i in np.arange(x, x+w):
theMask[j][i] = cv2.pointPolygonTest(contour, (i, j), False) != -1 and slice[j][i] >= level
if theMask[j][i] :
numpix += 1
if cv2.pointPolygonTest(contour, (i, j), False) != -1 and slice[j][i] >= level:
accum.append(slice[j,i])
accum = np.asarray(accum)
result = {}
result["sum"] = "%11.4g" % (np.nansum(slice[theMask]).item() / self.__convert * self.__cdelt);
result["min"] = "%11.4g" % (np.nanmin(slice[theMask]).item())
result["max"] = "%11.4g" % (np.nanmax(slice[theMask]).item())
result["mean"] = "%11.4g" % (np.nanmean(slice[theMask]).item())
result["var"] = "%11.4g" % (np.nanvar(slice[theMask]).item())
result["numpix"] = numpix
result["percent"] = "%9.2g" % (numpix / (shape[0]*shape[1]) * 100)
result["boundingRect"] = cv2.boundingRect(contour)
result["sum"] = "%11.4g" % (np.sum(accum) / self.__convert * self.__cdelt);
result["min"] = "%11.4g" % (np.min(accum))
result["max"] = "%11.4g" % (np.max(accum))
result["mean"] = "%11.4g" % (np.mean(accum))
result["stdev"] = "%11.4g" % (np.std(accum))
result["numpix"] = accum.shape[0]
result["percentage of total number of pixels"] = "%9.2g%%" % (accum.shape[0] / (shape[0]*shape[1]) * 100)
result["boundingRect"] = bdr
result = {"status":True, "message":"", "result":result}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment