www.pudn.com > source.zip > energy.cpp
#include "gui.h"
/***************************************************
* get_color_pot_1_order()
*
* We compute here K + 1/2*(f-m)*invS*(f-m)^T , where f denotes the
* actual pixel, K denotes the log amplitude, m denotes the mean vector
* and invS denotes the inverse covariance matrix of the class.
**************************************************/
double ImageOperations::get_color_pot_1_order(int f, int row, int col, gray Class)
{
double result = 0.0;
int i, j;
double t1;//,t2;
// t2 = R1 * log_prior_color[Class] + R * log_numclass_prior_color;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
result += (Color[i][col][row] - ColorMean[i][Class]) *
(Color[j][col][row] - ColorMean[j][Class])
* InvColor[i][j][Class];
t1 = result / 2.0 + K_color[Class];
return t1;//+t2;
}
/***************************************************
* local_color_energy4()
*
* Standard local energy with a nearest neighborhood.
**************************************************/
double ImageOperations::local_color_energy4(int f, int row, int col, gray l)
{
double result;
double t1,t2;
t1 = get_color_pot_1_order(f, row, col, l);
t2 = local_color_interclue_energy(f, row, col, l);
result = t1+t2;
if (col > 0)
result += (l == Color_Layer[col-1][row]? -beta_color : beta_color);
if (col < width - 1)
result += (l == Color_Layer[col+1][row]? -beta_color : beta_color);
if (row > 0)
result += (l == Color_Layer[col][row-1]? -beta_color : beta_color);
if (row < height - 1)
result += (l == Color_Layer[col][row+1]? -beta_color : beta_color);
return(result);
}
/***************************************************
* local_color_interclue_energy()
*
* Inter-clue color local energy
**************************************************/
double ImageOperations::local_color_interclue_energy(int f, int row, int col, gray Class)
{
double result, color[6];
color[0] = get_color_pot_1_order(f, row, col, Class);
/* cooresponding pixcel on combined layer */
color[1] = get_color_pot_1_order(f, row, col,
X_Layer[col][row]%no_color_region);///no_texture_region);
result = 0.6*ABS(color[0]-color[1]);
if (row >= 2) {
color[2] = get_color_pot_1_order(f, row, col,
X_Layer[col][row-2]%no_color_region);///no_texture_region);
result += 0.1*ABS(color[0]-color[2]);
}
if (col >= 2) {
color[3] = get_color_pot_1_order(f, row, col,
X_Layer[col-2][row]%no_color_region);///no_texture_region);
result += 0.1*ABS(color[0]-color[3]);
}
if (row < height-2) { //cols
color[4] = get_color_pot_1_order(f, row, col,
X_Layer[col][row+2]%no_color_region);///no_texture_region);
result += 0.1*ABS(color[0]-color[4]);
}
if (col < width-2) { //rows
color[5] = get_color_pot_1_order(f, row, col,
X_Layer[col+2][row]%no_color_region);///no_texture_region);
result += 0.1*ABS(color[0]-color[5]);
}
return(Gamma[COMBINE_COLOR] * result);
/* return (-Gamma[COLOR] * */
/* (ABS(color[0]) < ABS(color[1])? color[0]/color[1] : color[1]/color[0])); */
}
/***************************************************
* global_color_energy()
*
* Standard global energy with a nearest neighborhood.
**************************************************/
double ImageOperations::global_color_energy()
{
int row, col;
double result = 0.0;
for (row = 0; row < height; row++) //row
for (col = 0; col < width; col++) { //col
result += get_color_pot_1_order(0, row, col, Color_Layer[col][row]);
result += local_color_interclue_energy(0, row, col, Color_Layer[col][row]);
if (col > 0)
result += (Color_Layer[col][row] == Color_Layer[col-1][row]? -beta_color : beta_color);
if (row > 0)
result += (Color_Layer[col][row] == Color_Layer[col][row-1]? -beta_color : beta_color);
}
return(result);
}
/***************************************************
* local_texture_energy4()
*
* Standard local energy with a nearest neighborhood.
**************************************************/
double ImageOperations::local_texture_energy4(int f, int row, int col, gray l)
{
double result;
double t1,t2;
t1 = get_texture_pot_1_order(f, row, col, l);
t2 = local_texture_interclue_energy(f, row, col, l);
result = t1+t2;
if (col > 0) /* nord */
result += (l == Texture_Layer[col-1][row]? -beta_texture : beta_texture);
if (col < width - 1) /* sud */
result += (l == Texture_Layer[col+1][row]? -beta_texture : beta_texture);
if (row > 0) /* west */
result += (l == Texture_Layer[col][row-1]? -beta_texture : beta_texture);
if (row < height - 1) /* est */
result += (l == Texture_Layer[col][row+1]? -beta_texture : beta_texture);
return(result);
}
/***************************************************
* get_texture_pot_1_order()
*
* We compute here K + 1/2*(f-m)*invS*(f-m)^T , where f denotes the
* actual pixel, K denotes the log amplitude, m denotes the mean vector
* and invS denotes the inverse covariance matrix of the class.
**************************************************/
double ImageOperations::get_texture_pot_1_order(int f, int row, int col, gray Class)
{
double result = 0.0;
int i, j;
double t1;//,t2;
//t2 = R1 * log_prior_texture[Class];// + R * log_numclass_prior_texture;
for (i = 0; i < no_g_feature; i++)
for (j = 0; j < no_g_feature; j++){
result +=
(Texture[i][col][row] - TextureMean[i][Class]) *
(Texture[j][col][row] - TextureMean[j][Class]) *
InvTexture[i][j][Class];
}
t1 = result / 2.0 + K_texture[Class];
return t1;//+t2;
}
/***************************************************
* local_texture_interclue_energy()
*
* Inter-clue texture local energy
**************************************************/
double ImageOperations::local_texture_interclue_energy(int f, int row, int col, gray Class)
{
double result, texture[6];
texture[0] = get_texture_pot_1_order(f, row, col, Class);
/* center */
texture[1] = get_texture_pot_1_order(f, row, col,
X_Layer[col][row]/no_color_region);//%no_texture_region);
result = 0.6*ABS(texture[0]-texture[1]);
if (row >= 2) {
texture[2] = get_texture_pot_1_order(f, row, col,
X_Layer[col][row-2]/no_color_region);//%no_texture_region);
result += 0.1*ABS(texture[0]-texture[2]);
}
if (col >= 2) {
texture[3] = get_texture_pot_1_order(f, row, col,
X_Layer[col-2][row]/no_color_region);//%no_texture_region);
result += 0.1*ABS(texture[0]-texture[3]);
}
if (row < height-2) {
texture[4] = get_texture_pot_1_order(f, row, col,
X_Layer[col][row+2]/no_color_region);//%no_texture_region);
result += 0.1*ABS(texture[0]-texture[4]);
}
if (col < width-2) {
texture[5] = get_texture_pot_1_order(f, row, col,
X_Layer[col+2][row]/no_color_region);//%no_texture_region);
result += 0.1*ABS(texture[0]-texture[5]);
}
return(Gamma[COMBINE_TEXTURE] * result);
/* return (-Gamma[TEXTURE] * */
/* (ABS(texture[0]) < ABS(texture[1])? texture[0]/texture[1] : texture[1]/texture[0])); */
}
/***************************************************
* global_texture_energy()
*
* Standard global energy with a nearest neighborhood.
**************************************************/
double ImageOperations::global_texture_energy()
{
int row, col;
double result = 0.0;
for (row = 0; row < height; row++)
for (col = 0; col < width; col++) {
result += get_texture_pot_1_order(0, row, col, Texture_Layer[row][col]);
result += local_texture_interclue_energy(0, row, col, Texture_Layer[row][col]);
if (col > 0) /* nord */
result += (Texture_Layer[col][row] == Texture_Layer[col-1][row]? -beta_texture : beta_texture);
if (row > 0) /* west */
result += (Texture_Layer[col][row] == Texture_Layer[col][row-1]? -beta_texture : beta_texture);
}
return(result);
}
/***************************************************
* local_combined_interclue_energy()
*
* Inter-clue local energy
**************************************************/
double ImageOperations::local_combined_interclue_energy(int f, int row, int col, gray Class)
{
double result,color_result,texture_result, texture[6], color[6];
texture[0] = get_texture_pot_1_order(f, row, col, Class/no_color_region);//%no_texture_region);
/* center */
texture[1] = get_texture_pot_1_order(f, row, col, Texture_Layer[col][row]);
texture_result = 0.6*ABS(texture[0]-texture[1]);
if (row >=2) {
texture[2] = get_texture_pot_1_order(f, row, col, Texture_Layer[col][row-2]);
texture_result += 0.1*ABS(texture[0]-texture[2]);
}
if (col >=2) {
texture[3] = get_texture_pot_1_order(f, row, col, Texture_Layer[col-2][row]);
texture_result += 0.1*ABS(texture[0]-texture[3]);
}
if (row < height-2) {
texture[4] = get_texture_pot_1_order(f, row, col, Texture_Layer[col][row+2]);
texture_result += 0.1*ABS(texture[0]-texture[4]);
}
if (col < width-2) {
texture[5] = get_texture_pot_1_order(f, row, col, Texture_Layer[col+2][row]);
texture_result += 0.1*ABS(texture[0]-texture[5]);
}
color[0] = get_color_pot_1_order(f, row, col, Class%no_color_region);//)/no_texture_region);
color[1] = get_color_pot_1_order(f, row, col, Color_Layer[col][row]);
color_result = 0.6*ABS(color[0]-color[1]);
if (row >=2) {
color[2] = get_color_pot_1_order(f, row, col, Color_Layer[col][row-2]);
color_result += 0.1*ABS(color[0]-color[2]);
}
if (col >=2) {
color[3] = get_color_pot_1_order(f, row, col, Color_Layer[col-2][row]);
color_result += 0.1*ABS(color[0]-color[3]);
}
if (row < height-2) {
color[4] = get_color_pot_1_order(f, row, col, Color_Layer[col][row+2]);
color_result += 0.1*ABS(color[0]-color[4]);
}
if (col < width-2) {
color[5] = get_color_pot_1_order(f, row, col, Color_Layer[col+2][row]);
color_result += 0.1*ABS(color[0]-color[5]);
}
result = Gamma[TEXTURE_COMBINE] * texture_result + Gamma[COLOR_COMBINE] * color_result
+ R1 * (log_prior[Class] + log_numclass_prior);
/* result = -Gamma[TEXTURE] * */
/* (ABS(texture[0]) < ABS(texture[1])? texture[0]/texture[1] : texture[1]/texture[0]) + */
/* -Gamma[COLOR] * */
/* (ABS(color[0]) < ABS(color[1])? color[0]/color[1] : color[1]/color[0]); */
return (result);
}
/***************************************************
* local_combined_energy()
*
* Combined local energy
**************************************************/
double ImageOperations::local_combined_energy(int f, int row, int col, gray Class)
{
double result;
result = local_combined_interclue_energy(f, row, col, Class);
if (col > 0) /* nord */
result +=
(Class == X_Layer[col-1][row]? -alpha_combined :
/* texture matches only */
(Class/no_color_region == Texture_Layer[col-1][row]&& Class%no_color_region != Color_Layer[col-1][row])? alpha_combined :
/* color matches only */
(Class/no_color_region != Texture_Layer[col-1][row]&& Class%no_color_region == Color_Layer[col-1][row])? alpha_combined : 0);
if (col < width - 1) /* south */
result +=
(Class == X_Layer[col+1][row]? -alpha_combined :
/* texture matches only */
(Class/no_color_region == Texture_Layer[col+1][row]&& Class%no_color_region != Color_Layer[col+1][row])? alpha_combined :
/* color matches only */
(Class/no_color_region != Texture_Layer[col+1][row]&& Class%no_color_region == Color_Layer[col+1][row])? alpha_combined : 0);
if (row > 0) /* west */
result +=
(Class == X_Layer[col][row-1]?
-alpha_combined :
/* texture matches only */
(Class/no_color_region == Texture_Layer[col][row-1]&& Class%no_color_region != Color_Layer[col][row-1])? alpha_combined:
/* color matches only */
(Class/no_color_region != Texture_Layer[col][row-1]&& Class%no_color_region == Color_Layer[col][row-1])? alpha_combined : 0);
if (row < height - 1) /* east */
result +=
(Class == X_Layer[col][row+1]?
-alpha_combined :
/* texture matches only */
(Class/no_color_region == Texture_Layer[col][row+1]&&Class%no_color_region != Color_Layer[col][row+1])? alpha_combined :
/* color matches only */
(Class/no_color_region != Texture_Layer[col][row+1]&&Class%no_color_region == Color_Layer[col][row+1])? alpha_combined : 0);
return(result);
}
/***************************************************
* global_color_energy()
*
* Combined global energy with a nearest neighborhood.
**************************************************/
double ImageOperations::global_combined_energy()
{
int row, col;
double result;
result = global_texture_energy() + global_color_energy();
for (row = 0; row < height; row++)
for (col = 0; col < width; col++) {
result += local_combined_interclue_energy(0, row, col, X_Layer[col][row]);
if (col > 0) /* nord */
result += (X_Layer[col][row] == X_Layer[col-1][row]? -alpha_combined : alpha_combined);
if (row > 0) /* west */
result += (X_Layer[col][row] == X_Layer[col][row-1]? -alpha_combined : alpha_combined);
}
return(result);
}