1
0
Fork 0
mirror of https://github.com/ruby-opencv/ruby-opencv synced 2023-03-27 23:22:12 -04:00

uniformed code format

This commit is contained in:
ser1zw 2011-08-12 02:26:54 +09:00
parent 2ac67221f9
commit 42c6f5cc37
30 changed files with 144 additions and 141 deletions

View file

@ -272,25 +272,29 @@ rb_smoothness(int argc, VALUE *argv, VALUE self)
double outLowDensity, outHighDensity;
if (TYPE(lowFreqRatio) == T_NIL) {
f_lowFreqRatio = 10 / 128.0f;
} else {
}
else {
Check_Type(lowFreqRatio, T_FLOAT);
f_lowFreqRatio = NUM2DBL(lowFreqRatio);
}
if (TYPE(blankDensity) == T_NIL) {
f_blankDensity = 1.2f;
} else {
}
else {
Check_Type(blankDensity, T_FLOAT);
f_blankDensity = NUM2DBL(blankDensity);
}
if (TYPE(messyDensity) == T_NIL) {
f_messyDensity = 0.151f;
} else {
}
else {
Check_Type(messyDensity, T_FLOAT);
f_messyDensity = NUM2DBL(messyDensity);
}
if (TYPE(highFreqRatio) == T_NIL) {
f_highFreqRatio = 5 / 128.0f;
} else {
}
else {
Check_Type(highFreqRatio, T_FLOAT);
f_highFreqRatio = NUM2DBL(highFreqRatio);
}
@ -300,30 +304,31 @@ rb_smoothness(int argc, VALUE *argv, VALUE self)
// the image is required to be in depth of 64
if (IPLIMAGE(self)->depth == 64) {
p64DepthImage = NULL;
pFourierImage = create_fourier_image(IPLIMAGE(self));
} else {
p64DepthImage = rb_cvCreateImage(cvGetSize(IPLIMAGE(self)), IPL_DEPTH_64F, 1);
cvConvertScale(CVARR(self), p64DepthImage, 1.0, 0.0);
pFourierImage = create_fourier_image(p64DepthImage);
p64DepthImage = NULL;
pFourierImage = create_fourier_image(IPLIMAGE(self));
}
else {
p64DepthImage = rb_cvCreateImage(cvGetSize(IPLIMAGE(self)), IPL_DEPTH_64F, 1);
cvConvertScale(CVARR(self), p64DepthImage, 1.0, 0.0);
pFourierImage = create_fourier_image(p64DepthImage);
}
Smoothness result = compute_smoothness(pFourierImage, f_lowFreqRatio, f_blankDensity, f_messyDensity, f_highFreqRatio, outLowDensity, outHighDensity);
Smoothness result = compute_smoothness(pFourierImage, f_lowFreqRatio, f_blankDensity, f_messyDnsity,
f_highFreqRatio, outLowDensity, outHighDensity);
cvReleaseImage(&pFourierImage);
if (p64DepthImage != NULL)
cvReleaseImage(&p64DepthImage);
cvReleaseImage(&p64DepthImage);
switch(result)
{
case SMOOTH:
return rb_ary_new3(3, ID2SYM(rb_intern("smooth")), rb_float_new(outLowDensity), rb_float_new(outHighDensity));
case MESSY:
return rb_ary_new3(3, ID2SYM(rb_intern("messy")), rb_float_new(outLowDensity), rb_float_new(outHighDensity));
case BLANK:
return rb_ary_new3(3, ID2SYM(rb_intern("blank")), rb_float_new(outLowDensity), rb_float_new(outHighDensity));
default:
return rb_ary_new3(3, NULL, rb_float_new(outLowDensity), rb_float_new(outHighDensity));
switch(result) {
case SMOOTH:
return rb_ary_new3(3, ID2SYM(rb_intern("smooth")), rb_float_new(outLowDensity), rb_float_new(outHighDensity));
case MESSY:
return rb_ary_new3(3, ID2SYM(rb_intern("messy")), rb_float_new(outLowDensity), rb_float_new(outHighDensity));
case BLANK:
return rb_ary_new3(3, ID2SYM(rb_intern("blank")), rb_float_new(outLowDensity), rb_float_new(outHighDensity));
default:
return rb_ary_new3(3, NULL, rb_float_new(outLowDensity), rb_float_new(outHighDensity));
}
}
@ -333,7 +338,9 @@ rb_smoothness(int argc, VALUE *argv, VALUE self)
* else -> good;
*/
Smoothness
compute_smoothness(const IplImage *pFourierImage, const double lowFreqRatio, const double blankDensity, const double messyDensity, const double highFreqRatio, double &outLowDensity, double &outHighDensity)
compute_smoothness(const IplImage *pFourierImage, const double lowFreqRatio, const double blankDensity,
const double messyDensity, const double highFreqRatio, double &outLowDensity
double &outHighDensity)
{
int low, high;
IplImage *filteredFourierImage;
@ -341,10 +348,11 @@ compute_smoothness(const IplImage *pFourierImage, const double lowFreqRatio, con
double sum, den, totalArea;
CvScalar scalar;
if(! (pFourierImage->nChannels == 1 && pFourierImage->depth == 64) ) {
cvError( CV_StsUnmatchedSizes, "compute_smoothness", "input image must contain only 1 channel and a depth of 64", __FILE__, __LINE__ );
if (!(pFourierImage->nChannels == 1 && pFourierImage->depth == 64) ) {
cvError(CV_StsUnmatchedSizes, "compute_smoothness", "input image must contain only 1 channel and a depth of 64",
__FILE__, __LINE__ );
}
high_pass_range(pFourierImage, lowFreqRatio, low, high );
totalArea = M_PI * (high * high - low * low);
@ -354,12 +362,11 @@ compute_smoothness(const IplImage *pFourierImage, const double lowFreqRatio, con
cvReleaseImage(&filteredFourierImage);
outLowDensity = den = totalIntensity / totalArea;
if(den <= blankDensity)
{
if (den <= blankDensity) {
return BLANK;
}
low = (int) (high * (1.0 - highFreqRatio));
low = (int)(high * (1.0 - highFreqRatio));
filteredFourierImage = create_frequency_filtered_image(pFourierImage, low, high);
scalar = cvSum(filteredFourierImage);
@ -367,8 +374,7 @@ compute_smoothness(const IplImage *pFourierImage, const double lowFreqRatio, con
cvReleaseImage(&filteredFourierImage);
outHighDensity = den = totalIntensity / totalArea;
if(den >= messyDensity)
{
if (den >= messyDensity) {
return MESSY;
}
@ -393,37 +399,39 @@ cvShiftDFT(CvArr *src_arr, CvArr *dst_arr )
CvSize dst_size = cvGetSize(dst_arr);
int cx, cy;
if(dst_size.width != size.width ||
dst_size.height != size.height){
cvError( CV_StsUnmatchedSizes, "cvShiftDFT", "Source and Destination arrays must have equal sizes", __FILE__, __LINE__ );
if (dst_size.width != size.width ||
dst_size.height != size.height) {
cvError( CV_StsUnmatchedSizes, "cvShiftDFT", "Source and Destination arrays must have equal sizes",
__FILE__, __LINE__ );
}
if(src_arr==dst_arr){
tmp = rb_cvCreateMat(size.height/2, size.width/2, cvGetElemType(src_arr));
if (src_arr == dst_arr) {
tmp = rb_cvCreateMat(size.height / 2, size.width / 2, cvGetElemType(src_arr));
}
cx = size.width / 2;
cy = size.height / 2; // image center
cx = size.width/2;
cy = size.height/2; // image center
q1 = cvGetSubRect(src_arr, &q1stub, cvRect(0,0,cx, cy));
q2 = cvGetSubRect(src_arr, &q2stub, cvRect(cx,0,cx,cy));
q3 = cvGetSubRect(src_arr, &q3stub, cvRect(cx,cy,cx,cy));
q4 = cvGetSubRect(src_arr, &q4stub, cvRect(0,cy,cx,cy));
d1 = cvGetSubRect(src_arr, &d1stub, cvRect(0,0,cx,cy));
d2 = cvGetSubRect(src_arr, &d2stub, cvRect(cx,0,cx,cy));
d3 = cvGetSubRect(src_arr, &d3stub, cvRect(cx,cy,cx,cy));
d4 = cvGetSubRect(src_arr, &d4stub, cvRect(0,cy,cx,cy));
q1 = cvGetSubRect( src_arr, &q1stub, cvRect(0,0,cx, cy) );
q2 = cvGetSubRect( src_arr, &q2stub, cvRect(cx,0,cx,cy) );
q3 = cvGetSubRect( src_arr, &q3stub, cvRect(cx,cy,cx,cy) );
q4 = cvGetSubRect( src_arr, &q4stub, cvRect(0,cy,cx,cy) );
d1 = cvGetSubRect( src_arr, &d1stub, cvRect(0,0,cx,cy) );
d2 = cvGetSubRect( src_arr, &d2stub, cvRect(cx,0,cx,cy) );
d3 = cvGetSubRect( src_arr, &d3stub, cvRect(cx,cy,cx,cy) );
d4 = cvGetSubRect( src_arr, &d4stub, cvRect(0,cy,cx,cy) );
if(src_arr!=dst_arr){
if( !CV_ARE_TYPES_EQ( q1, d1 )){
cvError( CV_StsUnmatchedFormats, "cvShiftDFT", "Source and Destination arrays must have the same format", __FILE__, __LINE__ );
if (src_arr != dst_arr) {
if (!CV_ARE_TYPES_EQ(q1, d1)) {
cvError(CV_StsUnmatchedFormats, "cvShiftDFT", "Source and Destination arrays must have the same format",
__FILE__, __LINE__ );
}
cvCopy(q3, d1, 0);
cvCopy(q4, d2, 0);
cvCopy(q1, d3, 0);
cvCopy(q2, d4, 0);
}
else{
else {
cvCopy(q3, tmp, 0);
cvCopy(q1, q3, 0);
cvCopy(tmp, q1, 0);
@ -432,16 +440,14 @@ cvShiftDFT(CvArr *src_arr, CvArr *dst_arr )
cvCopy(tmp, q2, 0);
}
if (tmp != NULL)
{
cvReleaseMat(&tmp);
if (tmp != NULL) {
cvReleaseMat(&tmp);
}
}
IplImage*
create_fourier_image(const IplImage *im)
{
IplImage *realInput;
IplImage *imaginaryInput;
IplImage *complexInput;
@ -450,51 +456,50 @@ create_fourier_image(const IplImage *im)
IplImage *image_Re;
IplImage *image_Im;
realInput = rb_cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
imaginaryInput = rb_cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
complexInput = rb_cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 2);
realInput = rb_cvCreateImage(cvGetSize(im), IPL_DEPTH_64F, 1);
imaginaryInput = rb_cvCreateImage(cvGetSize(im), IPL_DEPTH_64F, 1);
complexInput = rb_cvCreateImage(cvGetSize(im), IPL_DEPTH_64F, 2);
cvScale(im, realInput, 1.0, 0.0);
cvZero(imaginaryInput);
cvMerge(realInput, imaginaryInput, NULL, NULL, complexInput);
dft_M = cvGetOptimalDFTSize( im->height - 1 );
dft_N = cvGetOptimalDFTSize( im->width - 1 );
dft_M = cvGetOptimalDFTSize(im->height - 1);
dft_N = cvGetOptimalDFTSize(im->width - 1);
dft_A = rb_cvCreateMat( dft_M, dft_N, CV_64FC2 );
image_Re = rb_cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
image_Im = rb_cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
dft_A = rb_cvCreateMat(dft_M, dft_N, CV_64FC2);
image_Re = rb_cvCreateImage(cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
image_Im = rb_cvCreateImage(cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
// copy A to dft_A and pad dft_A with zeros
cvGetSubRect( dft_A, &tmp, cvRect(0,0, im->width, im->height));
cvCopy( complexInput, &tmp, NULL );
if( dft_A->cols > im->width )
{
cvGetSubRect( dft_A, &tmp, cvRect(im->width,0, dft_A->cols - im->width, im->height));
cvZero( &tmp );
cvGetSubRect(dft_A, &tmp, cvRect(0,0, im->width, im->height));
cvCopy(complexInput, &tmp, NULL);
if (dft_A->cols > im->width) {
cvGetSubRect(dft_A, &tmp, cvRect(im->width,0, dft_A->cols - im->width, im->height));
cvZero(&tmp);
}
// no need to pad bottom part of dft_A with zeros because of
// use nonzero_rows parameter in cvDFT() call below
cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput->height );
cvDFT(dft_A, dft_A, CV_DXT_FORWARD, complexInput->height);
// Split Fourier in real and imaginary parts
cvSplit( dft_A, image_Re, image_Im, 0, 0 );
cvSplit(dft_A, image_Re, image_Im, 0, 0);
// Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
cvPow( image_Re, image_Re, 2.0);
cvPow( image_Im, image_Im, 2.0);
cvAdd( image_Re, image_Im, image_Re, NULL);
cvPow( image_Re, image_Re, 0.5 );
cvPow(image_Re, image_Re, 2.0);
cvPow(image_Im, image_Im, 2.0);
cvAdd(image_Re, image_Im, image_Re, NULL);
cvPow(image_Re, image_Re, 0.5);
// Compute log(1 + Mag)
cvAddS( image_Re, cvScalarAll(1.0), image_Re, NULL ); // 1 + Mag
cvLog( image_Re, image_Re ); // log(1 + Mag)
cvAddS(image_Re, cvScalarAll(1.0), image_Re, NULL); // 1 + Mag
cvLog(image_Re, image_Re); // log(1 + Mag)
// Rearrange the quadrants of Fourier image so that the origin is at
// the image center
cvShiftDFT( image_Re, image_Re );
cvShiftDFT(image_Re, image_Re);
cvReleaseImage(&realInput);
cvReleaseImage(&imaginaryInput);
@ -504,7 +509,6 @@ create_fourier_image(const IplImage *im)
cvReleaseMat(&dft_A);
return image_Re;
}
IplImage*
@ -520,18 +524,18 @@ create_frequency_filtered_image(const IplImage *pImage, int low, int high)
box.size.width = high;
box.size.height = high;
IplImage *pFilterMask = rb_cvCreateImage( cvGetSize(pImage), IPL_DEPTH_64F, 1 );
IplImage *pFiltered = rb_cvCreateImage( cvGetSize(pImage), IPL_DEPTH_64F, 1 );
IplImage *pFilterMask = rb_cvCreateImage(cvGetSize(pImage), IPL_DEPTH_64F, 1);
IplImage *pFiltered = rb_cvCreateImage(cvGetSize(pImage), IPL_DEPTH_64F, 1);
cvZero(pFilterMask);
cvZero(pFiltered);
if(high > 0)
if (high > 0)
cvEllipseBox(pFilterMask, box, cvScalar(255, 255, 255, 255), CV_FILLED, 8, 0);
box.size.width = low;
box.size.height = low;
if(low > 0)
if (low > 0)
cvEllipseBox(pFilterMask, box, cvScalar(0, 0, 0, 0), CV_FILLED, 8, 0);
cvAnd(pImage, pFilterMask, pFiltered, NULL);
@ -544,17 +548,15 @@ create_frequency_filtered_image(const IplImage *pImage, int low, int high)
void
high_pass_range(const IplImage *pImage, float lostPercentage, int &outLow, int &outHigh)
{
if(lostPercentage > 1.0f)
{
if (lostPercentage > 1.0f) {
lostPercentage = 1;
}
else if(lostPercentage < 0.0f )
{
else if (lostPercentage < 0.0f) {
lostPercentage = 0;
}
outHigh = (int) MIN( pImage->width, pImage->height );
outLow = (int) (lostPercentage * outHigh);
outHigh = (int)MIN(pImage->width, pImage->height);
outLow = (int)(lostPercentage * outHigh);
}