In [11]:
# import the required packages
from swat import *
from pprint import pprint
import numpy as np
import matplotlib.pyplot as plt
import cv2
# define the function to display the processed image files.
def imageShow(session, casTable, imageId, nimages):
a = session.table.fetch(sastypes=False,sortby=[{'name':'_id_'}],table=casTable,to=nimages)
fig = plt.figure(figsize=(20, 20))
for i in range(nimages):
imageData = a['Fetch'].ix[ i][ imageId]
img_np = cv2.imdecode(np.fromstring( imageData, np.uint8),1)
fig.add_subplot(1,nimages,i+1)
plt.imshow(img_np)
img_np[:,:,[0,2]]=img_np[:,:,[2,0]]
plt.xticks([]), plt.yticks([])
In [9]:
# define the host machine and port for CAS connection: port is 5570 from Linux client and 8777 from Windows client.
hostmachine = 'my-viya-server.my-domain.com'
port = 8777
# authentication file on the client machine with user name and password (see the link above).
_authinfo = r"my-local-path\_authinfo"
# path on the Viya server where the image files to be processed are stored.
path_source_images = "my-host-path-for-sources"
path_reference_images = "my-host-path-for-references"
# set up a CAS session.
conn = CAS(hostmachine, port, authinfo = _authinfo)
# load CAS image action set for processing images.
conn.loadactionset('image')
Out[9]:
In [4]:
conn.image.loadImages(casout={'name':'inputTable', 'replace':True}, path= path_source_images)
conn.image.processimages(casout={'name':'inputTable_resized', 'replace':True},
imagefunctions=[{'functionoptions':{'width':1000,'functiontype':'RESIZE','height':600}}],
imagetable={'name':'inputTable'})
imageTable = conn.CASTable('inputTable_resized')
imageShow(conn, imageTable, 0, 4)
In [5]:
r = conn.image.processImages(casout={'name':'resultingImages','replace':True},
imagetable={'name':'inputTable_resized'},
imagefunctions=[
{'options':{'functiontype':'CONVERT_COLOR'}} #change color space
])
print(r)
outTable = conn.CASTable('resultingImages')
type(outTable)
Out[5]:
In [6]:
imageShow(conn, outTable, 0, 4)
In [8]:
r = conn.image.processImages(casout={'name':'resultingImages','replace':True},
imagetable={'name':'inputTable_resized'},
imagefunctions=[
{'options':{'functiontype':'CONVERT_COLOR'}}, #change color space
{'options':{'functiontype':'BILATERAL_FILTER', #noise reduction
'diameter':13,'sigmacolor':30,'sigmaspace':30}},
{'options':{'functiontype':'THRESHOLD', #image binarization
'type':'OTSU','value':125}},
{'options':{'functiontype':'LAPLACIAN', #edge detection with the Laplace operator
'kernelsize':12}}
])
print(r)
outTable = conn.CASTable('resultingImages')
imageShow(conn, outTable, 0, 4)
In [7]:
outTable.head(4)
Out[7]:
In [ ]:
# Process reference files to compare.
conn.image.loadImages(casout={'name':'inTable', 'replace':True}, path= 'path_reference_images')
conn.image.processImages(casout={'name':'refTable','replace':True},
imagetable={'name':'inTable'},
imagefunctions=[{'functionoptions':{'width':1000,'functiontype':'RESIZE','height':600}}, # resize
{'options':{'functiontype':'CONVERT_COLOR'}}, #change color space
{'options':{'functiontype':'BILATERAL_FILTER', #noise reduction
'diameter':13,'sigmacolor':30,'sigmaspace':30}},
{'options':{'functiontype':'THRESHOLD', #image binarization
'type':'OTSU','value':125}}
])
In [16]:
# Compare reference and source images to find the similarity index.
results = conn.image.compareImages(
casOut={
"name":"output",
"replace":True
},
pairAll=True,
referenceImages={
"table":{
"name":'refTable'
}},
sourceImages={
"table":{
"name":'resultingImages'
}}
)
scoreTable = conn.CASTable("output")
del scoreTable['_channel4_']
del scoreTable['_channel3_']
print(results)
print(scoreTable.head())
In [ ]:
# end the CAS session.
conn.session.endsession()