📄 imggen.c
字号:
/****************************************************************************
File Name : imggen.c
Purpose : generate image based on contour
Date : Aug 31,1995
GSNAKE API is jointly developed by the Information Technology Institute (ITI), Singapore, and the School of Applied Science, Nanyang Technological
University (NTU), Singapore.
These software programs are available to the user without any license or royalty fees. Permission is hereby granted to use, copy, modify, and distribute this software and its documentation for any purpose. ITI and NTU gives no warranty, express, implied, or statuary for the software and/or documentation provided, including, without limitation, waranty of merchantibility and warranty of fitness for a particular purpose. The software provided hereunder is on an "as is" basis, and ITI and NTU has no obligation to provide maintenance, support, updates, enhancements, or modifications.
GSNAKE API is available for any UNIX compatible system. User feedback, bugs, or software and manual suggestions should be sent via electronic mail to one of the following, who may or may not act on them as he/she desires :
asschan@ntu.ac.sg
kflai@iti.gov.sg
***************************************************************************/
#include "gsnake.h"
#include "random.h"
#include "xwindow.h"
void testmain(int contourMode, char *contourFName,
double Mx, double My, double dx, double dy, double R, double Dx,
double Dy, int numSnaxel, double deformNoiseStd, char *imgFileName,
int numImgRow, int numImgCol, int blackPixel, int WhitePixel,
double imgNoiseStd, int verbose);
void printhelp(char *);
void getparam(int ac, char** av, int *contourMode, char *contourFName,
double *Mx, double *My, double *dx, double *dy, double *R, double *Dx,
double *Dy, int *numSnaxel, double *deformNoiseStd, char *imgFileName,
int *numImgRow, int *numImgCol, int *blackPixel, int *WhitePixel,
double *imgNoiseStd, int *verbose) ;
enum { _Circle, _Rectangle, _Manual, _File } ;
main(int argc,char **argv)
{
int contourMode = _Circle ;
char contourFName[100] ; /* contour file name */
double Mx = 1.0, My = 1.0 ; /* scale change parameters */
double dx = 0.0, dy = 0.0; /* displacement */
double RotateAngle = 1.0; /* angle for rotation */
double Dx = 0.0, Dy = 0.0; /* dilation */
int numSnaxel = 8; /* number of snaxel */
double deformNoiseStd = 0.0; /* deformation noise */
char imgFileName[100]; /* image file name */
int numImgRow = 129, numImgCol = 129; /* image dimension */
int blackPixel = 50, whitePixel = 150; /* pixel values */
double imgNoiseStd = 0.0 ; /* image noise */
int verbose = 1 ;
sprintf(imgFileName, "test.rs");
if (argc==1) printhelp(argv[0]);
else
{
getparam(argc, argv, &contourMode, contourFName,
&Mx, &My, &dx, &dy, &RotateAngle, &Dx, &Dy,
&numSnaxel, &deformNoiseStd, imgFileName,
&numImgRow, &numImgCol,
&blackPixel, &whitePixel,
&imgNoiseStd, &verbose) ;
testmain(contourMode, contourFName,
Mx, My, dx, dy, RotateAngle, Dx, Dy,
numSnaxel, deformNoiseStd, imgFileName,
numImgRow, numImgCol, blackPixel, whitePixel,
imgNoiseStd, verbose);
return(0);
}
}
void testmain(int contourMode, char *contourFName,
double Mx, double My, double dx, double dy, double RotateAngle, double Dx,
double Dy, int numSnaxel, double deformNoiseStd, char *imgFileName,
int numImgRow, int numImgCol, int blackPixel, int whitePixel,
double imgNoiseStd, int verbose)
{
IMAGE myImage ;
CONTOUR mycontour;
SNAXEL *sptr;
register int i=0, j, k;
int status ;
double AvgLength ;
myImage.init(numImgRow, numImgCol) ;
switch(contourMode) {
case _Manual : status = mycontour.init(&myImage) ;
break ;
case _File : status = mycontour.read(contourFName) ;
break ;
case _Rectangle :
status = mycontour.init(0, 0, 0.0, 4);
double Len = (double)(int) MIN(numImgRow, numImgCol)/4;
for(sptr = mycontour.getHead(), k=0 ; sptr ;
sptr = sptr->getNext(), k++) {
sptr->putCol((k==0 || k==3) ? -Len : Len);
sptr->putRow((k<2) ? -Len : Len);
}
break ;
default : status = mycontour.init(numImgRow/2, numImgCol/2,
MIN(numImgRow, numImgCol)/4.0, numSnaxel);
}
if( status != NOERROR )
return ;
rand_init() ; /* initialise random number generator */
/* handle affine transformation */
mycontour.contourCentered() ;
mycontour.scale(Mx, My);
mycontour.dilate(Dx, Dy) ;
mycontour.rotate(RotateAngle);
AvgLength = mycontour.computeAvgLength() ;
mycontour.putCgRow(numImgRow/2 + dy ) ;
mycontour.putCgCol(numImgCol/2 + dx ) ;
/* local deformation */
if( deformNoiseStd > 0) {
float noise_x, noise_y ;
for(sptr = mycontour.getHead(); sptr; sptr=sptr->getNext()) {
rand_gausspair(0.0, deformNoiseStd*AvgLength,
&noise_x, &noise_y) ;
sptr->putRow(sptr->getRow() + noise_y) ;
sptr->putCol(sptr->getCol() + noise_x) ;
}
}
/* fill image values inside contour */
for(sptr = mycontour.getHead(); sptr; sptr=sptr->getNext()) {
SNAXEL *next = sptr->getNext(mycontour.getMode(),
mycontour.getHead()) ;
double dy = next->getRow()-sptr->getRow(),
dx = next->getCol()-sptr->getCol() ;
int numInsert = (int)(MAX(fabs(dx), fabs(dy))+2) ;
dx /= numInsert ; dy /= numInsert ;
for(k=0; k<=numInsert; k++) {
double x = sptr->getCol() + dx*k ;
double y = sptr->getRow() + dy*k ;
int numDraw = ROUNDOFF(MAX(fabs(x), fabs(y))) ;
x /= numDraw ; y /= numDraw ;
for(j=0; j<=numDraw; j++)
myImage.put(
ROUNDOFF(j*y+mycontour.getCgRow()),
ROUNDOFF(j*x+mycontour.getCgCol()),
1) ;
}
}
/* handle isolated points */
for(i=1; i<myImage.getRow()-1; i++)
for(j=1; j<myImage.getCol()-1; j++)
if( myImage.get(i-1, j) && myImage.get(i+1, j) &&
myImage.get(i, j-1) && myImage.get(i, j+1) )
myImage.put(i, j, 1) ;
/* put in image noise */
for(i=0; i<myImage.getRow(); i++)
for(j=0; j<myImage.getCol(); j++)
myImage.put(i, j, rand_gaussian(0.0, imgNoiseStd) +
((myImage.get(i, j)) ? whitePixel : blackPixel)) ;
myImage.write(imgFileName, _ras);
if(verbose) {
myImage.show() ;
getchar() ;
}
}
void printhelp(char *progname)
{
printf("\n\nImage Generation based on Contour");
printf("\n Usage : %s -<option><value>", progname);
printf("\n\tH : print this help screen");
printf("\n\tV : verbose mode");
printf("\n Contour Specification :");
printf("\n\tC/circle/rect/manual/:filename : source of contour");
printf("\n\tMx1 : magnification factor in X dimension");
printf("\n\tMy1 : magnification factor in Y dimension");
printf("\n\tdx0 : displacement in X dimension");
printf("\n\tdy0 : displacement in Y dimension");
printf("\n\tR0 : rotation in degree");
printf("\n\tDx0 : dilation in X dimension");
printf("\n\tDy0 : dilation in Y dimension");
printf("\n\tN8 : Number of snaxels (for circles and rectangles)");
printf("\n\tX0 : standard deviation of local deformation");
printf("\n Image Specification :");
printf("\n\tFtest : output image name");
printf("\n\tr129 : number of image rows") ;
printf("\n\tc129 : number of image columns");
printf("\n\tB50 : black pixel value");
printf("\n\tW150 : white pixel value");
printf("\n\tS0 : standard deviation of image gaussian noise");
printf("\n");
}
void getparam(int ac, char** av, int *contourMode, char *contourFName,
double *Mx, double *My, double *dx, double *dy, double *RotateAngle,
double *Dx, double *Dy, int *numSnaxel, double *deformNoiseStd,
char *imgFileName, int *numImgRow, int *numImgCol, int *blackPixel,
int *whitePixel, double *imgNoiseStd, int *verbose)
{
register int i;
char *inputstr;
for (i=1; i<ac; i++)
{
if( *av[i] != '-' )
continue; /* illegal input */
inputstr = av[i]+2;
switch( *(av[i]+1) ) {
case 'H' :
printhelp(av[0]) ;
exit(0) ;
case 'C' :
if(*inputstr == ':') {
*contourMode = _File ;
strcpy(contourFName, inputstr + 1) ;
}
else if (!strncmp(inputstr, "circle", 3))
*contourMode = _Circle ;
else if (!strncmp(inputstr, "rect", 3))
*contourMode = _Rectangle ;
else if (!strncmp(inputstr, "manual", 3))
*contourMode = _Manual ;
break;
case 'M' :
inputstr=av[i]+3;
switch (*(av[i]+2)) {
case 'x': *Mx = atof(inputstr); break;
case 'y': *My = atof(inputstr);break;
};
break;
case 'd' :
inputstr=av[i]+3;
switch (*(av[i]+2)) {
case 'x': *dx = atof(inputstr); break;
case 'y': *dy = atof(inputstr);break;
};
break;
case 'R' :
*RotateAngle=atof(inputstr);
break;
case 'D' :
inputstr=av[i]+3;
switch (*(av[i]+2)) {
case 'x': *Dx = atof(inputstr); break;
case 'y': *Dy = atof(inputstr);break;
};
break;
case 'N' :
*numSnaxel=atoi(inputstr);
break;
case 'X' :
*deformNoiseStd=atof(inputstr);
break;
case 'F' :
strcpy(imgFileName, inputstr) ;
break;
case 'r' :
*numImgRow=atoi(inputstr);
break;
case 'c' :
*numImgCol=atoi(inputstr);
break;
case 'B' :
*blackPixel=atoi(inputstr);
break;
case 'W' :
*whitePixel=atoi(inputstr);
break;
case 'S' :
*imgNoiseStd=atof(inputstr);
break;
case 'V' :
*verbose = atoi(inputstr) ;
break ;
default: break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -