program cv_Integral;
{KaTeX parse error: Expected 'EOF', got '}' at position 16: APPTYPE CONSOLE}̲ {R *.res}
uses
System.SysUtils,
ocv.highgui_c,
ocv.core_c,
ocv.core.types_c,
ocv.imgproc_c,
ocv.imgproc.types_c,
uResourcePaths;
const
filename = cResourceMedia + ‘cat2.jpg’;
var
SourceImage: pIplImage = nil;
SumImage: pIplImage = nil;
SquareSumImage: pIplImage = nil;
SquareSumImage32S: pIplImage = nil;
TiltedSumImage: pIplImage = nil;
begin
try
// 读取图像
SourceImage := cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
WriteLn(Format(‘[i] 图像: %s’, [filename]));
// 显示原始图像
cvNamedWindow('原始图像', 1);
cvShowImage('原始图像', SourceImage);
// 创建积分图
SumImage := cvCreateImage(cvSize(SourceImage^.width + 1, SourceImage^.height + 1), IPL_DEPTH_32S, 1);
SquareSumImage := cvCreateImage(cvSize(SourceImage^.width + 1, SourceImage^.height + 1), IPL_DEPTH_64F, 1);
SquareSumImage32S := cvCreateImage(cvSize(SourceImage^.width + 1, SourceImage^.height + 1), IPL_DEPTH_32S, 1);
TiltedSumImage := cvCreateImage(cvSize(SourceImage^.width + 1, SourceImage^.height + 1), IPL_DEPTH_32S, 1);
// 计算积分图
{
void cvIntegral( const CvArr* SourceImage, CvArr* SumImage, CvArr* SquareSumImage=0, CvArr* TiltedSumImage=0 );
SourceImage: 源图像,w x h,单通道,8位或浮点型(32f或64f)。
SumImage: 和图像,w+1 x h+1,单通道,32位整数或双精度浮点数(64f)。
SquareSumImage: 平方和图像,w+1 x h+1,单通道,双精度浮点数(64f)。
TiltedSumImage: 倾斜和图像(旋转45度的和图像),w+1 x h+1,单通道,数据类型与 sum 相同。
函数 cvIntegral 为源图像计算一个或多个积分图,计算公式如下:
S(X,Y) = sum(x < X, y < Y) I(x, y)
Sq(X,Y) = sum(x < X, y < Y) I(x, y)^2
T(X,Y) = sum(y < Y, abs(x - X) < y) I(x, y)
计算完成后,这些图像可用于计算任意矩形区域的像素和,例如:
sum(x1 <= x < x2, y1 <= y < y2) I(x, y) = S(x2, y2) - S(x1, y2) - S(x2, y1) + S(x1, x1)
这使得可以快速进行模糊处理、可变窗口大小的快速块相关等操作。
}
cvIntegral(SourceImage, SumImage, SquareSumImage, TiltedSumImage);
// 显示积分图
cvNamedWindow('cvIntegral', 1);
cvShowImage('cvIntegral', SumImage);
cvNamedWindow('cvIntegral 2', 1);
cvConvert(SquareSumImage, SquareSumImage32S);
cvShowImage('cvIntegral 2', SquareSumImage32S);
cvNamedWindow('cvIntegral tilted');
cvShowImage('cvIntegral tilted', TiltedSumImage);
// 等待按键
cvWaitKey(0);
// 释放图像资源
cvReleaseImage(SourceImage);
cvReleaseImage(SumImage);
cvReleaseImage(SquareSumImage);
cvReleaseImage(SquareSumImage32S);
cvReleaseImage(TiltedSumImage);
// 关闭窗口
cvDestroyAllWindows();
except
on E: Exception do
WriteLn(E.ClassName, ': ', E.Message);
end;
end.