📄 txtrsubs.c
字号:
/********************************************
*
* file txtrsubs.c
*
* Functions: This file contains
* sigma
* skewness
* amean
* adifference
* difference_array
* hurst
* compare
* get_texture_options
*
* Purpose:
* These functions calculate measures
* that help distinguish textures.
*
* External Calls:
* utility.c - fix_edges
* sort_elements
* fitt.c - fit
*
* Modifications:
* 12 August 1993- created
* 27 August 1998 - modified to work on
* entire images at once.
*
********************************************/
#include "cips.h"
/*******************************************
*
* sigma(..
*
* This calculates the variance and the
* sigma for a sizeXsize area.
*
* It sums the squares of the difference
* between each pixel and the mean of
* the area and divides that by the
* number of pixels in the area.
*
* The output image is set to the square
* root of the variance since the variance
* will almost certainly be out of range
* for the image. The square root of the
* variance will be sigma.
*
*******************************************/
sigma(the_image, out_image,
size, threshold, high,
rows, cols, bits_per_pixel)
int high, threshold, size;
long bits_per_pixel, cols, rows;
short **the_image,
**out_image;
{
int a, b, count, i, j, k,
max, mean, new_hi, new_low,
sd2, sd2p1;
short sigma;
unsigned long diff, variance;
sd2 = size/2;
sd2p1 = sd2 + 1;
max = 255;
new_hi = 250;
new_low = 16;
if(bits_per_pixel == 4){
new_hi = 10;
new_low = 3;
max = 16;
}
/***************************
*
* Loop over image array
*
****************************/
printf("\n");
for(i=sd2; i<rows-sd2; i++){
if( (i%10) == 0) printf("%d ", i);
for(j=sd2; j<cols-sd2; j++){
/*****************************
*
* Run through the small area
* and calculate the mean.
*
******************************/
mean = 0;
for(a=-sd2; a<sd2p1; a++){
for(b=-sd2; b<sd2p1; b++){
mean = mean + the_image[i+a][j+b];
}
}
mean = mean/(size*size);
/*****************************
*
* Run through the small area
* again and the calculate the
* variance.
*
******************************/
variance = 0;
diff = 0;
for(a=-sd2; a<sd2p1; a++){
for(b=-sd2; b<sd2p1; b++){
diff = the_image[i+a][j+b] - mean;
variance = variance + (diff*diff);
}
}
variance = variance/(size*size);
sigma = sqrt(variance);
if(sigma > max) sigma = max;
out_image[i][j] = sigma;
} /* ends loop over j */
} /* ends loop over i */
/* if desired, threshold the output image */
if(threshold == 1){
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
if(out_image[i][j] > high){
out_image[i][j] = new_hi;
}
else{
out_image[i][j] = new_low;
}
}
}
} /* ends if threshold == 1 */
fix_edges(out_image, sd2, rows-1, cols-1);
} /* ends sigma */
/*******************************************
*
* skewness(..
*
* This calculates the skewness for a
* sizeXsize area.
*
* Look at Levine's book page 449 for
* the formula.
* "Vision in Man and Machine" by
* Martin D. Levine, McGraw Hill, 1985.
*
*******************************************/
skewness(the_image, out_image,
size, threshold, high,
rows, cols, bits_per_pixel)
int high, threshold, size;
long bits_per_pixel, cols, rows;
short **the_image,
**out_image;
{
int a, b, count, i, j, k,
max, mean, new_hi, new_low,
sd2, sd2p1;
long cube;
short sigma, skew;
unsigned long diff, sigma3, variance;
sd2 = size/2;
sd2p1 = sd2 + 1;
max = 255;
new_hi = 250;
new_low = 16;
if(bits_per_pixel == 4){
new_hi = 10;
new_low = 3;
max = 16;
}
/***************************
*
* Loop over image array
*
****************************/
printf("\n");
for(i=sd2; i<rows-sd2; i++){
if( (i%10) == 0) printf("%d ", i);
for(j=sd2; j<cols-sd2; j++){
/*****************************
*
* Run through the small area
* and calculate the mean.
*
******************************/
mean = 0;
for(a=-sd2; a<sd2p1; a++){
for(b=-sd2; b<sd2p1; b++){
mean = mean + the_image[i+a][j+b];
}
}
mean = mean/(size*size);
/*****************************
*
* Run through the small area
* again and the calculate the
* variance and the cube.
*
******************************/
variance = 0;
diff = 0;
cube = 0;
for(a=-sd2; a<sd2p1; a++){
for(b=-sd2; b<sd2p1; b++){
diff = the_image[i+a][j+b] - mean;
cube = cube + (diff*diff*diff);
variance = variance + (diff*diff);
}
}
variance = variance/(size*size);
sigma = sqrt(variance);
sigma3 = sigma*sigma*sigma;
if(sigma3 == 0)
sigma3 = 1;
skew = cube/(sigma3*size*size);
out_image[i][j] = skew;
if(out_image[i][j] > max)
out_image[i][j] = max;
} /* ends loop over j */
} /* ends loop over i */
/* if desired, threshold the output image */
if(threshold == 1){
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
if(out_image[i][j] > high){
out_image[i][j] = new_hi;
}
else{
out_image[i][j] = new_low;
}
}
}
} /* ends if threshold == 1 */
fix_edges(out_image, sd2, rows-1, cols-1);
} /* ends skewness */
/*******************************************
*
* adifference(..
*
* This function performs the difference
* operation for a specified array
* in an image file.
*
*******************************************/
adifference(the_image, out_image,
size,
rows, cols)
int size;
long cols, rows;
short **the_image,
**out_image;
{
int sd2, sd2p1;
sd2 = size/2;
sd2p1 = sd2 + 1;
difference_array(the_image, out_image,
size, rows, cols);
fix_edges(out_image, sd2, rows-1, cols-1);
} /* ends adifference */
/*******************************************
*
* difference_array(..
*
* This function takes the input image
* array the_image and places in out_image
* the gray level differences of the pixels
* in the_image. It uses the size
* parameter for the distance between pixels
* used to get the difference.
*
*******************************************/
difference_array(the_image, out_image,
size, rows, cols)
int size;
long cols, rows;
short **the_image,
**out_image;
{
int i, j, sd2;
sd2 = size/2;
for(i=sd2; i<rows-sd2; i++){
for(j=sd2; j<cols-sd2; j++){
out_image[i][j] =
abs(the_image[i][j] -
the_image[i+sd2][j+sd2]);
} /* ends loop over j */
} /* ends loop over i */
fix_edges(out_image, sd2, rows-1, cols-1);
} /* ends difference_array */
/*******************************************
*
* amean(..
*
* This calculates the mean measure
* for a sizeXsize area.
*
* Look at Levine's book page 451 for
* the formula.
* "Vision in Man and Machine" by
* Martin D. Levine, McGraw Hill, 1985.
*
*******************************************/
amean(the_image, out_image,
size,
rows, cols, bits_per_pixel)
int size;
long bits_per_pixel, cols, rows;
short **the_image,
**out_image;
{
int a, b, count, i, j, k, max,
sd2, sd2p1;
short pixel;
unsigned long big;
sd2 = size/2;
sd2p1 = sd2 + 1;
max = 255;
if(bits_per_pixel == 4){
max = 16;
}
/**************************************
*
* Calculate the gray level difference
* array.
*
***************************************/
difference_array(the_image, out_image,
size, rows, cols);
for(i=0; i<rows; i++)
for(j=0; j<cols; j++)
the_image[i][j] = out_image[i][j];
/**************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -