Commit 512c9686 authored by josh's avatar josh
Browse files

rearranged dirs

parent e481058a
......@@ -14,24 +14,24 @@ Note: if the slice size doesn't fit evenly into the size of
## Example run:
```bash
python sliceImage.py ./sliceImageResults/GeoEye_Original.jpg GeoEye_Slice 512 512 ./sliceImageResults/
python ./Scripts/sliceImage.py ./TestResults/Slice/GeoEye_Original.jpg GeoEye_Slice 512 512 ./TestResults/Slice/
```
## Example run result:
## Original Image: 1180x1180
![](sliceImageResults/GeoEye_Original.jpg)
![](TestResults/Slice/GeoEye_Original.jpg)
## Slice 1: 512x512
![](sliceImageResults/GeoEye_Slice-0-0.jpg)
![](TestResults/Slice/GeoEye_Slice-0-0.jpg)
## Slice 2: 512x512
![](sliceImageResults/GeoEye_Slice-0-512.jpg)
![](TestResults/Slice/GeoEye_Slice-0-512.jpg)
## Slice 3: 512x512
![](sliceImageResults/GeoEye_Slice-512-0.jpg)
![](TestResults/Slice/GeoEye_Slice-512-0.jpg)
## Slice 4: 512x512
![](sliceImageResults/GeoEye_Slice-512-512.jpg)
![](TestResults/Slice/GeoEye_Slice-512-512.jpg)
## "meanShiftSeg.py" program:
......@@ -48,18 +48,18 @@ python sliceImage.py ./sliceImageResults/GeoEye_Original.jpg GeoEye_Slice 512 51
## Example run:
```bash
python meanShiftSeg.py ./meanShiftResults/GeoEye_MS_Original.jpg ./meanShiftResults/ pyms 6 6 50
python ./Scripts/meanShiftSeg.py ./TestResults/MS/GeoEye_MS_Original.jpg ./TestResults/MS pyms 6 6 50
```
## Example run result:
## Original Image:
![](meanShiftResults/GeoEye_MS_Original.jpg)
## Example run result:
## Original Image:
![](TestResults/MS/GeoEye_MS_Original.jpg)
## Segmentation Image:
![](meanShiftResults/pyms-color-seg.jpg)
## Segmentation Image:
![](TestResults/MS/pyms-color-seg.jpg)
## Labels Image:
![](meanShiftResults/pyms-labels-image.jpg)
## Labels Image:
![](TestResults/MS/pyms-labels-image.jpg)
## "meanShift3Channel.py" program:
......@@ -71,15 +71,15 @@ Uses the mean shift algorithm to create a segmentation mask for an image.
## Example run:
```bash
python meanShift3Channel.py meanShiftResults/GeoEye_MS_Original.jpg meanShiftResults/GeoEye_MS_Segmented
python ./Scripts/meanShift3Channel.py ./TestResults/MS/GeoEye_MS_Original.jpg ./TestResults/MS/three-channel-seg
```
## Example run result:
## Original Image:
![](meanShiftResults/GeoEye_MS_Original.jpg)
![](TestResults/MS/GeoEye_MS_Original.jpg)
## Segmentation Image:
![](meanShiftResults/three_channel_seg.jpg)
![](TestResults/MS/three-channel-seg.jpg)
## Program run time:
This program takes roughly 20 seconds to run, tested on an AMD FX(tm)-4300 Quad-Core Processor
......
......@@ -10,28 +10,27 @@ from matplotlib.colors import to_rgb
from sklearn.cluster import MeanShift, estimate_bandwidth
from skimage import color
def sliceImage( imagePath: str, newSize: tuple ) -> dict:
def sliceImage( imgArr: np.array, newSize: tuple ) -> dict:
'''
Creates slices of an image.
Parameters:
- imagePath : path to image used for slicing
- newSize : width and height of new slices
Arguments:
- imgArr : image array used for slicing
- newSize : width and height of new slices
returns: dictionary containing the following information
{
"slicedImages": [ ( xStartPos, yStartPos, imgArr ) ],
"slicedImages": [ ( xStartPos, yStartPos, slicedImgArr ) ],
"pixelsLost" : int,
"sliceCount" : int
}
OR empty dictionary if slicing was unable to be completed
'''
#image = cv2.imread( imagePath )
image = cv2.imread( imagePath )
width = image.shape[0]
height = image.shape[1]
width = imgArr.shape[0]
height = imgArr.shape[1]
newWidth = newSize[0]
newHeight = newSize[1]
......@@ -52,14 +51,84 @@ def sliceImage( imagePath: str, newSize: tuple ) -> dict:
for i in range( 0, maxWidth, newWidth ):
for j in range( 0, maxHeight, newHeight ):
newSliceCount += 1
imageList.append( ( i, j, image[i:i+newWidth, j:j+newHeight, :] ) )
imageList.append( ( i, j, imgArr[i:i+newWidth, j:j+newHeight, :] ) )
return {
"slicedImages": imageList,
"pixelsLost" : pixelsLost,
"sliceCount" : newSliceCount
}
def meanShiftSegmentation( imgArr: np.array, spatialRadius: int=6, rangeRadius: int=6, minDensity: int=50 ):
'''
SOURCE - https://github.com/fjean/pymeanshift
Arguments:
- imgArr : image array to use for algo
- spatialRadius: spatial radius for ms algo
- rangeRadius : range radius for ms algo
- minDensity : min density for ms algo
return: ( segmented_image, labels_image, number_regions )
'''
return pms.segment( imgArr,
spatial_radius=spatialRadius,
range_radius=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( baseDir: str, name: str ) -> bool:
'''
Initialized the image database at a given directory
File Struture:
|-- ImageDatabase
│   |-- MeanShift
│   |-- Original
│   |-- Sliced
'''
baseDir = pathlib.Path( baseDir )
dbDir = baseDir.joinpath( name )
if dbDir.is_dir():
print( "Failed to init: a database with this name already exists." )
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 )
'''
OTHER IMPLEMENTATIONS
'''
def meanshift3Channel( imagePath: str, outPath: str, quantile=0.2, samples=500, classColors: list=None ) -> dict:
'''
......@@ -68,7 +137,7 @@ def meanshift3Channel( imagePath: str, outPath: str, quantile=0.2, samples=500,
Creates and saves a segmented version of image using the mean shift algorithm to determine pixel classes.
Supports 3 channel images, each color channel is used as a feature.
Parameters:
Arguments:
- imagePath : path to image used
- outPath : path to save segmented image ( image type extension automatically added )
- quantile : used for estimate_bandwidth function, should be between [0, 1], default: 0.2
......@@ -135,68 +204,6 @@ def meanshift3Channel( imagePath: str, outPath: str, quantile=0.2, samples=500,
1: ( classColors[1], color2 ),
2: ( classColors[2], color3 )
}
def meanShiftSegmentation( imagePath: str, outPath: str, spatialRadius: int=6, rangeRadius: int=6, minDensity: int=50 ):
'''
SOURCE - https://github.com/fjean/pymeanshift
return: ( segmented_image, labels_image, number_regions )
'''
original_image = cv2.imread( imagePath )
return pms.segment( original_image,
spatial_radius=spatialRadius,
range_radius=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( baseDir: str, name: str ) -> bool:
'''
Initialized the image database at a given directory
File Struture:
|-- ImageDatabase
│   |-- MeanShift
│   |-- Original
│   |-- Sliced
'''
baseDir = pathlib.Path( baseDir )
dbDir = baseDir.joinpath( name )
if dbDir.is_dir():
print( "Failed to init: a database with this name already exists." )
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 )
if __name__ == "__main__":
baseDir = "../"
......
......@@ -15,7 +15,6 @@ if __name__ == "__main__":
- arg6: min density for ms algo
'''
if len( sys.argv ) == 7:
imagePath = sys.argv[1]
outPath = pathlib.Path( sys.argv[2] )
......@@ -24,18 +23,19 @@ if __name__ == "__main__":
rangeRadius = int( sys.argv[5] )
minDensity = int( sys.argv[6] )
( segmentedImage, labelsImage, numberRegions ) = meanShiftSegmentation( imagePath=imagePath,
outPath=outPath,
# run algorithm
imgArr = cv2.imread( imagePath )
( segmentedImage, labelsImage, numberRegions ) = meanShiftSegmentation( imgArr=imgArr,
spatialRadius=spatialRadius,
rangeRadius=rangeRadius,
minDensity=minDensity )
# create save file paths
fileExt = pathlib.Path( imagePath ).name.split( "." )[-1]
segColorPath = outPath.joinpath( "{}-color-seg.{}".format( baseFileName, fileExt ) )
labelImgPath = outPath.joinpath( "{}-labels-image.{}".format( baseFileName, fileExt ) )
# scale labelsImage to [0,255]
print( labelsImage.max() )
labelsImage = labelsImage.astype( float )
labelsImage /= labelsImage.max()
labelsImage *= 255
......
......@@ -21,18 +21,19 @@ if __name__ == "__main__":
newPath = sys.argv[5]
# get sliced images
result = sliceImage( imagePath, newSize )
imgArr = cv2.imread( imagePath )
result = sliceImage( imgArr, newSize )
fileExt = pathlib.Path( imagePath ).name.split( "." )[-1]
newPath = pathlib.Path( newPath )
# save each image to DB
for res in result["slicedImages"]:
i = res[0]
j = res[1]
imgArr = res[2]
i = res[0]
j = res[1]
slicedImg = res[2]
slicePath = newPath.joinpath( "{}-{}-{}.{}".format( newFileBase, i, j, fileExt ) )
cv2.imwrite( str( slicePath ), imgArr )
cv2.imwrite( str( slicePath ), slicedImg )
else:
print( "Invalid arg count" )
......
This diff is collapsed.
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