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

gmm with Replicates

parent 61a70568
Branches
No related tags found
No related merge requests found
...@@ -24,8 +24,8 @@ ...@@ -24,8 +24,8 @@
<file leaf-file-name="PixelRep_Demo.py" pinned="false" current-in-tab="false"> <file leaf-file-name="PixelRep_Demo.py" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/PixelRep_Demo.py"> <entry file="file://$PROJECT_DIR$/PixelRep_Demo.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="298"> <state relative-caret-position="162">
<caret line="38" column="60" selection-start-line="38" selection-start-column="60" selection-end-line="38" selection-end-column="60" /> <caret line="30" column="0" selection-start-line="30" selection-start-column="0" selection-end-line="60" selection-end-column="51" />
<folding> <folding>
<element signature="e#1584#1618#0" expanded="true" /> <element signature="e#1584#1618#0" expanded="true" />
</folding> </folding>
...@@ -33,33 +33,21 @@ ...@@ -33,33 +33,21 @@
</provider> </provider>
</entry> </entry>
</file> </file>
<file leaf-file-name="TestScript.py" pinned="false" current-in-tab="false"> <file leaf-file-name="PIXEL_REPLICATION_LIB.py" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/TestScript.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1041">
<caret line="80" column="0" selection-start-line="80" selection-start-column="0" selection-end-line="80" selection-end-column="0" />
<folding>
<element signature="e#1584#1618#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file leaf-file-name="PIXEL_REPLICATION_LIB.py" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/PIXEL_REPLICATION_LIB.py"> <entry file="file://$PROJECT_DIR$/PIXEL_REPLICATION_LIB.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1054"> <state relative-caret-position="522">
<caret line="101" column="0" selection-start-line="101" selection-start-column="0" selection-end-line="101" selection-end-column="0" /> <caret line="86" column="0" selection-start-line="86" selection-start-column="0" selection-end-line="89" selection-end-column="23" />
<folding /> <folding />
</state> </state>
</provider> </provider>
</entry> </entry>
</file> </file>
<file leaf-file-name="GENERATE_ELLIPSE_LIB.py" pinned="false" current-in-tab="true"> <file leaf-file-name="GENERATE_ELLIPSE_LIB.py" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/GENERATE_ELLIPSE_LIB.py"> <entry file="file://$PROJECT_DIR$/GENERATE_ELLIPSE_LIB.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="341"> <state relative-caret-position="-84">
<caret line="85" column="56" selection-start-line="85" selection-start-column="56" selection-end-line="85" selection-end-column="56" /> <caret line="122" column="42" selection-start-line="122" selection-start-column="42" selection-end-line="122" selection-end-column="42" />
<folding /> <folding />
</state> </state>
</provider> </provider>
...@@ -393,9 +381,9 @@ ...@@ -393,9 +381,9 @@
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.0" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" /> <window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.0" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
<window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" /> <window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
<window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.32921812" sideWeight="0.50208336" order="7" side_tool="true" content_ui="tabs" /> <window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.32921812" sideWeight="0.50208336" order="7" side_tool="true" content_ui="tabs" />
<window_info id="Python Console" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.60260415" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" /> <window_info id="Python Console" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.6515625" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="8" side_tool="false" content_ui="tabs" /> <window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="8" side_tool="false" content_ui="tabs" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.23868313" sideWeight="0.49791667" order="2" side_tool="false" content_ui="tabs" /> <window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.23868313" sideWeight="0.49791667" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" /> <window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="9" side_tool="false" content_ui="tabs" /> <window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="9" side_tool="false" content_ui="tabs" />
<window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="2" side_tool="true" content_ui="tabs" /> <window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="2" side_tool="true" content_ui="tabs" />
...@@ -548,15 +536,23 @@ ...@@ -548,15 +536,23 @@
<state relative-caret-position="1041"> <state relative-caret-position="1041">
<caret line="80" column="0" selection-start-line="80" selection-start-column="0" selection-end-line="80" selection-end-column="0" /> <caret line="80" column="0" selection-start-line="80" selection-start-column="0" selection-end-line="80" selection-end-column="0" />
<folding> <folding>
<element signature="e#1584#1618#0" expanded="true" /> <element signature="e#1584#1618#0" expanded="false" />
</folding> </folding>
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$PROJECT_DIR$/GENERATE_ELLIPSE_LIB.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-84">
<caret line="122" column="42" selection-start-line="122" selection-start-column="42" selection-end-line="122" selection-end-column="42" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/PixelRep_Demo.py"> <entry file="file://$PROJECT_DIR$/PixelRep_Demo.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="298"> <state relative-caret-position="162">
<caret line="38" column="60" selection-start-line="38" selection-start-column="60" selection-end-line="38" selection-end-column="60" /> <caret line="30" column="0" selection-start-line="30" selection-start-column="0" selection-end-line="60" selection-end-column="51" />
<folding> <folding>
<element signature="e#1584#1618#0" expanded="true" /> <element signature="e#1584#1618#0" expanded="true" />
</folding> </folding>
...@@ -565,16 +561,8 @@ ...@@ -565,16 +561,8 @@
</entry> </entry>
<entry file="file://$PROJECT_DIR$/PIXEL_REPLICATION_LIB.py"> <entry file="file://$PROJECT_DIR$/PIXEL_REPLICATION_LIB.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1054"> <state relative-caret-position="522">
<caret line="101" column="0" selection-start-line="101" selection-start-column="0" selection-end-line="101" selection-end-column="0" /> <caret line="86" column="0" selection-start-line="86" selection-start-column="0" selection-end-line="89" selection-end-column="23" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/GENERATE_ELLIPSE_LIB.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="341">
<caret line="85" column="56" selection-start-line="85" selection-start-column="56" selection-end-line="85" selection-end-column="56" />
<folding /> <folding />
</state> </state>
</provider> </provider>
......
...@@ -40,7 +40,7 @@ def PixelReplicate(bwim): ...@@ -40,7 +40,7 @@ def PixelReplicate(bwim):
import cv2 import cv2
print("Computing Distance Transform...") print("Computing Distance Transform...")
bwd = cv2.distanceTransform(bwim, distanceType=cv2.DIST_L2, maskSize=cv2.DIST_MASK_5) bwd = cv2.distanceTransform(bwim, distanceType=cv2.DIST_L2, maskSize=cv2.DIST_MASK_PRECISE)
bwd = np.asmatrix(bwd, dtype=float) bwd = np.asmatrix(bwd, dtype=float)
roundBwd = np.round(bwd) roundBwd = np.round(bwd)
nPtsRep = np.sum(roundBwd, dtype=int) nPtsRep = np.sum(roundBwd, dtype=int)
...@@ -71,17 +71,26 @@ def PixelReplicate(bwim): ...@@ -71,17 +71,26 @@ def PixelReplicate(bwim):
def fitGMM(ptsRep, k): def fitGMM(ptsRep, k):
from sklearn.mixture import GMM from sklearn.mixture import GMM
import numpy as np
import cv2 import cv2
print("Fitting GMM...") print("Fitting GMM...")
gmm0 = cv2.ml.EM_create() gmm0 = cv2.ml.EM_create()
gmm0.setCovarianceMatrixType(2) gmm0.setCovarianceMatrixType(2)
gmm0.setClustersNumber(k) gmm0.setClustersNumber(k)
termCrit = (20, 400, 1e-4) termCrit = (20, 400, 1e-15)
gmm0.setTermCriteria(termCrit) gmm0.setTermCriteria(termCrit)
gmm0.trainEM(ptsRep) bestGMM = []
bestLL = float("-inf")
for i in np.arange(0, 10, 1):
print('Replicate: ', str(i+1))
newGMM = gmm0.trainEM(ptsRep)
LL = sum(newGMM[1])
if LL > bestLL:
bestGMM = gmm0
bestLL = LL
# gmm0 = GMM(n_components=k, covariance_type='full', n_iter=400, n_init=20, verbose=0, tol=1e-4, init_params='m') # gmm0 = GMM(n_components=k, covariance_type='full', n_iter=400, n_init=20, verbose=0, tol=1e-4, init_params='m')
# gmmfit = gmm0.fit(ptsRep) # gmmfit = gmm0.fit(ptsRep)
return gmm0 return bestGMM
def drawClusters(gmm0, bwim, ptsEdge): def drawClusters(gmm0, bwim, ptsEdge):
import numpy as np import numpy as np
...@@ -107,12 +116,12 @@ def drawClusters(gmm0, bwim, ptsEdge): ...@@ -107,12 +116,12 @@ def drawClusters(gmm0, bwim, ptsEdge):
plt.hold(True) plt.hold(True)
print("Plotting...") print("Plotting...")
colors = ['m', 'r', 'c', 'g', 'y', 'b'] colors = ['m', 'r', 'c', 'g', 'y', 'b']
for h in np.unique(clusters): for h in np.arange(0, np.max(clusters)+1, 1):
clustIdx = np.where(clusters == h) clustIdx = np.where(clusters == h)
bwLabel[np.array([pts[clustIdx, 0]]), np.array([pts[clustIdx, 1]])] = h + 1 bwLabel[np.array([pts[clustIdx[0], 0]]), np.array([pts[clustIdx[0], 1]])] = h + 1
plt.scatter(x=pts[clustIdx, 1], y=pts[clustIdx, 0], s=50, c=colors[h], lw=0) plt.scatter(x=pts[clustIdx[0], 1], y=pts[clustIdx[0], 0], s=40, c=colors[h], lw=0)
if ~all(ptsEdge == 0): if sum(ptsEdge) != 0:
plt.scatter(ptsEdge[0:, 1], ptsEdge[0:, 1], s=50, c='w', lw=0) plt.scatter(ptsEdge[0:, 1], ptsEdge[0:, 1], s=50, c='w', lw=0)
plt.axis('off') plt.axis('off')
......
...@@ -33,12 +33,12 @@ import GENERATE_ELLIPSE_LIB as GenEllipse ...@@ -33,12 +33,12 @@ import GENERATE_ELLIPSE_LIB as GenEllipse
# Generate random ellipse image # Generate random ellipse image
print("Generating Ellipse...") print("Generating Ellipse...")
K = 5 # number of ellipse to fit K = 4 # number of ellipse to fit
bwim, trueBounds = GenEllipse.GetRandomEllipseImage(K) # bwim, trueBounds = GenEllipse.GetRandomEllipseImage(K)
# # You may import an image for demo by uncommenting the following code: # # You may import an image for demo by uncommenting the following code:
import cv2 import cv2
im = cv2.imread('U:\Angeline\PixelRep\Ellipse.tif', cv2.CV_8UC1) im = cv2.imread('U:\Angeline\PixelRep\Ellipse_k4.tif', cv2.CV_8UC1)
# im = cv2.imread('INSERT FILENAME', cv2.CV_8UC1) # im = cv2.imread('INSERT FILENAME', cv2.CV_8UC1)
th, bwim = cv2.threshold(im, 0, 1, cv2.THRESH_BINARY) th, bwim = cv2.threshold(im, 0, 1, cv2.THRESH_BINARY)
trueBounds = [0, 0] trueBounds = [0, 0]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment