Commit c348b083 authored by josh's avatar josh
Browse files

decoupled image processing and saving

parent f72b0ca6
......@@ -5,6 +5,7 @@ import os
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import pymeanshift as pms
from pprint import pprint
from matplotlib.colors import to_rgb
from sklearn.cluster import MeanShift, estimate_bandwidth
......@@ -124,6 +125,72 @@ def initDB( dbLoc: str, name: str ) -> bool:
return True
# TO DO::POTENTIAL PROBLEM - this function holds all the results in memory, may be too much for large images
# consider option to do limited what is produced
def sliceAndMeanShift( imgArr: np.array ):
'''
Returns dictionary:
{
"slices":
{
"128": [ ( x, y, imgArr ) ], <- x, y are pixel start positions in original image
"256": ...,
"512":
},
"meanShift":
{
"128": [ ( x, y, numRegions, segImage, labelImage ) ],
"256": ...,
"512":
}
}
'''
finalResults = {
"slices":
{
"128": [],
"256": [],
"512": []
},
"meanShift":
{
"128": [],
"256": [],
"512": []
}
}
sizes = [ 128, 256, 512 ]
# Loop: 128, 256, 512
for size in sizes:
print( "Processing Size:", size )
if imgArr.shape[0] >= size and imgArr.shape[1] >= size:
# Step 4: Slice
sliceResults = sliceImage( imgArr, ( size, size ) )
# Step 5: Save slices
finalResults["slices"][str( size )] = sliceResults["slicedImages"]
# Step 6: Mean Shift on all slices
for res in sliceResults["slicedImages"]:
i = res[0]
j = res[1]
slicedImg = res[2]
#print( slicedImg.shape )
# mean shift
( segmentedImage, labelsImage, numberRegions ) = meanShiftSegmentation( slicedImg )
# add to results
finalResults["meanShift"][str( size )].append( ( i, j, numberRegions, segmentedImage, labelsImage ) )
return finalResults
def importAndProcess( imgPath: str, dbPath: str, newImgName: str ):
'''
Returns: Name used to query DB for image
......@@ -162,59 +229,56 @@ def importAndProcess( imgPath: str, dbPath: str, newImgName: str ):
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 )
sizes = [ 128, 256, 512 ]
# Loop: 128, 256, 512
for size in sizes:
print( "Processing Size:", size )
# Save Slices
sizes = [ "128", "256", "512" ]
# Step 3: Create Dirs - always create dir even if image wont generate slices of a size
# MeanShift/image_name-id/<size>
# Sliced/image_name-id/<size>
print( "|--------------- SLICES ---------------|" )
for size in sizes:
imgSlicedSizePath = imgSlicedPath.joinpath( str( size ) )
imgMSSizePath = imgMSPath.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 )
if imgArr.shape[0] >= size and imgArr.shape[1] >= size:
# Step 4: Slice
sliceResults = sliceImage( imgArr, ( size, size ) )
# Step 5: Save slices
for res in sliceResults["slicedImages"]:
i = res[0]
j = res[1]
slicedImg = res[2]
finalFileName = "{}-{}-{}".format( newImgName, i, j )
sliceSizePath = imgSlicedSizePath.joinpath( "{}.{}".format( finalFileName, fileExt ) )
print( "SAVING IMAGE:", sliceSizePath )
cv2.imwrite( str( sliceSizePath ), slicedImg )
# Step 6: Mean Shift
( segmentedImage, labelsImage, numberRegions ) = meanShiftSegmentation( slicedImg )
# Step 7: Save MS results
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 ), segmentedImage )
cv2.imwrite( str( labelImgPath ), labelsImage )
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 )
......
......@@ -36,9 +36,9 @@ if __name__ == "__main__":
labelImgPath = outPath.joinpath( "{}-labels-image.{}".format( baseFileName, fileExt ) )
# scale labelsImage to [0,255]
labelsImage = labelsImage.astype( float )
labelsImage /= labelsImage.max()
labelsImage *= 255
#labelsImage = labelsImage.astype( float )
#labelsImage /= labelsImage.max()
#labelsImage *= 255
# save results to image files
cv2.imwrite( str( segColorPath ), segmentedImage )
......
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