Demo1
program cv_MatchShapes;
{$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
const_original = cResourceMedia + 'matchshapes2.jpg';
const_template = cResourceMedia + 'matchshapes_template.jpg';
var
original: pIplImage = nil;
template: pIplImage = nil;
original_filename, template_filename: AnsiString;
src: pIplImage = nil;
dst: pIplImage = nil;
binI: pIplImage = nil;
binT: pIplImage = nil;
rgb: pIplImage = nil;
rgbT: pIplImage = nil;
storageI: pCvMemStorage = nil;
storageT: pCvMemStorage = nil;
contoursI: pCvSeq = nil;
contoursT: pCvSeq = nil;
seq0: pCvSeq = nil;
seqT: pCvSeq = nil;
seqM: pCvSeq = nil;
contoursCont: integer;
font: TCvFont;
counter: integer;
perim, match0: double;
perimT: double = 0;
matchM: double = 1000;
begin
try
if ParamCount = 2 then
begin
original_filename := AnsiString(ParamStr(1));
template_filename := AnsiString(ParamStr(2));
end
else
begin
original_filename := const_original;
template_filename := const_template;
end;
original := cvLoadImage(pCVChar(original_filename), CV_LOAD_IMAGE_COLOR);
WriteLn(Format('[i] 原始图像: %s', [original_filename]));
template := cvLoadImage(pCVChar(template_filename), CV_LOAD_IMAGE_COLOR);
WriteLn(Format('[i] 模板图像: %s', [template_filename]));
cvNamedWindow('原始图像', CV_WINDOW_AUTOSIZE);
cvShowImage('原始图像', original);
cvNamedWindow('模板图像', CV_WINDOW_AUTOSIZE);
cvShowImage('模板图像', template);
WriteLn('[i] 运行测试 cvMatchShapes()');
src := cvCloneImage(original);
binI := cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
binT := cvCreateImage(cvGetSize(template), IPL_DEPTH_8U, 1);
rgb := cvCreateImage(cvGetSize(original), IPL_DEPTH_8U, 3);
cvConvertImage(src, rgb, CV_GRAY2BGR);
rgbT := cvCreateImage(cvGetSize(template), IPL_DEPTH_8U, 3);
cvConvertImage(template, rgbT, CV_GRAY2BGR);
cvCanny(src, binI, 50, 200);
cvCanny(template, binT, 50, 200);
storageI := cvCreateMemStorage(0);
contoursCont := cvFindContours(binI, storageI, @contoursI, sizeof(TCvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
cvInitFont(@font, CV_FONT_HERSHEY_PLAIN, 1.0, 1.0);
if Assigned(contoursI) then
begin
seq0 := contoursI;
while Assigned(seq0) do
begin
cvDrawContours(rgb, seq0, CV_RGB(255, 216, 0), CV_RGB(0, 0, 250), 0, 1, 8, cvPoint(0, 0));
seq0 := seq0.h_next;
end;
end;
cvConvertImage(src, rgb, CV_GRAY2BGR);
storageT := cvCreateMemStorage(0);
cvFindContours(binT, storageT, @contoursT, sizeof(TCvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
if Assigned(contoursT) then
begin
seq0 := contoursT;
while Assigned(seq0) do
begin
perim := cvContourPerimeter(seq0);
if perim > perimT then
begin
perimT := perim;
seqT := seq0;
end;
cvDrawContours(rgbT, seq0, CV_RGB(255, 216, 0), CV_RGB(0, 0, 250), 0, 1, 8, cvPoint(0, 0));
seq0 := seq0.h_next;
end;
end;
cvDrawContours(rgbT, seqT, CV_RGB(52, 201, 36), CV_RGB(36, 201, 197), 0, 2, 8, cvPoint(0, 0));
counter := 0;
if Assigned(contoursI) then
begin
seq0 := contoursI;
while Assigned(seq0) do
begin
WriteLn('Assigned seq0: ', Assigned(seq0), '(', IntToHex(Integer(seq0), 8), ')');
WriteLn('Assigned seqT: ', Assigned(seqT), '(', IntToHex(Integer(seqT), 8), ')');
match0 := cvMatchShapes(seq0, seqT, CV_CONTOURS_MATCH_I3);
if match0 < matchM then
begin
matchM := match0;
seqM := seq0;
end;
Inc(counter);
WriteLn(Format('[i] %d 匹配度: %.2f', [counter, match0]));
seq0 := seq0.h_next;
end;
end;
cvDrawContours(rgb, seqM, CV_RGB(52, 201, 36), CV_RGB(36, 201, 197), 0, 2, 8, cvPoint(0, 0));
cvNamedWindow('查找结果', CV_WINDOW_AUTOSIZE);
cvShowImage('查找结果', rgb);
cvWaitKey(0);
cvReleaseMemStorage(storageI);
cvReleaseMemStorage(storageT);
cvReleaseImage(src);
cvReleaseImage(dst);
cvReleaseImage(rgb);
cvReleaseImage(rgbT);
cvReleaseImage(binI);
cvReleaseImage(binT);
cvDestroyAllWindows();
cvReleaseImage(original);
cvReleaseImage(template);
cvDestroyAllWindows();
except
on E: Exception do
WriteLn(E.ClassName, ': ', E.Message);
end;
end.
Demo2
program cv_MatchShapes2;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.Math,
ocv.highgui_c,
ocv.core_c,
ocv.core.types_c,
ocv.imgproc_c,
ocv.imgproc.types_c,
ocv.utils,
uResourcePaths;
procedure testMatch(original, templ: pIplImage);
Var
src, dst: pIplImage;
binI: pIplImage;
binT: pIplImage;
rgb: pIplImage;
rgbT: pIplImage;
storageI: pCvMemStorage;
storageT: pCvMemStorage;
contoursI: pCvSeq;
contoursT: pCvSeq;
contoursCont: Integer;
font: TCvFont;
counter: Integer;
buf: string;
seq0: pCvSeq;
seqT: pCvSeq;
perimT: double;
perim: double;
seqM: pCvSeq;
matchM: double;
match0: double;
point: TCvPoint2D32f;
rad: Float;
begin
assert(original <> nil);
assert(templ <> nil);
WriteLn('[i] test cvMatchShapes()\n');
src := nil;
dst := nil;
src := cvCloneImage(original);
binI := cvCreateImage(cvGetSize(src), 8, 1);
binT := cvCreateImage(cvGetSize(templ), 8, 1);
rgb := cvCreateImage(cvGetSize(original), 8, 3);
cvConvertImage(src, rgb, CV_GRAY2BGR);
rgbT := cvCreateImage(cvGetSize(templ), 8, 3);
cvConvertImage(templ, rgbT, CV_GRAY2BGR);
cvCanny(src, binI, 50, 200);
cvCanny(templ, binT, 50, 200);
cvNamedWindow('cannyI', 1);
cvShowImage('cannyI', binI);
cvNamedWindow('cannyT', 1);
cvShowImage('cannyT', binT);
storageI := cvCreateMemStorage(0);
storageT := cvCreateMemStorage(0);
contoursI := nil;
contoursT := nil;
contoursCont := cvFindContours(binI, storageI, @contoursI, sizeof(TCvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
cvInitFont(@font, CV_FONT_HERSHEY_PLAIN, 1.0, 1.0);
counter := 0;
if (contoursI <> nil) then
begin
seq0 := contoursI;
while seq0 <> nil do
begin
cvDrawContours(rgb, seq0, CV_RGB(255, 216, 0), CV_RGB(0, 0, 250), 0, 1, 8, cvPoint(0, 0));
cvMinEnclosingCircle(seq0, @point, @rad);
Inc(counter);
cvPutText(rgb, c_str(IntToStr(counter)), cvPointFrom32f(point), @font, CV_RGB(0, 255, 0));
seq0 := seq0^.h_next;
end;
end;
cvNamedWindow('cont', 1);
cvShowImage('cont', rgb);
cvConvertImage(src, rgb, CV_GRAY2BGR);
cvFindContours(binT, storageT, @contoursT, sizeof(TCvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
seqT := nil;
perimT := 0;
if (contoursT <> nil) then
begin
seq0 := contoursT;
while seq0 <> nil do
begin
perim := cvContourPerimeter(seq0);
if (perim > perimT) then
begin
perimT := perim;
seqT := seq0;
end;
cvDrawContours(rgbT, seq0, CV_RGB(255, 216, 0), CV_RGB(0, 0, 250), 0, 1, 8, cvPoint(0, 0));
seq0 := seq0^.h_next;
end;
end;
cvDrawContours(rgbT, seqT, CV_RGB(52, 201, 36), CV_RGB(36, 201, 197), 0, 2, 8, cvPoint(0, 0));
cvNamedWindow('contT', 1);
cvShowImage('contT', rgbT);
seqM := nil;
matchM := 1000;
counter := 0;
if (contoursI <> nil) then
begin
seq0 := contoursI;
while seq0 <> nil do
begin
match0 := cvMatchShapes(seq0, seqT, CV_CONTOURS_MATCH_I3);
if (match0 < matchM) then
begin
matchM := match0;
seqM := seq0;
end;
Inc(counter);
WriteLn(Format('[i] %d 匹配度: %.2f', [counter, match0]));
seq0 := seq0^.h_next;
end;
end;
cvDrawContours(rgb, seqM, CV_RGB(52, 201, 36), CV_RGB(36, 201, 197), 0, 2, 8, cvPoint(0, 0));
cvNamedWindow('find', 1);
cvShowImage('find', rgb);
cvWaitKey(0);
cvReleaseMemStorage(storageI);
cvReleaseMemStorage(storageT);
cvReleaseImage(src);
cvReleaseImage(dst);
cvReleaseImage(rgb);
cvReleaseImage(rgbT);
cvReleaseImage(binI);
cvReleaseImage(binT);
cvDestroyAllWindows();
end;
const
const_original = cResourceMedia + 'matchshapes2.jpg';
const_template = cResourceMedia + 'matchshapes_template.jpg';
Var
original: pIplImage = nil;
templ: pIplImage = nil;
filename, filename2: String;
begin
try
filename := ifthen(ParamCount > 0, ParamStr(1), const_original);
original := cvLoadImage(c_str(filename), 0);
WriteLn('[i] 图像: ', filename);
assert(Assigned(original));
filename2 := ifthen(ParamCount > 1, ParamStr(2), const_template);
templ := cvLoadImage(c_str(filename2), 0);
WriteLn('[i] 模板: ', filename2);
assert(Assigned(templ));
cvNamedWindow('original', 1);
cvShowImage('original', original);
cvNamedWindow('template', 1);
cvShowImage('template', templ);
testMatch(original, templ);
cvReleaseImage(original);
cvReleaseImage(templ);
cvDestroyAllWindows();
except
on E: Exception do
WriteLn(E.ClassName, ': ', E.Message);
end;
end.