Added sample cv_Sum

Signed-off-by: Laex <laex@bk.ru>
This commit is contained in:
Laex 2013-09-12 13:49:13 +04:00
parent f4e3bb49f1
commit 6fac4b8aef
4 changed files with 349 additions and 49 deletions

View File

@ -99,7 +99,8 @@ unit core_c;
interface
uses
Windows, core.types_c;
Windows,
core.types_c;
{ ****************************************************************************************
* cArray allocation, deallocation, initialization and access to elements *
@ -733,13 +734,13 @@ procedure cvCvtPlaneToPix(const src0: pIplImage; const src1: pIplImage; const sr
{ dst(idx) = src1(idx) | src2(idx) */
CVAPI(void) cvOr( const CvArr* src1, const CvArr* src2,
CvArr* dst, const CvArr* mask CV_DEFAULT(NULL));
CvArr* dst, const CvArr* mask CV_DEFAULT(NULL));
}
procedure cvOr(const src1, src2: pCvArr; dst: pCvArr; const mask: pCvArr = nil); cdecl;
{ dst(idx) = src1(idx) ^ src2(idx) */
CVAPI(void) cvXor( const CvArr* src1, const CvArr* src2,
CvArr* dst, const CvArr* mask CV_DEFAULT(NULL));
CvArr* dst, const CvArr* mask CV_DEFAULT(NULL));
}
procedure cvXor(const src1, src2: pCvArr; dst: pCvArr; const mask: pCvArr = nil); cdecl;
@ -776,18 +777,18 @@ procedure cvFlip(const src: pCvArr; dst: pCvArr = nil; flip_mode: Integer = 0);
const
// * types of array norm */
CV_C = 1;
CV_L1 = 2;
CV_L2 = 4;
CV_C = 1;
CV_L1 = 2;
CV_L2 = 4;
CV_NORM_MASK = 7;
CV_RELATIVE = 8;
CV_DIFF = 16;
CV_MINMAX = 32;
CV_RELATIVE = 8;
CV_DIFF = 16;
CV_MINMAX = 32;
CV_DIFF_C = (CV_DIFF or CV_C);
CV_DIFF_L1 = (CV_DIFF or CV_L1);
CV_DIFF_L2 = (CV_DIFF or CV_L2);
CV_RELATIVE_C = (CV_RELATIVE or CV_C);
CV_DIFF_C = (CV_DIFF or CV_C);
CV_DIFF_L1 = (CV_DIFF or CV_L1);
CV_DIFF_L2 = (CV_DIFF or CV_L2);
CV_RELATIVE_C = (CV_RELATIVE or CV_C);
CV_RELATIVE_L1 = (CV_RELATIVE or CV_L1);
CV_RELATIVE_L2 = (CV_RELATIVE or CV_L2);
@ -803,6 +804,10 @@ function cvNorm(const arr1: pCvArr; const arr2: pCvArr = nil; norm_type: Integer
// * cArray Statistics *
// *************************************************************************************** *)
// * Finds sum of array elements */
// CVAPI(CvScalar) cvSum( const CvArr* arr );
function cvSum(const arr: pCvArr): TCvScalar; cdecl;
{ Calculates number of non-zero pixels
CVAPI(Integer)cvCountNonZero(pCvArr * arr);
}
@ -1132,21 +1137,22 @@ procedure cvSetNumThreads(threads: Integer = 0); cdecl;
function cvGetThreadNum: Integer; cdecl;
const
CV_CPU_NONE = 0;
CV_CPU_MMX = 1;
CV_CPU_SSE = 2;
CV_CPU_SSE2 = 3;
CV_CPU_SSE3 = 4;
CV_CPU_SSSE3 = 5;
CV_CPU_SSE4_1 = 6;
CV_CPU_SSE4_2 = 7;
CV_CPU_POPCNT = 8;
CV_CPU_AVX = 10;
CV_CPU_NONE = 0;
CV_CPU_MMX = 1;
CV_CPU_SSE = 2;
CV_CPU_SSE2 = 3;
CV_CPU_SSE3 = 4;
CV_CPU_SSSE3 = 5;
CV_CPU_SSE4_1 = 6;
CV_CPU_SSE4_2 = 7;
CV_CPU_POPCNT = 8;
CV_CPU_AVX = 10;
CV_HARDWARE_MAX_FEATURE = 255;
implementation
uses ulibname;
uses
ulibname;
function cvCreateImageHeader; external Core_Dll;
function cvInitImageHeader; external Core_Dll;
@ -1169,14 +1175,23 @@ procedure cvGetSubArr; external Core_Dll name 'cvGetSubRect';
function cvGetRow(const arr: pCvArr; submat: pCvMat; row: Integer): pCvMat;
begin
Result := cvGetRows(arr, submat, row, row + 1, 1);
Result := cvGetRows(
arr,
submat,
row,
row + 1,
1);
end;
function cvGetCols; external Core_Dll;
function cvGetCol(const arr: pCvArr; submat: pCvMat; col: Integer): pCvMat;
begin
Result := cvGetCols(arr, submat, col, col + 1);
Result := cvGetCols(
arr,
submat,
col,
col + 1);
end;
function cvGetDiag; external Core_Dll;
@ -1219,13 +1234,13 @@ function cvInitSparseMatIterator; external Core_Dll;
// }
function cvGetNextSparseNode(mat_iterator: pCvSparseMatIterator): pCvSparseNode;
var
idx: Integer;
idx : Integer;
node: pCvSparseNode;
begin
if Assigned(mat_iterator.node.next) then
begin
mat_iterator.node := mat_iterator.node.next;
Result := mat_iterator.node;
Result := mat_iterator.node;
end
else
begin
@ -1236,8 +1251,8 @@ begin
if Assigned(node) then
begin
mat_iterator.curidx := idx;
mat_iterator.node := node;
Result := mat_iterator.node;
mat_iterator.node := node;
Result := mat_iterator.node;
exit;
end;
end;
@ -1278,7 +1293,13 @@ function cvReshapeMatND; external Core_Dll;
function cvReshapeND(const arr: pCvArr; sizeof_header: Integer; header: pCvArr; new_cn, new_dims: Integer;
new_sizes: pInteger): pCvArr; inline;
begin
Result := cvReshapeMatND(arr, sizeof(sizeof_header), header, new_cn, new_dims, new_sizes);
Result := cvReshapeMatND(
arr,
sizeof(sizeof_header),
header,
new_cn,
new_dims,
new_sizes);
end;
function cvReshape; external Core_Dll;
@ -1308,14 +1329,16 @@ procedure cvSet(arr: pCvArr; value: TCvScalar; const mask: pCvArr = Nil); extern
procedure cvSet(mat: pCvMat; i, j: Integer; val: Single); inline;
var
type_: Integer;
ptr: puchar;
pf: PSingle;
ptr : puchar;
pf : PSingle;
begin
type_ := CV_MAT_TYPE(mat._type);
assert((i < mat^.rows) and (j < mat^.cols) and (type_ = CV_32FC1));
ptr := mat^.data;
Inc(ptr, mat.step * i + sizeof(Single) * j);
pf := PSingle(ptr);
Inc(
ptr,
mat.step * i + sizeof(Single) * j);
pf := PSingle(ptr);
pf^ := val;
end;
@ -1328,7 +1351,11 @@ procedure cvConvertScale; external Core_Dll;
procedure cvConvert(const src: pCvArr; dst: pCvArr); inline;
begin
cvConvertScale(src, dst, 1, 0);
cvConvertScale(
src,
dst,
1,
0);
end;
procedure cvScale; external Core_Dll name 'cvConvertScale';
@ -1342,7 +1369,11 @@ procedure cvSub; external Core_Dll;
procedure cvSubS(const src: pIplImage; value: TCvScalar; dst: pIplImage; const mask: pIplImage);
begin
cvAddS(src, CvScalar(-value.val[0], -value.val[1], -value.val[2], -value.val[3]), dst, mask);
cvAddS(
src,
CvScalar(-value.val[0], -value.val[1], -value.val[2], -value.val[3]),
dst,
mask);
end;
procedure cvSubRS; external Core_Dll;
@ -1353,7 +1384,11 @@ procedure cvScaleAdd; external Core_Dll;
// define cvAXPY( A, real_scalar, B, C ) cvScaleAdd(A, cvRealScalar(real_scalar), B, C)
procedure cvAXPY(A: pIplImage; real_scalar: double; B, C: pIplImage); inline;
begin
cvScaleAdd(A, cvRealScalar(real_scalar), B, C);
cvScaleAdd(
A,
cvRealScalar(real_scalar),
B,
C);
end;
procedure cvAddWeighted; external Core_Dll;
@ -1372,7 +1407,11 @@ procedure cvCopyImage; external Core_Dll name 'cvCopy';
function CV_RGB(const r, g, B: double): TCvScalar; inline;
begin
Result := CvScalar(B, g, r, 0);
Result := CvScalar(
B,
g,
r,
0);
end;
procedure cvSave(const filename: pCVChar; const struct_ptr: pointer; const name: pCVChar; const comment: pCVChar;
@ -1381,7 +1420,12 @@ procedure cvSave(const filename: pCVChar; const struct_ptr: pointer; const name:
procedure cvSave(const filename: pCVChar; const struct_ptr: pointer; const name: pCVChar = Nil;
const comment: pCVChar = Nil); overload; inline;
begin
cvSave(filename, struct_ptr, name, comment, ZeroCvAttrList);
cvSave(
filename,
struct_ptr,
name,
comment,
ZeroCvAttrList);
end;
function cvLoad; external Core_Dll;
@ -1416,14 +1460,18 @@ begin
// return !node ? default_value :
// CV_NODE_IS_INT(node->tag) ? node->data.i :
// CV_NODE_IS_REAL(node->tag) ? cvRound(node->data.f) : 0x7fffffff;
Result := iif(not Assigned(node), default_value, iif(CV_NODE_IS_INT(node^.tag), node^.i,
iif(CV_NODE_IS_REAL(node^.tag), node^.f, $7FFFFFFF)));
Result := iif(
not Assigned(node),
default_value,
iif(CV_NODE_IS_INT(node^.tag), node^.i, iif(CV_NODE_IS_REAL(node^.tag), node^.f, $7FFFFFFF)));
end;
function cvReadIntByName;
begin
// return cvReadInt( cvGetFileNodeByName( fs, map, name ), default_value );
Result := cvReadInt(cvGetFileNodeByName(fs, map, name), default_value);
Result := cvReadInt(
cvGetFileNodeByName(fs, map, name),
default_value);
end;
function cvRead; external Core_Dll;
@ -1440,9 +1488,19 @@ procedure cvEllipseBox;
var
axes: TCvSize;
begin
axes.width := cvRound(box.size.width * 0.5);
axes.width := cvRound(box.size.width * 0.5);
axes.height := cvRound(box.size.height * 0.5);
cvEllipse(img, cvPointFrom32f(box.center), axes, box.angle, 0, 360, color, thickness, line_type, shift);
cvEllipse(
img,
cvPointFrom32f(box.center),
axes,
box.angle,
0,
360,
color,
thickness,
line_type,
shift);
end;
procedure cvOr; external Core_Dll;
@ -1463,14 +1521,16 @@ function cvCountNonZero; external Core_Dll;
function cvGet(const mat: pCvMat; i, j: Integer): Single; inline;
var
type_: Integer;
ptr: puchar;
pf: PSingle;
ptr : puchar;
pf : PSingle;
begin
type_ := CV_MAT_TYPE(mat^._type);
assert((i < mat^.rows) and (j < mat^.cols) and (type_ = CV_32FC1));
ptr := mat^.data;
Inc(ptr, mat.step * i + sizeof(Single) * j);
pf := PSingle(ptr);
Inc(
ptr,
mat.step * i + sizeof(Single) * j);
pf := PSingle(ptr);
Result := pf^;
end;
@ -1508,4 +1568,6 @@ procedure cvClearSeq; external Core_Dll;
procedure cvWrite; external Core_Dll;
function cvSeqPartition; external Core_Dll;
function cvSum; external Core_Dll;
end.

View File

@ -0,0 +1,97 @@
// *****************************************************************
// 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.
// *******************************************************************
// Source: http://answers.opencv.org/question/20484/get-the-color-percentage/
//
// Get the color percentage of RGB(ex red 50%, green 25% and blue %25) in an image
//
// *******************************************************************
program cv_Sum;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
highgui_c,
core_c,
Core.types_c,
imgproc_c,
imgproc.types_c;
const
// èìÿ êàðòèíêè
filename = 'Resource\cat2.jpg';
var
img : pIplImage;
channels: array [0 .. 2] of pIplImage;
BGRSum : Array [0 .. 2] of TCvScalar;
i, total: Integer;
begin
try
img := cvLoadImage(
filename,
1);
for i := 0 to 2 do
channels[i] := cvCreateImage(
cvGetSize(img),
8,
1);
cvSplit(
img,
channels[0],
channels[1],
channels[2],
0);
for i := 0 to 2 do
BGRSum[i] := cvSum(channels[i]);
total := img^.width * img^.height * 255;
WriteLn('Color percentage of RGB(ex red 50%, green 25% and blue %25) in an image is');
writeln(
'red: ',
BGRSum[2].val[0] / total * 100:2:2);
writeln(
'green: ',
BGRSum[1].val[0] / total * 100:2:2);
writeln(
'blue: ',
BGRSum[0].val[0] / total * 100:2:2);
readln;
except
on E: Exception do
writeln(
E.ClassName,
': ',
E.Message);
end;
end.

View File

@ -0,0 +1,141 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{1182C644-116C-49CE-9AA6-86129B93A971}</ProjectGuid>
<MainSource>cv_Sum.dpr</MainSource>
<Base>True</Base>
<Config Condition="'$(Config)'==''">Debug</Config>
<TargetedPlatforms>1</TargetedPlatforms>
<AppType>Console</AppType>
<FrameworkType>None</FrameworkType>
<ProjectVersion>14.6</ProjectVersion>
<Platform Condition="'$(Platform)'==''">Win32</Platform>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Base' or '$(Base)'!=''">
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="('$(Platform)'=='Win32' and '$(Base)'=='true') or '$(Base_Win32)'!=''">
<Base_Win32>true</Base_Win32>
<CfgParent>Base</CfgParent>
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Release' or '$(Cfg_1)'!=''">
<Cfg_1>true</Cfg_1>
<CfgParent>Base</CfgParent>
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Debug' or '$(Cfg_2)'!=''">
<Cfg_2>true</Cfg_2>
<CfgParent>Base</CfgParent>
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="('$(Platform)'=='Win32' and '$(Cfg_2)'=='true') or '$(Cfg_2_Win32)'!=''">
<Cfg_2_Win32>true</Cfg_2_Win32>
<CfgParent>Cfg_2</CfgParent>
<Cfg_2>true</Cfg_2>
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="'$(Base)'!=''">
<DCC_E>false</DCC_E>
<DCC_S>false</DCC_S>
<DCC_Namespace>System;Xml;Data;Datasnap;Web;Soap;$(DCC_Namespace)</DCC_Namespace>
<Manifest_File>None</Manifest_File>
<DCC_ImageBase>00400000</DCC_ImageBase>
<DCC_F>false</DCC_F>
<DCC_K>false</DCC_K>
<DCC_ExeOutput>..\..\..\bin\</DCC_ExeOutput>
<VerInfo_Locale>1049</VerInfo_Locale>
<DCC_N>false</DCC_N>
<VerInfo_Keys>CompanyName=;FileDescription=;FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductName=;ProductVersion=1.0.0.0;Comments=;CFBundleName=;CFBundleDisplayName=;CFBundleIdentifier=;CFBundleVersion=;CFBundlePackageType=;CFBundleSignature=;CFBundleAllowMixedLocalizations=;CFBundleExecutable=</VerInfo_Keys>
</PropertyGroup>
<PropertyGroup Condition="'$(Base_Win32)'!=''">
<DCC_Namespace>Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace)</DCC_Namespace>
<VerInfo_Keys>CompanyName=;FileDescription=;FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductName=;ProductVersion=1.0.0.0;Comments=</VerInfo_Keys>
<VerInfo_Locale>1033</VerInfo_Locale>
</PropertyGroup>
<PropertyGroup Condition="'$(Cfg_1)'!=''">
<DCC_SymbolReferenceInfo>0</DCC_SymbolReferenceInfo>
<DCC_DebugInformation>false</DCC_DebugInformation>
<DCC_Define>RELEASE;$(DCC_Define)</DCC_Define>
<DCC_LocalDebugSymbols>false</DCC_LocalDebugSymbols>
</PropertyGroup>
<PropertyGroup Condition="'$(Cfg_2)'!=''">
<DCC_GenerateStackFrames>true</DCC_GenerateStackFrames>
<DCC_Optimize>false</DCC_Optimize>
<DCC_Define>DEBUG;$(DCC_Define)</DCC_Define>
</PropertyGroup>
<PropertyGroup Condition="'$(Cfg_2_Win32)'!=''">
<Manifest_File>None</Manifest_File>
<VerInfo_Keys>CompanyName=;FileDescription=;FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductName=;ProductVersion=1.0.0.0;Comments=</VerInfo_Keys>
<VerInfo_Locale>1033</VerInfo_Locale>
</PropertyGroup>
<ItemGroup>
<DelphiCompile Include="$(MainSource)">
<MainSource>MainSource</MainSource>
</DelphiCompile>
<BuildConfiguration Include="Debug">
<Key>Cfg_2</Key>
<CfgParent>Base</CfgParent>
</BuildConfiguration>
<BuildConfiguration Include="Base">
<Key>Base</Key>
</BuildConfiguration>
<BuildConfiguration Include="Release">
<Key>Cfg_1</Key>
<CfgParent>Base</CfgParent>
</BuildConfiguration>
</ItemGroup>
<ProjectExtensions>
<Borland.Personality>Delphi.Personality.12</Borland.Personality>
<Borland.ProjectType/>
<BorlandProject>
<Delphi.Personality>
<Source>
<Source Name="MainSource">cv_Sum.dpr</Source>
</Source>
<VersionInfo>
<VersionInfo Name="IncludeVerInfo">False</VersionInfo>
<VersionInfo Name="AutoIncBuild">False</VersionInfo>
<VersionInfo Name="MajorVer">1</VersionInfo>
<VersionInfo Name="MinorVer">0</VersionInfo>
<VersionInfo Name="Release">0</VersionInfo>
<VersionInfo Name="Build">0</VersionInfo>
<VersionInfo Name="Debug">False</VersionInfo>
<VersionInfo Name="PreRelease">False</VersionInfo>
<VersionInfo Name="Special">False</VersionInfo>
<VersionInfo Name="Private">False</VersionInfo>
<VersionInfo Name="DLL">False</VersionInfo>
<VersionInfo Name="Locale">1049</VersionInfo>
<VersionInfo Name="CodePage">1251</VersionInfo>
</VersionInfo>
<VersionInfoKeys>
<VersionInfoKeys Name="CompanyName"/>
<VersionInfoKeys Name="FileDescription"/>
<VersionInfoKeys Name="FileVersion">1.0.0.0</VersionInfoKeys>
<VersionInfoKeys Name="InternalName"/>
<VersionInfoKeys Name="LegalCopyright"/>
<VersionInfoKeys Name="LegalTrademarks"/>
<VersionInfoKeys Name="OriginalFilename"/>
<VersionInfoKeys Name="ProductName"/>
<VersionInfoKeys Name="ProductVersion">1.0.0.0</VersionInfoKeys>
<VersionInfoKeys Name="Comments"/>
<VersionInfoKeys Name="CFBundleName"/>
<VersionInfoKeys Name="CFBundleDisplayName"/>
<VersionInfoKeys Name="CFBundleIdentifier"/>
<VersionInfoKeys Name="CFBundleVersion"/>
<VersionInfoKeys Name="CFBundlePackageType"/>
<VersionInfoKeys Name="CFBundleSignature"/>
<VersionInfoKeys Name="CFBundleAllowMixedLocalizations"/>
<VersionInfoKeys Name="CFBundleExecutable"/>
</VersionInfoKeys>
</Delphi.Personality>
<Platforms>
<Platform value="OSX32">False</Platform>
<Platform value="Win32">True</Platform>
<Platform value="Win64">False</Platform>
</Platforms>
</BorlandProject>
<ProjectFileVersion>12</ProjectFileVersion>
</ProjectExtensions>
<Import Project="$(BDS)\Bin\CodeGear.Delphi.Targets" Condition="Exists('$(BDS)\Bin\CodeGear.Delphi.Targets')"/>
<Import Project="$(APPDATA)\Embarcadero\$(BDSAPPDATABASEDIR)\$(PRODUCTVERSION)\UserTools.proj" Condition="Exists('$(APPDATA)\Embarcadero\$(BDSAPPDATABASEDIR)\$(PRODUCTVERSION)\UserTools.proj')"/>
</Project>

Binary file not shown.