35 return cvSize(image->width, image->height);
40 void mcvSmooth(IplImage * image, IplImage * smoothed_image,
int smooth_type,
int aperture)
42 if (smooth_type != CV_GAUSSIAN)
44 cerr <<
"mcvSmooth: only for CV_GAUSSIAN" << endl;
48 IplImage * image_to_smooth = image;
53 cvSmooth(image_to_smooth, smoothed_image, CV_GAUSSIAN, 7);
54 image_to_smooth = smoothed_image;
57 else if (aperture >= 5)
59 cvSmooth(image_to_smooth, smoothed_image, CV_GAUSSIAN, 5);
60 image_to_smooth = smoothed_image;
65 cvSmooth(image_to_smooth, smoothed_image, CV_GAUSSIAN, 3);
66 image_to_smooth = smoothed_image;
74 int half_size = int(3 * sqrt(2.) * sigma + 0.5);
78 for(
int i = -half_size; i <= +half_size; i++)
79 for(
int j = -half_size; j <= +half_size; j++)
82 float e = exp(-(i*i+j*j) / (2 * sigma * sigma));
84 if (order_x == 2 && order_y == 0)
85 c = (i * i / (sigma * sigma * sigma * sigma) - 1 / (sigma * sigma)) * e;
86 else if (order_x == 0 && order_y == 2)
87 c = (j * j / (sigma * sigma * sigma * sigma) - 1 / (sigma * sigma)) * e;
88 else if (order_x == 1 && order_y == 1)
89 c = i * j / (sigma * sigma * sigma * sigma) * e;
93 cerr <<
"error when calling mcvGaussianDerivative" << endl;
96 int nx = x + i, ny = y + j;
97 if (nx < 0 || nx >= image->width || ny < 0 || ny >= image->height)
101 if (image->depth ==
int(IPL_DEPTH_8U))
102 result += c *
mcvGet2D(image, nx, ny,
unsigned char);
103 else if (image->depth ==
int(IPL_DEPTH_16S))
104 result += c *
mcvGet2D(image, nx, ny,
short);
105 else if (image->depth ==
int(IPL_DEPTH_32F))
106 result += c *
mcvGet2D(image, nx, ny,
float);
108 cerr <<
"mcvGaussianDerivative: wrong image format." << endl;
117 IplImage * image_32f = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_32F, 1);
118 cvCvtScale(image, image_32f);
120 IplImage * image_32f_x = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_32F, 1);
121 IplImage * image_32f_y = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_32F, 1);
123 cvSobel(image_32f, image_32f_x, 1, 0, aperture);
124 cvSobel(image_32f, image_32f_y, 1, 0, aperture);
126 IplImage * result_32f = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_32F, 1);
127 cvMul(image_32f_x, image_32f_x, image_32f_x);
128 cvMul(image_32f_y, image_32f_y, image_32f_y);
129 cvAdd(image_32f_x, image_32f_y, result_32f);
131 IplImage * result = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_8U, 1);
135 cvReleaseImage(&image_32f);
136 cvReleaseImage(&image_32f_x);
137 cvReleaseImage(&image_32f_y);
138 cvReleaseImage(&result_32f);
145 if (image->depth ==
int(IPL_DEPTH_16S))
147 for(
int y = 0; y < image->height; y++)
148 for(
int x = 0; x < image->width; x++)
149 if (x < border || y < border || x >= image->width - border || y >= image->height - border)
150 mcvGet2D(image, x, y,
short) = short(value);
152 else if (image->depth ==
int(IPL_DEPTH_32F))
154 for(
int y = 0; y < image->height; y++)
155 for(
int x = 0; x < image->width; x++)
156 if (x < border || y < border || x >= image->width - border || y >= image->height - border)
157 mcvGet2D(image, x, y,
float) = float(value);
160 cerr <<
"mcvSetBorder: wrong image format." << endl;
165 if (image->depth != IPL_DEPTH_32F)
167 cerr <<
"Error when calling mcvAddBorder: image should be 32F" << endl;
173 for(
int j = border_size + 1; j < image->height - border_size - 1; j++)
175 float * row =
mcvRow(image, j,
float);
176 for(
int i = border_size + 1; i < image->width - border_size - 1; i++)
178 sum += double(row[i]);
182 float mean = float(sum /
double(N));
183 for(
int j = 0; j < image->height; j++)
185 float * row =
mcvRow(image, j,
float);
186 for(
int i = 0; i < image->width; i++)
188 if (i <= border_size || i >= image->width - border_size - 1 ||
189 j <= border_size || j >= image->height - border_size - 1)
199 IplImage * result = cvCreateImage(cvSize(colorImage->width, colorImage->height), IPL_DEPTH_8U, 1);
201 cvCvtColor(colorImage, result, CV_RGB2GRAY);
208 IplImage * result = cvCreateImage(cvSize(grayImage->width, grayImage->height), IPL_DEPTH_8U, 3);
210 if (grayImage->depth == IPL_DEPTH_32F)
212 IplImage * tempImage;
214 double min = 255, max = 0;
216 cvMinMaxLoc(grayImage, &min, &max, &Pmin, &Pmax);
217 tempImage = cvCreateImage(cvSize(grayImage->width, grayImage->height), IPL_DEPTH_8U, 1);
218 cvConvertScale(grayImage, tempImage, 255. / (max - min), -min * 255 / (max - min));
219 cvCvtColor(tempImage, result, CV_GRAY2RGB);
220 cvReleaseImage(&tempImage);
223 cvCvtColor(grayImage, result, CV_GRAY2RGB);
228 void mcvHSV2RGB(
float H,
float S,
float V,
int & R,
int & G,
int & B)
232 R = int( V * 255.f );
233 G = int( V * 255.f );
234 B = int( V * 255.f );
239 if ( var_h >= 6 ) var_h -= 6;
240 int var_i = int( var_h );
241 float var_1 = V * ( 1 -
S );
242 float var_2 = V * ( 1 - S * ( var_h - var_i ) );
243 float var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) );
245 float var_r, var_g, var_b;
246 if ( var_i == 0 ) { var_r = V ; var_g = var_3 ; var_b = var_1; }
247 else if ( var_i == 1 ) { var_r = var_2 ; var_g = V ; var_b = var_1; }
248 else if ( var_i == 2 ) { var_r = var_1 ; var_g = V ; var_b = var_3; }
249 else if ( var_i == 3 ) { var_r = var_1 ; var_g = var_2 ; var_b = V; }
250 else if ( var_i == 4 ) { var_r = var_3 ; var_g = var_1 ; var_b = V; }
251 else { var_r = V ; var_g = var_1 ; var_b = var_2; }
253 R = int( var_r * 255.f );
254 G = int( var_g * 255.f );
255 B = int( var_b * 255.f );
261 if (floatImage->depth != IPL_DEPTH_32F)
264 IplImage * result = cvCreateImage(cvSize(floatImage->width, floatImage->height),
266 double min = 255, max = 0;
268 cvMinMaxLoc(floatImage, &min, &max, &Pmin, &Pmax);
270 for(
int y = 0; y < result->height; y++)
272 float * row =
mcvRow(floatImage, y,
float);
273 unsigned char * rowH =
mcvRow(result, y,
unsigned char);
274 for(
int x = 0; x < result->width; x++)
276 bool use_color =
true;
277 if (x < result->width - 1 && y < result->height - 1)
279 int l1 = int( ((log(row[x]) - log(min)) / (log(max) - log(min))) * curve_number );
280 int l2 = int( ((log(row[x + 1]) - log(min)) / (log(max) - log(min))) * curve_number );
281 int l3 = int( ((log(row[x + floatImage->width]) - log(min)) / (log(max) - log(min))) * curve_number );
282 if (l1 != l2 || l1 != l3)
289 mcvHSV2RGB(
float( (row[x] - min) / (max - min) ), 1.f, 1.f, r, g, b);
290 rowH[3 * x] = (
unsigned char)(r);
291 rowH[3 * x + 1] = (
unsigned char)(g);
292 rowH[3 * x + 2] = (
unsigned char)(b);
308 if (floatImage->depth != IPL_DEPTH_32F)
311 IplImage * result = cvCreateImage(cvSize(floatImage->width, floatImage->height),
313 double min = 255, max = 0;
315 cvMinMaxLoc(floatImage, &min, &max, &Pmin, &Pmax);
317 for(
int y = 0; y < result->height; y++)
319 float * row =
mcvRow(floatImage, y,
float);
320 unsigned char * rowH =
mcvRow(result, y,
unsigned char);
321 for(
int x = 0; x < result->width; x++)
323 bool use_gray =
true;
324 if (x < result->width - 1 && y < result->height - 1)
326 int l1 = int( (row[x] - min) / (max - min) * curve_number );
327 int l2 = int( (row[x + 1] - min) / (max - min) * curve_number );
328 int l3 = int( (row[x + floatImage->width] - min) / (max - min) * curve_number );
329 if (l1 != l2 || l1 != l3)
335 rowH[3 * x] = (
unsigned char)( 255. * (row[x] - min) / (max - min) );
336 rowH[3 * x + 1] = (
unsigned char)( 255. * (row[x] - min) / (max - min) );
337 rowH[3 * x + 2] = (
unsigned char)( 255. * (row[x] - min) / (max - min) );
343 rowH[3 * x + 2] = 255;
355 int dy = image->widthStep /
sizeof(float);
357 for(
int y = 1; y < image->height - 1; y++)
359 float * row =
mcvRow(image, y,
float);
360 unsigned char * row_r =
mcvRow(result, y,
unsigned char);
361 for(
int x = 1; x < image->width - 1; x++)
363 if (row[x] < row[x - 1] && row[x] < row[x + 1] &&
364 row[x] < row[x - dy] && row[x] < row[x + dy] &&
365 row[x] < row[x - dy - 1] && row[x] < row[x + dy - 1] &&
366 row[x] < row[x - dy + 1] && row[x] < row[x + dy + 1])
369 row_r[3 * x + 1] = 0;
370 row_r[3 * x + 2] = 255;
382 int dy = image->widthStep /
sizeof(float);
384 for(
int y = 1; y < image->height - 1; y++)
386 float * row =
mcvRow(image, y,
float);
387 unsigned char * row_r =
mcvRow(result, y,
unsigned char);
388 for(
int x = 1; x < image->width - 1; x++)
390 if (row[x] > row[x - 1] && row[x] > row[x + 1] &&
391 row[x] > row[x - dy] && row[x] > row[x + dy] &&
392 row[x] > row[x - dy - 1] && row[x] > row[x + dy - 1] &&
393 row[x] > row[x - dy + 1] && row[x] > row[x + dy + 1])
396 row_r[3 * x + 1] = 0;
397 row_r[3 * x + 2] = 255;
407 for(
int l = 0; l < image->height; l++)
409 unsigned char * line = (
unsigned char *)image->imageData + l * image->widthStep;
411 for(
int c = 0; c < image->nChannels * image->width; c += 3)
414 line[c] = line[c + 2];
422 double min = 255, max = 0;
425 cvMinMaxLoc(original, &min, &max, &Pmin, &Pmax);
426 cvConvertScale(original, scaled, 255. / (max - min), -min * 255 / (max - min));
431 int mcvSaveImage(
const char * filename, IplImage * image,
bool verbose)
434 cout <<
"(saving " << filename <<
"..." << flush;
438 if (image->depth == IPL_DEPTH_8U)
439 result = cvSaveImage(filename, image);
442 IplImage * tempImage;
444 double min = 255, max = 0;
446 cvMinMaxLoc(image, &min, &max, &Pmin, &Pmax);
449 cout <<
"[" << min <<
" : " << max <<
"] " << flush;
451 tempImage = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_8U, 1);
452 cvConvertScale(image, tempImage, 255. / (max - min), -min * 255 / (max - min));
454 result = cvSaveImage(filename, tempImage);
456 cvReleaseImage(&tempImage);
459 if (verbose && !result)
460 cout <<
"ERROR !" << endl;
462 if (verbose && result)
463 cout <<
"ok)" << endl;
468 int mcvSaveImage(
const char * generic_filename,
int index, IplImage * image,
bool verbose)
472 sprintf(filename, generic_filename, index);
481 return cvCreateImage(cvSize(image->width, image->height), image->depth, image->nChannels);
487 cout <<
"(loading " << filename <<
"..." << flush;
489 IplImage * result = cvLoadImage(filename, code);
491 if (verbose && !result)
492 cout <<
"ERROR !" << endl;
494 if (verbose && result)
495 cout <<
"ok)" << endl;
500 IplImage *
mcvLoadImage(
const char * generic_filename,
int index,
int code,
bool verbose)
504 sprintf(filename, generic_filename, index);
511 void mcvSquare(IplImage * image,
int x,
int y,
int size, CvScalar color,
int width)
513 cvLine(image,
cvPoint(x - size / 2, y - size / 2),
cvPoint(x + size / 2, y - size / 2), color, width);
514 cvLine(image,
cvPoint(x + size / 2, y - size / 2),
cvPoint(x + size / 2, y + size / 2), color, width);
515 cvLine(image,
cvPoint(x + size / 2, y + size / 2),
cvPoint(x - size / 2, y + size / 2), color, width);
516 cvLine(image,
cvPoint(x - size / 2, y + size / 2),
cvPoint(x - size / 2, y - size / 2), color, width);
519 void mcvX(IplImage * image,
int x,
int y,
int size, CvScalar color,
int width)
521 cvLine(image,
cvPoint(x - size / 2, y - size / 2),
cvPoint(x + size / 2, y + size / 2), color, width);
522 cvLine(image,
cvPoint(x + size / 2, y - size / 2),
cvPoint(x - size / 2, y + size / 2), color, width);
525 void mcvCross(IplImage * image,
int x,
int y,
int size, CvScalar color,
int width)
527 cvLine(image,
cvPoint(x - size / 2, y),
cvPoint(x + size / 2, y), color, width);
528 cvLine(image,
cvPoint(x, y - size / 2),
cvPoint(x, y + size / 2), color, width);
531 void mcvCircle(IplImage * image,
int x,
int y,
int size, CvScalar color,
int thickness)
533 cvCircle(image,
cvPoint(x, y), size / 2, color, thickness);
536 void mcvVisibleLine(IplImage * image,
int x1,
int y1,
int x2,
int y2,
int thickness)
538 cvLine(image,
cvPoint(x1, y1),
cvPoint(x2, y2), cvScalar(0, 0, 0), thickness + 2);
539 cvLine(image,
cvPoint(x1, y1),
cvPoint(x2, y2), cvScalar(255, 255, 255), thickness);
546 IplImage * result = cvCreateImage(size, depth, nChannels);
549 for(
int l = 0; l < size.height; l++)
551 unsigned char * line = (
unsigned char *)result->imageData + l * result->widthStep;
552 for(
int c = 0; c < nChannels * size.width; c++)
553 line[c] = (
unsigned char)(
rand() % 256);
556 for(
int l = 0; l < size.height; l++)
558 unsigned char * line = (
unsigned char *)result->imageData + l * result->widthStep;
559 for(
int c = 0; c < nChannels * size.width; c += 3)
560 line[c] = line[c + 1] = line[c + 2] = (
unsigned char)(
rand() % 256);
566 void mcvReplace(IplImage * image,
int old_value,
int new_value)
568 for(
int l = 0; l < image->height; l++)
570 unsigned char * line =
mcvRow(image, l,
unsigned char);
572 for(
int c = 0; c < image->width; c++)
573 if (
int(line[c]) == old_value)
574 line[c] = (
unsigned char)new_value;
580 for(
int l = 0; l < image->height; l++)
582 unsigned char * line =
mcvRow(image, l,
unsigned char);
584 for(
int c = 0; c < image->width; c++)
585 if (
int(line[c]) == value)
586 line[c] = (
unsigned char)(
rand() % 256);
592 int deltaNoise = maxNoise - minNoise;
594 for(
int y = 0; y < image->height; y++)
596 unsigned char * line =
mcvRow(image, y,
unsigned char);
598 for(
int x = 0; x < image->width; x++)
601 int noise =
rand() % (2 * deltaNoise + 1) - deltaNoise;
609 if (p > 255) p = 255;
612 line[x] = (
unsigned char)p;
619 for(
int y = 0; y < image->height; y++)
621 unsigned char * line = (
unsigned char *)(image->imageData + y * image->widthStep);
623 for(
int x = 0; x < image->width; x++)
627 p +=
rand() % (2 * maxNoise + 1) - maxNoise;
634 line[x] = (
unsigned char)p;
642 for(
int y = 0; y < image->height; y++)
644 unsigned char * line = (
unsigned char *)(image->imageData + y * image->widthStep);
646 for(
int x = 0; x < image->width; x++)
648 int I = int(255 * pow(line[x] / 255.0f, gamma));
650 if (I > 256) I = 255;
651 line[x] = (
unsigned char)I;
658 void mcvCrop(IplImage * image,
int x,
int y, IplImage * croppedImage)
660 IplROI * originalROI = image->roi;
661 int width = croppedImage->width;
662 int height = croppedImage->height;
664 if (x < 0 || y < 0)
return;
665 if (x + width >= image->width || y + height >= image->height)
return;
676 cvCopy(image, croppedImage);
677 image->roi = originalROI;
680 IplImage *
mcvCrop(IplImage * image,
int x,
int y,
int width,
int height)
682 IplImage * result = cvCreateImage(cvSize(width, height), image->depth, image->nChannels);
691 IplImage *
mcvResize(IplImage * original_image,
int new_width,
int new_height)
694 new_height = int(
float(new_width * original_image->height) / original_image->width);
695 IplImage * result = cvCreateImage(cvSize(new_width, new_height), original_image->depth, original_image->nChannels);
696 cvResize(original_image, result, CV_INTER_LINEAR);
700 IplImage *
mcvZoom(IplImage * source,
int xc,
int yc,
float zoom)
702 IplImage * temp = cvCreateImage(cvSize(
int(source->width * zoom),
int(source->height * zoom)), IPL_DEPTH_8U, 1);
703 IplImage * result = cvCreateImage(cvSize(source->width, source->height), IPL_DEPTH_8U, 1);
705 cvResize(source, temp, CV_INTER_LINEAR);
707 int nxc = int(xc * zoom);
708 int nyc = int(yc * zoom);
709 roi = cvRect(nxc - source->width / 2, nyc - source->height / 2, source->width, source->height);
710 cvSetImageROI(temp, roi);
711 cvCopy(temp, result);
712 cvReleaseImage(&temp);
717 void mcvPut(IplImage * destImage, IplImage * imageToCopy,
int x,
int y)
722 roi.width = imageToCopy->width;
723 roi.height = imageToCopy->height;
726 IplROI * tempRoi = destImage->roi;
728 destImage->roi = &roi;
730 if (imageToCopy->nChannels == 1 && destImage->nChannels == 3)
733 cvCopy(colorImageToCopy, destImage);
734 cvReleaseImage(&colorImageToCopy);
737 cvCopy(imageToCopy, destImage);
739 destImage->roi = tempRoi;
744 unsigned char * line = (
unsigned char *)(image->imageData + image->widthStep);
745 int width = image->nChannels * image->width;
747 for(
int y = 1; y < image->height; y += 2)
749 unsigned char * previous = line - image->widthStep;
750 unsigned char * next = line + image->widthStep;
752 for(
int x = 0; x < width; x++)
753 line[x] = (
unsigned char)((int(previous[x]) + int(next[x])) >> 1);
755 line += 2 * image->widthStep;
761 IplImage *
mcvGetPatch(IplImage * image,
int u,
int v,
int width,
int height)
763 return mcvCrop(image, u - width / 2, v - height / 2, width, height);
766 void mcvGetPatch(IplImage * image, IplImage * patch,
int u,
int v)
768 mcvCrop(image, u - patch->width / 2, v - patch->height / 2, patch);
773 Rz[0][0] = cos(angle); Rz[0][1] = -sin(angle); Rz[0][2] = 0.;
774 Rz[1][0] = sin(angle); Rz[1][1] = cos(angle); Rz[1][2] = 0.;
775 Rz[2][0] = 0.; Rz[2][1] = 0.; Rz[2][2] = 1.;
778 void imcvDiag3(
double D[3][3],
double d1,
double d2,
double d3)
780 D[0][0] = d1; D[0][1] = 0.; D[0][2] = 0.;
781 D[1][0] = 0.; D[1][1] = d2; D[1][2] = 0.;
782 D[2][0] = 0.; D[2][1] = 0.; D[2][2] = d3;
787 T[0][0] = 1.; T[0][1] = 0.; T[0][2] = tx;
788 T[1][0] = 0.; T[1][1] = 1.; T[1][2] = ty;
789 T[2][0] = 0.; T[2][1] = 0.; T[2][2] = 1.;
796 for(
int i = 0; i < 3; i++)
797 for(
int j = 0; j < 3; j++)
800 for(
int k = 0; k < 3; k++)
801 copy[i][j] += A[i][k] * B[k][j];
803 for(
int i = 0; i < 3; i++)
804 for(
int j = 0; j < 3; j++)
805 AB[i][j] = copy[i][j];
812 for(
int i = 0; i < 3; i++)
813 for(
int j = 0; j < 3; j++)
816 for(
int k = 0; k < 3; k++)
817 copy[i][j] += A[i][k] * B[j][k];
819 for(
int i = 0; i < 3; i++)
820 for(
int j = 0; j < 3; j++)
821 ABt[i][j] = copy[i][j];
843 float theta,
float phi,
844 float lambda1,
float lambda2,
857 imcvDiag3(K, 1. / lambda1, 1. / lambda2, 1);
863 a[0] = (float)A[0][0];
864 a[1] = (float)A[0][1];
865 a[2] = (float)u + (
float)A[0][2];
866 a[3] = (float)A[1][0];
867 a[4] = (float)A[1][1];
868 a[5] = (float)v + (
float)A[1][2];
880 case 0:
return CV_RGB(255 * coeff, 0, 0);
881 case 1:
return CV_RGB(0, 255 * coeff, 0);
882 case 2:
return CV_RGB(0, 0, 255 * coeff);
884 case 3:
return CV_RGB(255 * coeff, 255 * coeff, 0);
885 case 4:
return CV_RGB(0, 255 * coeff, 255 * coeff);
886 case 5:
return CV_RGB(255 * coeff, 0, 255 * coeff);
888 default:
return CV_RGB(128, 128, 128);