Skip to content
Snippets Groups Projects
Commit 32953eee authored by Angeline Aguinaldo's avatar Angeline Aguinaldo
Browse files

-Initialize opencv rng by iterating generator random-n times prior to training data

-Import matlib library directly
-Changed replicates to 5
-Pass zero matrix to execute kmeans++ alg in opencv (OpenCV does not use  means that are passed in)
parent 31c0fe98
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.5.1 (C:\Users\angeline\Anaconda3\python.exe)" jdkType="Python SDK" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.5.1 (C:\Users\angeline\Anaconda3\python.exe)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.5.1 (C:\Users\angeline\Anaconda3\envs\AnacondaEnv\python.exe)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
This diff is collapsed.
......@@ -37,6 +37,7 @@
# respective depth of the pixel in the Euclidean distance transform.
def PixelReplicate(bwim):
import numpy as np
from numpy import matlib as matlib
import cv2
# Computer Euclidean Distance Transform
......@@ -64,7 +65,7 @@ def PixelReplicate(bwim):
nrep = np.round(bwd[idxPts[0, 0], idxPts[0, 1]])
nrep = int(nrep)
nrepTotal = np.append(nrepTotal,nrep)
rp = np.matlib.repmat(idxPts, nrep, 1)
rp = matlib.repmat(idxPts, nrep, 1)
if (nInsert + nrep) > nPtsRep:
ptsRep = np.append(ptsRep, rp, axis=0)
else:
......@@ -81,20 +82,20 @@ def PixelReplicate(bwim):
def fitGMM(ptsRep, k):
import numpy as np
import cv2
import time
print("Fitting GMM...")
bestGMM = []
bestLL = float("-inf") # initialize lowest possible log likelihood value (-inf)
# Get unique points from replicated points
coorPtsRep = [tuple(x) for x in ptsRep]
uniqPts = sorted(set(coorPtsRep), key=lambda x: coorPtsRep.index(x))
uniqPts = np.array(uniqPts)
# Initialize random number generator
n = int(np.round(time.time() % 100))
for i in np.arange(0,n,1):
cv2.randu(1, 1, 10)
# GAUSSIAN MIXTURE MODEL FITTING
for i in np.arange(0, 10, 1): # Initialization Loop
# Random generation of indices into non-replicated points array
rndIdx = np.random.randint(low=0, high=uniqPts.shape[0], size=(1, 1, k), dtype=int)
rndMeans = np.matrix(uniqPts[np.array(rndIdx), 0:])
for i in np.arange(0, 5, 1): # Initialization Loop
# Must pass in zero matrix of means to execute kmeans++ alg in opencv
startMeans = np.matrix(np.zeros((k,2)))
# Initialize GMM model
gmm0 = cv2.ml.EM_create()
......@@ -104,16 +105,16 @@ def fitGMM(ptsRep, k):
gmm0.setTermCriteria(termCrit)
# Train the GMM
newGMM = gmm0.trainE(samples=ptsRep, means0=rndMeans)
newGMM = gmm0.trainE(samples=ptsRep, means0=startMeans)
# Calculate Log Likelihood of fit
LL = sum(newGMM[1]) # sum of log likelihoods per sample
print('Likelihood Logarithm of GMM fit (Rep. ', i, ') = ', LL)
# Select better fit using log likelihood comparison
if LL > bestLL: # closer to zero Negative-LL is better
bestGMM = gmm0
bestLL = LL
return bestGMM # return best gmm model (openCV EM class)
......
......@@ -36,14 +36,14 @@ import GENERATE_ELLIPSE_LIB as GenEllipse
# Generate random ellipse image
print("Generating Ellipse...")
K = 4 # number of ellipse to fit
bwim, trueEdges = GenEllipse.GetRandomEllipseImage(K)
# bwim, trueEdges = GenEllipse.GetRandomEllipseImage(K)
# # You may import an image for demo by uncommenting the following code:
# import cv2
# im = cv2.imread('U:\Angeline\PixelRep\Ellipse_k4.tif', cv2.CV_8UC1)
# th, bwim = cv2.threshold(im, 0, 1, cv2.THRESH_BINARY)
# trueEdges = [0, 0] # keep as [0, 0] if true edges are unknown.
# # Include (r, c) array of boundary pixels if known
import cv2
im = cv2.imread('U:\Angeline\~CODE\PixelRep\Ellipse_k4.tif', cv2.CV_8UC1)
th, bwim = cv2.threshold(im, 0, 1, cv2.THRESH_BINARY)
trueEdges = [0, 0] # keep as [0, 0] if true edges are unknown.
# Include (r, c) array of boundary pixels if known
# # (Remember to modify K number of ellipses)
# IMPORTANT NOTE:
......
File deleted
File deleted
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment