EMAN2
tomoseg.cpp
Go to the documentation of this file.
1/*
2 * This software is issued under a joint BSD/GNU license. You may use the
3 * source code in this file under either license. However, note that the
4 * complete EMAN2 and SPARX software packages have some GPL dependencies,
5 * so you are responsible for compliance with the licenses of these packages
6 * if you opt to use BSD licensing. The warranty disclaimer below holds
7 * in either instance.
8 *
9 * This complete copyright notice must be included in any revised version of the
10 * source code. Additional authorship citations may be added, but existing
11 * author citations must be preserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by.edge
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28#include "tomoseg.h"
29#include <algorithm>
30using namespace EMAN;
31
32float DistToLine(int x0,int y0,int x1,int y1,int x2,int y2){
33 return abs((x2-x1)*(y1-y0)-(x1-x0)*(y2-y1))/sqrt((float)((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
34}
35
36TomoObject::TomoObject(vector<Vec3i> allpt, float md, int slice){
37 nowslice=slice;
38 maxdist=md;
39 // find one end point
40 for (int i=0; i<(int)allpt.size(); i++){
41 if (allpt[i][2]==1){
42 allpoints.push_back(allpt[i]);
43 allpt[i][2]=-1;
44 break;
45 }
46 }
47
48 // sort points from one endpoint to the other
49 for (int i=0; i<(int)allpt.size()-1; i++){
50 float mindst=1e10;
51 int nxti=-1;
52 Vec3i nowp=allpoints.back();
53 for (int j=0; j<(int)allpt.size(); j++){
54 if (allpt[j][2]>0){
55 float dst=(allpt[j][0]-nowp[0])*(allpt[j][0]-nowp[0])+(allpt[j][1]-nowp[1])*(allpt[j][1]-nowp[1]);
56 if (dst<mindst){
57 mindst=dst;
58 nxti=j;
59 }
60 }
61 }
62 if (nxti>=0){
63 allpoints.push_back(allpt[nxti]);
64 allpt[nxti][2]=-1;
65 }
66 else
67 break;
68 }
69// for (int i=0; i<(int)allpoints.size(); i++)
70// printf("(%d,%d)",allpoints[i][0],allpoints[i][1]);
71// printf("\n");
72 ptid.push_back(0);
73 ptid.push_back((int)allpoints.size()-1);
74
75
76 // break into line segments
77 while(1){
78 int newpt=bend();
79 if (newpt>0){
80 ptid.push_back(newpt);
81 sort(ptid.begin(),ptid.end());
82// for (int i=0; i<ptid.size(); i++) printf("%d\t",ptid[i]);
83// printf("\n");
84
85 }
86 else
87 break;
88 }
89
90}
91
93 // distance to current line segments
94 float maxd=maxdist;
95 int newpt=-1;
96 for (int i=1; i<(int)ptid.size(); i++){
97 int x1=allpoints[ptid[i-1]][0],y1=allpoints[ptid[i-1]][1];
98 int x2=allpoints[ptid[i]][0],y2=allpoints[ptid[i]][1];
99 for (int j=ptid[i-1]; j<ptid[i]; j++){
100 int x0=allpoints[j][0],y0=allpoints[j][1];
101 float dst=DistToLine(x0,y0,x1,y1,x2,y2);
102 if (dst>maxd){
103 maxd=dst;
104 newpt=j;
105 }
106 }
107
108 }
109 return newpt;
110}
111
113 fprintf(fp,"color 0 1 0 0\nopen\ncontour 0 0 %d\n",(int)ptid.size());
114 for (int i=0; i<(int)ptid.size(); i++){
115 fprintf(fp,"%d %d %d\n",allpoints[ptid[i]][0],allpoints[ptid[i]][1],nowslice);
116 }
117}
118
120 return (int)ptid.size();
121}
122
123void TomoObject::enlong(EMData *bwmap,EMData *skelmap){
124 // first push the current segment in.
125 int myid=skelmap->get_value_at(allpoints[0][0],allpoints[0][1]);
126 segid.push_back(myid);
127
128 //
129
130}
131
132
136
137
139 if (!map)
140 return 0;
141 skelmap=map;
142 return 1;
143}
144
145int TomoSeg::generate_objects(int numo, float maxdist, int nowslice){
146 if (!skelmap){
147 printf("No map input.\n");
148 return 0;
149 }
150
151 // Calculate area of each segment
152 EMData *rank=skelmap->process("threshold.notzero");
153 rank->process_inplace("morph.object.density",Dict("thresh",0,"more_neighbor",true));
154 int nx=rank->get_xsize();
155 int ny=rank->get_ysize();
156
157 // Take numo largest objects
158 for (int i=0; i<numo; i++){
159 float max=rank->get_attr("maximum");
160 vector <Vec3i> pts;
161 float curid=-1;
162 bool branch=false;
163 if (max<=0)
164 break;
165 for (int x=0; x<nx; x++){
166 for (int y=0; y<ny; y++){
167 if (rank->get_value_at(x,y)==max){
168 if (curid<0)
169 curid=skelmap->get_value_at(x,y);
170 if (curid==skelmap->get_value_at(x,y)){
171 int nb=check_neighbors(x,y);
172 if (nb>2) branch=true;
173 pts.push_back(Vec3i(x,y,nb));
174 rank->set_value_at_fast(x,y,0);
175 }
176 }
177 }
178 }
179 if(verb) printf("id=%d, size=%d",int(curid),int(pts.size()));
180 if (branch){
181 if (verb) printf("\n\thave branch, throw out.\n");
182 }
183 else{
184 objs.push_back(TomoObject(pts,maxdist,nowslice));
185 if (verb) printf(" \t%d points\n",objs.back().get_size() );
186 }
187
188 }
189
190 delete rank;
191 return 1;
192}
193
194void TomoSeg::write_imod(const char *file){
195 FILE *fp = fopen(file, "w");
196 int nx=skelmap->get_xsize();
197 int ny=skelmap->get_ysize();
198 int nz=skelmap->get_zsize();
199 fprintf(fp,"imod %d\n",(int)objs.size());
200 fprintf(fp,"max %d %d %d\n",nx,ny,nz);
201 for (int i=0; i<(int)objs.size(); i++){
202 fprintf(fp,"object %d 1 0\n",i);
203 objs[i].write_imod(fp);
204 fprintf(fp,"\n");
205 }
206 fclose(fp);
207}
208
210 int nb=-1;
211 for (int i=-1; i<=1; i++){
212 for (int j=-1; j<=1; j++){
213 nb+= (skelmap->get_value_at(x+i,y+j)>0 ? 1 : 0);
214 }
215 }
216 return nb;
217}
Dict is a dictionary to store <string, EMObject> pair.
Definition: emobject.h:385
EMData stores an image's data and defines core image processing routines.
Definition: emdata.h:82
void enlong(EMData *bwmap, EMData *skelmap)
Definition: tomoseg.cpp:123
vector< int > segid
Definition: tomoseg.h:50
void write_imod(FILE *fp)
Definition: tomoseg.cpp:112
vector< int > ptid
Definition: tomoseg.h:49
vector< Vec3i > allpoints
Definition: tomoseg.h:51
float maxdist
Definition: tomoseg.h:52
int check_neighbors(int x, int y)
Definition: tomoseg.cpp:209
int generate_objects(int numo, float maxdist, int nowslice)
Definition: tomoseg.cpp:145
void write_imod(const char *file)
Definition: tomoseg.cpp:194
int read_skelmap(EMData *map)
Definition: tomoseg.cpp:138
EMData * skelmap
Definition: tomoseg.h:77
bool verb
Definition: tomoseg.h:79
vector< TomoObject > objs
Definition: tomoseg.h:80
EMData * sqrt() const
return square root of current image
E2Exception class.
Definition: aligner.h:40
Vec3< int > Vec3i
Definition: vec3.h:694
#define y(i, j)
Definition: projector.cpp:1516
#define x(i)
Definition: projector.cpp:1517
float DistToLine(int x0, int y0, int x1, int y1, int x2, int y2)
Definition: tomoseg.cpp:32