forked from JzHuai0108/ImageSegmentor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolormrf.h
134 lines (123 loc) · 5.44 KB
/
colormrf.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/******************************************************************
* Modul name : colormrf.cpp
* Author : Mihaly Gara ([email protected]) based on the code
* written by Csaba Gradwohl ([email protected])
* with some minor contributions from Zoltan Kato
* ([email protected]).
*
* Copyright : GNU General Public License www.gnu.org/copyleft/gpl.html
*
* Description:
* Color-based image segmentation using a Markov random field
* segmentation model and four different optimization algorithms:
* Metropolis - Simulated Annealing using Metropolis dynamics
* Gibbs - Simulated Annealing using a Gibbs sampler
* ICM - Iterated Conditional Modes, a deterministic suboptimal
* method (depends on a good initialization).
* MMD - Modified Metropolis Dynamics, a pseudo-stochastic
* suboptimal method which is less sensitive to
* initialization than ICM.
*
* The program GUI is written in wxWidgets hence the code can be
* compiled and ran under Windows as well as under Linux/Unix.
*
* $Id: colormrf.cpp,v 1.1 2009/01/09 20:48:09 kato Exp $
* $Revision: 1.1 $
* $State: Exp $
* $Log: colormrf.cpp,v $
* Revision 1.1 2009/01/09 20:48:09 kato
* Initial revision
*
*
*****************************************************************/
#define WINDOW_TITLE "MRF Color Image Segmentation Demo $Revision: 1.1 $"
#define VERSION "MRF Color Image Segmentation Demo $Revision: 1.1 $" \
" (Last built " __DATE__" "__TIME__") "
#define COPYRIGHT "(c) 2006 by Mihaly Gara, Csaba Gradwohl & Zoltan Kato" \
" (SZTE - Hungary)"
#include "cv.h"
#include "highgui.h"
#include "CKProcessTimeCounter.h"
static CKProcessTimeCounter timer("core"); // CPU timer
static bool timer_valid = false;
/* ColorMRF class: it handles all image operations such as
* loading, saving, etc...
*/
class ColorMRF
{
public:
void SetImage(int, int, unsigned char*data=NULL);
ColorMRF(); // constructor
~ColorMRF();
unsigned char *GetOrigImage();
unsigned char *GetLImage();
unsigned char *GetUImage();
unsigned char *GetVImage();
bool IsOutput(); // TRUE if out_image <> NULL
void SetNoRegions(int n); // sets the number of regions,
// allocates/frees memory for
// mean vectors and covariance matrices
int GetNoRegions() { return no_regions; }
void SetBeta(double b) { beta = b; }
void SetT(double x) { t = x; }
void SetT0(double t) { T0 = t; }
void SetC(double x) { c = x; }
void SetAlpha(double x) { alpha = x; }
void SetLuv(); // Luv settings
int GetK() { return K; }
double GetT() { return T; }
double GetE() { return E; }
double GetTimer() { return (timer_valid? timer.GetElapsedTimeMs() : 0.0); }
void SetMeanVar(int region, double**);
void CalculateMeanAndCovariance(int *region);// computes mean and
// covariance of the given region.
double CalculateEnergy(); // computes global energy
// based on the current
// lableing in data
double LocalEnergy(int i, int j, int label);// computes the local
// energy at site (i,j)
// assuming "label" has
// been assigned to it.
void Metropolis(bool mmd=false); // executes Metropolis or MMD (if mmd=true)
void ICM(); // executes ICM
void Gibbs(); // executes Gibbs sampler
unsigned char *in_data, *out_data; // input & output images. in_image contains
private:
unsigned char *l_data; // L* component
unsigned char *u_data; // u* component
unsigned char *v_data; // v* component
int width, height; // width and height of the
// displayed image
int no_regions; // number of regions for Gaussian
// parameter computation
int *out_regions; // display color of each label (=mean color)
double beta; // strength of second order clique potential
double t; // Stop criteraia threshold: stop
// if (deltaE < t)
double T0; // Initial temperature (not used by ICM)
double c; // Temperature scheduler's factor:
// T(n+1)=c*T(n).
double alpha; // alpha value for MMD
double **mean; // computed mean values and
double **variance; // variances and
double **covariance; // covariances for each region
double **invcov; // inverse covariance matrix
double *denom; // denominator for inverse covariance
double E; // current global energy
double E_old; // global energy in the prvious iteration
double T; // current temperature
int K; // current iteration #
int **classes; // this is the labeled image
double ***in_image_data; // Input image (in RGB color space)
void InitOutImage();
unsigned char *scale(double *luv_vector); // scaling into [0,255]
double *LuvToRGB(double *luv_pixel);// convert a pixel from CIE-L*u*v* to RGB
void CreateOutput(); // creates and draws the output
// image based on the current labeling
double Singleton(int i, int j, int label); // computes singleton
// potential at site
// (i,j) having a label "label"
double Doubleton(int i, int j, int label); // computes doubleton
// potential at site
// (i,j) having a label "label"
};