2013-04-02 19:07:49 +02:00
|
|
|
|
// JCL_DEBUG_EXPERT_GENERATEJDBG OFF
|
|
|
|
|
// JCL_DEBUG_EXPERT_INSERTJDBG OFF
|
|
|
|
|
// JCL_DEBUG_EXPERT_DELETEMAPFILE OFF
|
|
|
|
|
program cv_MatchShapes;
|
|
|
|
|
|
|
|
|
|
{$APPTYPE CONSOLE}
|
|
|
|
|
{$R *.res}
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
System.SysUtils,
|
2013-04-05 13:36:47 +02:00
|
|
|
|
{$I ..\..\uses_include.inc}
|
|
|
|
|
;
|
2013-04-02 19:07:49 +02:00
|
|
|
|
|
|
|
|
|
const
|
|
|
|
|
const_original = 'resource\matchshapes2.jpg';
|
2013-04-05 13:36:47 +02:00
|
|
|
|
const_template = 'resource\matchshapes_template.jpg';
|
2013-04-02 19:07:49 +02:00
|
|
|
|
|
|
|
|
|
var
|
|
|
|
|
original: pIplImage = nil;
|
|
|
|
|
template: pIplImage = nil;
|
|
|
|
|
original_filename, template_filename: AnsiString;
|
|
|
|
|
|
2013-04-05 13:36:47 +02:00
|
|
|
|
src: pIplImage = nil;
|
|
|
|
|
dst: pIplImage = nil;
|
|
|
|
|
binI: pIplImage = nil;
|
|
|
|
|
binT: pIplImage = nil;
|
|
|
|
|
rgb: pIplImage = nil;
|
|
|
|
|
rgbT: pIplImage = nil;
|
|
|
|
|
storage: pCvMemStorage = nil;
|
|
|
|
|
contoursI: pCvSeq = nil;
|
|
|
|
|
contoursT: pCvSeq = nil;
|
|
|
|
|
seq0: pCvSeq = nil;
|
|
|
|
|
seqT: pCvSeq = nil;
|
|
|
|
|
seqM: pCvSeq = nil;
|
2013-04-02 19:07:49 +02:00
|
|
|
|
contoursCont: integer;
|
|
|
|
|
font: TCvFont;
|
|
|
|
|
counter: integer;
|
|
|
|
|
perim, match0: double;
|
2013-04-05 13:36:47 +02:00
|
|
|
|
perimT: double = 0;
|
|
|
|
|
matchM: double = 1000;
|
2013-04-02 19:07:49 +02:00
|
|
|
|
|
|
|
|
|
begin
|
2013-04-05 13:36:47 +02:00
|
|
|
|
try
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
if ParamCount = 2 then
|
|
|
|
|
begin
|
|
|
|
|
original_filename := ParamStr(1);
|
|
|
|
|
template_filename := ParamStr(2);
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
original_filename := const_original;
|
|
|
|
|
template_filename := const_template;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
original := cvLoadImage(pCVChar(original_filename), CV_LOAD_IMAGE_COLOR);
|
|
|
|
|
WriteLn(Format('[i] original image: %s', [original_filename]));
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
template := cvLoadImage(pCVChar(template_filename), CV_LOAD_IMAGE_COLOR);
|
|
|
|
|
WriteLn(Format('[i] template image: %s', [template_filename]));
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
cvNamedWindow('Original', CV_WINDOW_AUTOSIZE);
|
|
|
|
|
cvShowImage('Original', original);
|
|
|
|
|
cvNamedWindow('Template', CV_WINDOW_AUTOSIZE);
|
|
|
|
|
cvShowImage('Template', template);
|
|
|
|
|
|
|
|
|
|
// C<><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
WriteLn('[i] Run test cvMatchShapes()');
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
src := cvCloneImage(original);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
binI := cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
|
|
|
|
|
binT := cvCreateImage(cvGetSize(template), IPL_DEPTH_8U, 1);
|
2013-04-02 19:07:49 +02:00
|
|
|
|
|
2013-04-05 13:36:47 +02:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
cvCanny(src, binI, 50, 200);
|
|
|
|
|
cvCanny(template, binT, 50, 200);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// cvNamedWindow('CannyI', CV_WINDOW_AUTOSIZE);
|
|
|
|
|
// cvShowImage('CannyI', binI);
|
|
|
|
|
// cvNamedWindow('CannyT', CV_WINDOW_AUTOSIZE);
|
|
|
|
|
// cvShowImage('CannyT', binT);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
storage := nil;
|
|
|
|
|
storage := cvCreateMemStorage(0);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
contoursCont := cvFindContours(binI, storage, @contoursI, sizeof(TCvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE,
|
|
|
|
|
cvPoint(0, 0));
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
cvInitFont(@font, CV_FONT_HERSHEY_PLAIN, 1.0, 1.0);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
if contoursI <> nil then
|
2013-04-02 19:07:49 +02:00
|
|
|
|
begin
|
2013-04-05 13:36:47 +02:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
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));
|
|
|
|
|
seq0 := seq0.h_next;
|
|
|
|
|
end;
|
2013-04-02 19:07:49 +02:00
|
|
|
|
end;
|
2013-04-05 13:36:47 +02:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// cvNamedWindow('Cont', CV_WINDOW_AUTOSIZE);
|
|
|
|
|
// cvShowImage('Cont', rgb );
|
2013-04-02 19:07:49 +02:00
|
|
|
|
|
2013-04-05 13:36:47 +02:00
|
|
|
|
cvConvertImage(src, rgb, CV_GRAY2BGR);
|
2013-04-02 19:07:49 +02:00
|
|
|
|
|
2013-04-05 13:36:47 +02:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
cvFindContours(binT, storage, @contoursT, sizeof(TCvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
|
2013-04-02 19:07:49 +02:00
|
|
|
|
|
2013-04-05 13:36:47 +02:00
|
|
|
|
if contoursT <> nil then
|
2013-04-02 19:07:49 +02:00
|
|
|
|
begin
|
2013-04-05 13:36:47 +02:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
seq0 := contoursT;
|
|
|
|
|
while (seq0 <> nil) do
|
2013-04-02 19:07:49 +02:00
|
|
|
|
begin
|
2013-04-05 13:36:47 +02:00
|
|
|
|
perim := cvContourPerimeter(seq0);
|
|
|
|
|
if perim > perimT then
|
|
|
|
|
begin
|
|
|
|
|
perimT := perim;
|
|
|
|
|
seqT := seq0;
|
|
|
|
|
end;
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
cvDrawContours(rgbT, seq0, CV_RGB(255, 216, 0), CV_RGB(0, 0, 250), 0, 1, 8, cvPoint(0, 0));
|
|
|
|
|
seq0 := seq0.h_next;
|
2013-04-02 19:07:49 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
2013-04-05 13:36:47 +02:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
cvDrawContours(rgbT, seqT, CV_RGB(52, 201, 36), CV_RGB(36, 201, 197), 0, 2, 8, cvPoint(0, 0));
|
|
|
|
|
// cvNamedWindow('ContT', CV_WINDOW_AUTOSIZE);
|
|
|
|
|
// cvShowImage('ContT', rgbT);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
counter := 0;
|
|
|
|
|
if contoursI <> nil then
|
2013-04-02 19:07:49 +02:00
|
|
|
|
begin
|
2013-04-05 13:36:47 +02:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
seq0 := contoursI;
|
|
|
|
|
while (seq0 <> nil) do
|
2013-04-02 19:07:49 +02:00
|
|
|
|
begin
|
2013-04-05 13:36:47 +02:00
|
|
|
|
match0 := cvMatchShapes(seq0, seqT, CV_CONTOURS_MATCH_I3);
|
|
|
|
|
if match0 < matchM then
|
|
|
|
|
begin
|
|
|
|
|
matchM := match0;
|
|
|
|
|
seqM := seq0;
|
|
|
|
|
end;
|
|
|
|
|
Inc(counter);
|
|
|
|
|
WriteLn(Format('[i] %d match: %.2f', [counter, match0]));
|
|
|
|
|
seq0 := seq0.h_next;
|
2013-04-02 19:07:49 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
2013-04-05 13:36:47 +02:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
cvDrawContours(rgb, seqM, CV_RGB(52, 201, 36), CV_RGB(36, 201, 197), 0, 2, 8, cvPoint(0, 0));
|
|
|
|
|
cvNamedWindow('Find', CV_WINDOW_AUTOSIZE);
|
|
|
|
|
cvShowImage('Find', rgb);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
cvWaitKey(0);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
cvReleaseMemStorage(storage);
|
|
|
|
|
cvReleaseImage(src);
|
|
|
|
|
cvReleaseImage(dst);
|
|
|
|
|
cvReleaseImage(rgb);
|
|
|
|
|
cvReleaseImage(rgbT);
|
|
|
|
|
cvReleaseImage(binI);
|
|
|
|
|
cvReleaseImage(binT);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
|
|
|
|
cvDestroyAllWindows();
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
cvReleaseImage(original);
|
|
|
|
|
cvReleaseImage(template);
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD>
|
|
|
|
|
cvDestroyAllWindows();
|
|
|
|
|
except
|
|
|
|
|
on E: Exception do
|
|
|
|
|
WriteLn(E.ClassName, ': ', E.Message);
|
2013-04-02 19:07:49 +02:00
|
|
|
|
end;
|
2013-04-05 13:36:47 +02:00
|
|
|
|
|
2013-04-02 19:07:49 +02:00
|
|
|
|
end.
|