Delphi-OpenCV/samples/LibTest/cvMatchShapes/cv_MatchShapes.dpr
Laex a58d5f3056 Fixed issues #3
Signed-off-by: Laex <laex@bk.ru>
2013-04-18 10:34:16 +04:00

232 lines
7.2 KiB
ObjectPascal
Raw Blame History

(* /*****************************************************************
// Delphi-OpenCV Demo
// Copyright (C) 2013 Project Delphi-OpenCV
// ****************************************************************
// Contributor:
// laentir Valetov
// email:laex@bk.ru
// ****************************************************************
// You may retrieve the latest version of this file at the GitHub,
// located at git://github.com/Laex/Delphi-OpenCV.git
// ****************************************************************
// The contents of this file are used with permission, subject to
// the Mozilla Public License Version 1.1 (the "License"); you may
// not use this file except in compliance with the License. You may
// obtain a copy of the License at
// http://www.mozilla.org/MPL/MPL-1_1Final.html
//
// Software distributed under the License is distributed on an
// "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
// implied. See the License for the specific language governing
// rights and limitations under the License.
******************************************************************* *)
// 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,
uLibName in '..\..\..\include\uLibName.pas',
highgui_c in '..\..\..\include\highgui\highgui_c.pas',
core_c in '..\..\..\include\core\core_c.pas',
Core.types_c in '..\..\..\include\core\Core.types_c.pas',
imgproc.types_c in '..\..\..\include\imgproc\imgproc.types_c.pas',
imgproc_c in '..\..\..\include\imgproc\imgproc_c.pas',
legacy in '..\..\..\include\legacy\legacy.pas',
calib3d in '..\..\..\include\calib3d\calib3d.pas',
imgproc in '..\..\..\include\imgproc\imgproc.pas',
haar in '..\..\..\include\objdetect\haar.pas',
objdetect in '..\..\..\include\objdetect\objdetect.pas',
tracking in '..\..\..\include\video\tracking.pas',
Core in '..\..\..\include\core\core.pas';
const
const_original = 'resource\matchshapes2.jpg';
const_template = 'resource\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;
storage: 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
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>c<EFBFBD><63>
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);
// c<><63><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);
// <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);
// c<><63><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>c<EFBFBD><63><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if contoursI <> nil then
begin
// <20><>c<EFBFBD><63><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;
end;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// cvNamedWindow('Cont', CV_WINDOW_AUTOSIZE);
// cvShowImage('Cont', rgb );
cvConvertImage(src, rgb, CV_GRAY2BGR);
// <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));
if contoursT <> nil then
begin
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> c<><63><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
seq0 := contoursT;
while (seq0 <> nil) do
begin
perim := cvContourPerimeter(seq0);
if perim > perimT then
begin
perimT := perim;
seqT := seq0;
end;
// <20><>c<EFBFBD><63><EFBFBD>
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;
// <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
begin
// <20><><EFBFBD>c<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> c<><63><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
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 match: %.2f', [counter, match0]));
seq0 := seq0.h_next;
end;
end;
// <20><>c<EFBFBD><63><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>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><>c<EFBFBD><63>c<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>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><>c<EFBFBD><63>c<EFBFBD>
cvReleaseImage(original);
cvReleaseImage(template);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>c<EFBFBD> <20><><EFBFBD><EFBFBD>
cvDestroyAllWindows();
except
on E: Exception do
WriteLn(E.ClassName, ': ', E.Message);
end;
end.