Commit f4e22af1 authored by josh's avatar josh
Browse files

changing db structure

parent 2eb9ef26
from ai4hdr_utils import *
'''
Database file structure example:
|-- ImageDatabase
│   |-- MeanShift
| | |--image_name-id
| | | |--128x128
| | | | |--image_name-128-smap-0-0.<ext> <- segmented map
| | | | |--image_name-128-simage-0-0.<ext> <- segmented image
| | | |--256x256
| | | |--512x512
│   |-- Original
| | |--image_name-id.<ext> <- original image, id = auto increment integer
│   |-- Sliced
| | |--image_name-id
| | | |--128x128
| | | | |--image_name-128-0-0.<ext> <- sliced image
| | | |--256x256
| | | |--512x512
'''
def initDB( dbLoc: str, name: str ) -> bool:
'''
Initialized the image database at a given directory
File Struture:
|-- ImageDatabase
│   |-- MeanShift
│   |-- Original
│   |-- Sliced
'''
dbLoc = pathlib.Path( dbLoc )
dbDir = dbLoc.joinpath( name )
if dbDir.is_dir():
return False
else:
originalImagesDir = dbDir.joinpath( "Original" )
slicedImagesDir = dbDir.joinpath( "Sliced" )
meanShiftSegDir = dbDir.joinpath( "MeanShift" )
os.mkdir( dbDir )
os.mkdir( originalImagesDir )
os.mkdir( slicedImagesDir )
os.mkdir( meanShiftSegDir )
return True
def importAndProcess( imgPath: str, dbPath: str, newImgName: str ):
'''
Returns: Name used to query DB for image
'''
imgPath = pathlib.Path( imgPath ).absolute()
dbPath = pathlib.Path( dbPath ).absolute()
# Ensure database exists
if not dbPath.is_dir():
print( "ERROR: No database exists at {}".format( dbPath ) )
return False
# Internal database directories
originalPath = dbPath.joinpath( "Original" )
meanShiftPath = dbPath.joinpath( "MeanShift" )
slicedPath = dbPath.joinpath( "Sliced" )
# Ensure database was initialized correctly
if not originalPath.is_dir() or not meanShiftPath.is_dir() or not slicedPath.is_dir():
print( "ERROR - Database Not Initialized: Use 'initDB.py' to init database.")
return False
# PROCESS IMAGE
# Step 1: Read in image
imgArr = cv2.imread( str( imgPath ) )
fileExt = imgPath.name.split( "." )[-1]
# Auto increment id
dbID = len( [f for f in originalPath.iterdir() ] )
# Step 2: Save to Original, create MS, Sliced dirs
originalFileName = originalPath.joinpath( "{}-{}.{}".format( newImgName, dbID, fileExt ) )
cv2.imwrite( str( originalFileName ), imgArr )
imgSlicedPath = slicedPath.joinpath( "{}-{}".format( newImgName, dbID ) )
imgMSPath = meanShiftPath.joinpath( "{}-{}".format( newImgName, dbID ) )
# Run processing
processedResult = sliceAndMeanShift( imgArr )
# Create slice and meanshift dirs
os.mkdir( imgSlicedPath )
os.mkdir( imgMSPath )
# Save Slices
sizes = [ "128", "256", "512" ]
print( "|--------------- SLICES ---------------|" )
for size in sizes:
imgSlicedSizePath = imgSlicedPath.joinpath( str( size ) )
os.mkdir( imgSlicedSizePath )
for imgRes in processedResult["slices"][size]:
i = imgRes[0]
j = imgRes[1]
slicedImg = imgRes[2]
finalFileName = "{}-{}-{}".format( newImgName, i, j )
sliceSizePath = imgSlicedSizePath.joinpath( "{}.{}".format( finalFileName, fileExt ) )
print( "SAVING IMAGE:", sliceSizePath )
cv2.imwrite( str( sliceSizePath ), slicedImg )
print( "|--------------- MEAN SHIFT ---------------|" )
# Save Mean Shift Results
for size in sizes:
imgMSSizePath = imgMSPath.joinpath( str( size ) )
os.mkdir( imgMSSizePath )
for imgRes in processedResult["meanShift"][size]:
i = imgRes[0]
j = imgRes[1]
numRegions = imgRes[2]
segImage = imgRes[3]
labelImage = imgRes[4]
finalFileName = "{}-{}-{}".format( newImgName, i, j )
segColorPath = imgMSSizePath.joinpath( "{}-color-seg.{}".format( finalFileName, fileExt ) )
labelImgPath = imgMSSizePath.joinpath( "{}-labels-image".format( finalFileName ) )
print( "SAVING IMAGE:", segColorPath )
print( "SAVING IMAGE:", labelImgPath )
## scale labelsImage to [0,255]
#labelsImage = labelsImage.astype( float )
#labelsImage /= labelsImage.max()
#labelsImage *= 255
## save results to image files
cv2.imwrite( str( segColorPath ), segImage )
#cv2.imwrite( str( labelImgPath ), labelImage )
np.save( str( labelImgPath ), labelImage )
\ No newline at end of file
......@@ -76,52 +76,6 @@ def meanShiftSegmentation( imgArr: np.array, spatialRadius: int=6, rangeRadius:
min_density=minDensity )
'''
Database functions
Database file structure example:
|-- ImageDatabase
│   |-- MeanShift
| | |--image_name-id
| | | |--128x128
| | | | |--image_name-128-smap-0-0.<ext> <- segmented map
| | | | |--image_name-128-simage-0-0.<ext> <- segmented image
| | | |--256x256
| | | |--512x512
│   |-- Original
| | |--image_name-id.<ext> <- original image, id = auto increment integer
│   |-- Sliced
| | |--image_name-id
| | | |--128x128
| | | | |--image_name-128-0-0.<ext> <- sliced image
| | | |--256x256
| | | |--512x512
'''
def initDB( dbLoc: str, name: str ) -> bool:
'''
Initialized the image database at a given directory
File Struture:
|-- ImageDatabase
│   |-- MeanShift
│   |-- Original
│   |-- Sliced
'''
dbLoc = pathlib.Path( dbLoc )
dbDir = dbLoc.joinpath( name )
if dbDir.is_dir():
return False
else:
originalImagesDir = dbDir.joinpath( "Original" )
slicedImagesDir = dbDir.joinpath( "Sliced" )
meanShiftSegDir = dbDir.joinpath( "MeanShift" )
os.mkdir( dbDir )
os.mkdir( originalImagesDir )
os.mkdir( slicedImagesDir )
os.mkdir( meanShiftSegDir )
return True
# TO DO::POTENTIAL PROBLEM - this function holds all the results in memory, may be too much for large images
......@@ -180,7 +134,7 @@ def sliceAndMeanShift( imgArr: np.array ):
for res in sliceResults["slicedImages"]:
i = res[0]
j = res[1]
slicedImg = res[2]
slicedImg = res[2].astype( np.uint8 )
#print( slicedImg.shape )
# mean shift
( segmentedImage, labelsImage, numberRegions ) = meanShiftSegmentation( slicedImg )
......@@ -190,94 +144,6 @@ def sliceAndMeanShift( imgArr: np.array ):
return finalResults
def importAndProcess( imgPath: str, dbPath: str, newImgName: str ):
'''
Returns: Name used to query DB for image
'''
imgPath = pathlib.Path( imgPath ).absolute()
dbPath = pathlib.Path( dbPath ).absolute()
# Ensure database exists
if not dbPath.is_dir():
print( "ERROR: No database exists at {}".format( dbPath ) )
return False
# Internal database directories
originalPath = dbPath.joinpath( "Original" )
meanShiftPath = dbPath.joinpath( "MeanShift" )
slicedPath = dbPath.joinpath( "Sliced" )
# Ensure database was initialized correctly
if not originalPath.is_dir() or not meanShiftPath.is_dir() or not slicedPath.is_dir():
print( "ERROR - Database Not Initialized: Use 'initDB.py' to init database.")
return False
# PROCESS IMAGE
# Step 1: Read in image
imgArr = cv2.imread( str( imgPath ) )
fileExt = imgPath.name.split( "." )[-1]
# Auto increment id
dbID = len( [f for f in originalPath.iterdir() ] )
# Step 2: Save to Original, create MS, Sliced dirs
originalFileName = originalPath.joinpath( "{}-{}.{}".format( newImgName, dbID, fileExt ) )
cv2.imwrite( str( originalFileName ), imgArr )
imgSlicedPath = slicedPath.joinpath( "{}-{}".format( newImgName, dbID ) )
imgMSPath = meanShiftPath.joinpath( "{}-{}".format( newImgName, dbID ) )
# Run processing
processedResult = sliceAndMeanShift( imgArr )
# Create slice and meanshift dirs
os.mkdir( imgSlicedPath )
os.mkdir( imgMSPath )
# Save Slices
sizes = [ "128", "256", "512" ]
print( "|--------------- SLICES ---------------|" )
for size in sizes:
imgSlicedSizePath = imgSlicedPath.joinpath( str( size ) )
os.mkdir( imgSlicedSizePath )
for imgRes in processedResult["slices"][size]:
i = imgRes[0]
j = imgRes[1]
slicedImg = imgRes[2]
finalFileName = "{}-{}-{}".format( newImgName, i, j )
sliceSizePath = imgSlicedSizePath.joinpath( "{}.{}".format( finalFileName, fileExt ) )
print( "SAVING IMAGE:", sliceSizePath )
cv2.imwrite( str( sliceSizePath ), slicedImg )
print( "|--------------- MEAN SHIFT ---------------|" )
# Save Mean Shift Results
for size in sizes:
imgMSSizePath = imgMSPath.joinpath( str( size ) )
os.mkdir( imgMSSizePath )
for imgRes in processedResult["meanShift"][size]:
i = imgRes[0]
j = imgRes[1]
numRegions = imgRes[2]
segImage = imgRes[3]
labelImage = imgRes[4]
finalFileName = "{}-{}-{}".format( newImgName, i, j )
segColorPath = imgMSSizePath.joinpath( "{}-color-seg.{}".format( finalFileName, fileExt ) )
labelImgPath = imgMSSizePath.joinpath( "{}-labels-image.{}".format( finalFileName, fileExt ) )
print( "SAVING IMAGE:", segColorPath )
print( "SAVING IMAGE:", labelImgPath )
## scale labelsImage to [0,255]
#labelsImage = labelsImage.astype( float )
#labelsImage /= labelsImage.max()
#labelsImage *= 255
## save results to image files
cv2.imwrite( str( segColorPath ), segImage )
cv2.imwrite( str( labelImgPath ), labelImage )
......
Markdown is supported
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