delphi opencv 物品数量识别

unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,OpenCV,IPL,cvCAm, StdCtrls, ExtCtrls, ComCtrls;
const
  THRESHOLD = 10;     
  THRESHOLD_MAX_VALUE = 255; 
  CONTOUR_MAX_LEVEL = 1; 
  LINE_THICKNESS = 2; 
  LINE_TYPE = 8;  
type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    Button3: TButton;
    Button4: TButton;
    Label1: TLabel;
    Panel1: TPanel;
    Image1: TImage;
    Panel2: TPanel;
    Image2: TImage;
    Panel3: TPanel;
    Image3: TImage;
    Panel4: TPanel;
    Image4: TImage;
    TrackBar1: TTrackBar;
    Edit1: TEdit;
    TrackBar2: TTrackBar;
    Edit2: TEdit;
    Edit3: TEdit;
    Button5: TButton;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Button6: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure TrackBar2Change(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
 src     : pIplImage = 0;  //定义源图像指针
 tmp     : pIplImage = 0;  //定义临时图像指针
 src_back: pIplImage = 0; //定义源图像背景指针
 dst_gray: pIplImage = 0; //定义源文件去掉背景后的目标灰度图像指针
 dst_bw  : pIplImage = 0; //定义源文件去掉背景后的目标二值图像指针
 dst_contours  : pIplImage = 0;  //定义轮廓图像指针
 element  : pIplConvKernel = 0;   //定义形态学结构指针
 Number_Object :integer=0; //定义目标对象数量
 contour_area_tmp :integer = 0; //定义目标对象面积临时寄存器
 contour_area_sum :integer= 0; //定义目标所有对象面积的和
 contour_area_ave :integer= 0; //定义目标对象面积平均值
 contour_area_max :integer= 0; //定义目标对象面积最大值
 stor:   pCvMemStorage   = nil;
 cont:   pCvSeq   = 0;
 cont1:   pCvSeq   = 0;
 Cfont:   CvFont   ;
 C1font:  pCvFont   ;
 a_contour:   pCvSeq = 0;
 capture: PCvCapture;
 frame: PIplImage=0;
 frame1: PIplImage=0;
 MFrame:PIplImage=0;
 image: PIplImage=0;
 g_Gray: PIplImage=0;
 t_Gray: PIplImage=0;
 bmp: TBitmap;
 sz:PCvSize;
  track_box: CvBox2D;
implementation
{$R *.dfm}
 
 
procedure TForm1.TrackBar1Change(Sender: TObject);
begin
edit1.Text:=IntToStr(TrackBar1.Position);
end;
procedure TForm1.TrackBar2Change(Sender: TObject);
begin
edit2.Text:=IntToStr(TrackBar2.Position);
end;

procedure TForm1.Button5Click(Sender: TObject);
var         rec,rec1: TRect;
            rr:CvRect;
            red: CvScalar;
            yellow: CvScalar;
            count:integer;
            area,x,y:integer;
            ptr:pbyte;
            r,g,b:integer;
            OCVfont: CVFont;
begin
r:=StrToInt(Edit4.Text);
g:=StrToInt(Edit5.Text);
b:=StrToInt(Edit6.Text);
edit3.Text:='';
count:=0;
area:=0;
//1.读取和显示图像
  frame := cvQueryFrame( capture );
  if not(assigned(frame) ) then
      exit;
 IF (stor=NIL) then
 begin
 g_Gray:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,1);
 stor := cvCreateMemStorage(0);
 end
 else
 begin
 cvClearMemStorage(stor);
 end;
 
 src:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,3);
 src:=cvCloneImage(frame);
  src_back:=cvCreateImage(cvSize_(frame.Width,frame.Height),IPL_DEPTH_32F,3);
  src_back:=cvCloneImage(frame);
cvLine(frame, cvPoint_(160,40),cvPoint_(460,40), CV_RGB(0,0,0), 120, 8 );
    IplImage2Bitmap(frame, bmp);
    rec := Image1.canvas.ClipRect;
    Image1.canvas.StretchDraw(rec , bmp);
     cvThreshold( frame, frame ,TrackBar1.Position, 255, CV_THRESH_BINARY ); //取阀值为50把图像转为二值图像
     
     cvDilate( frame, frame, 0, 4);
     cvSmooth(frame,frame,CV_MEDIAN,9,9);
     
     IplImage2Bitmap(frame, bmp);
     rec := Image2.canvas.ClipRect;
    Image2.canvas.StretchDraw(rec , bmp);
     cvCvtColor(frame,g_Gray,CV_BGR2GRAY);  //CV_BGR2HSV
     g_Gray.Origin:=1;

      IplImage2Bitmap(g_Gray, bmp);
     rec := Image3.canvas.ClipRect;
    Image3.canvas.StretchDraw(rec , bmp);
     cvThreshold( g_Gray, g_Gray ,TrackBar2.Position, 255, CV_THRESH_BINARY ); //取阀值为50把图像转为二值图像
    cvErode( g_Gray, g_Gray, 0, 2);
    IplImage2Bitmap(g_Gray, bmp);
    rec := Image4.canvas.ClipRect;
    Image4.canvas.StretchDraw(rec , bmp);
    cont := cvCreateSeq(0, sizeof(CvSeq), sizeof(CvPoint), stor);
    Number_Object := cvFindContours( g_Gray, stor, @cont, sizeof(CvContour),
    CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint_(0,0) ); //找到所有轮廓
    label1.Caption:= IntToStr(Number_Object);
  red := CV_RGB(255, 0, 0);
 // cvDrawContours( src, cont,red,red ,CONTOUR_MAX_LEVEL, LINE_THICKNESS,LINE_TYPE,CvPoint_(0, 0));
 cvInitFont(@OCVfont, CV_FONT_VECTOR0, 0.5, 0.5, 0, 1,16);
  while cont <> Nil do begin
   rr:=pCvContour(cont).Rect;
   cvRectangle(src, cvPoint_(rr.x,rr.y), cvPoint_(rr.x +rr.width,rr.y+rr.height),CV_RGB(0,255,0),1,8,0);

   area:=cont.total;// cvContourArea(cont,CV_WHOLE_SEQ ) ;
   edit3.Text:=edit3.Text+':'+intToStr(area);
   count:=count+1;
   cont := cont.h_next;
   if cont=nil then cvInitFont(@OCVfont, CV_FONT_VECTOR0, 0.5, 0.5, 0, 2,16);
   cvPutText(src,Pchar(IntToStr(count)),cvPoint_(rr.x+15,rr.y+20), @OCVfont,CV_RGB(0,0,225));
  if (area<30) or (area>75) then
   begin
    showmessage('图像面积有不合法的');
    exit;
   end;
   end;

// cvInitFont(@Cfont,CV_FONT_HERSHEY_SCRIPT_SIMPLEX ,1.0,1.0,0.0, 1, 16); //CV_FONT_HERSHEY_SCRIPT_SIMPLEX
// cvPutText(frame,Pchar('easy'),cvPoint_(400,450), @Cfont,CV_RGB(0,255,0));
    IplImage2Bitmap(src, bmp);
    rec := Image1.canvas.ClipRect;
    Image1.canvas.StretchDraw(rec , bmp);
    //image1.Canvas.Rectangle
 
end;
 
 
 
procedure TForm1.Button1Click(Sender: TObject);
var         rec,rec1: TRect;
            rr:CvRect;
            red: CvScalar;
            yellow: CvScalar;
            count:integer;
            area,x,y:integer;
            ptr:pbyte;
            r,g,b:integer;
            OCVfont: CVFont;
begin
r:=StrToInt(Edit4.Text);
g:=StrToInt(Edit5.Text);
b:=StrToInt(Edit6.Text);
edit3.Text:='';
count:=0;
area:=0;
//1.读取和显示图像
  frame1 := cvQueryFrame( capture );
  if not(assigned(frame1) ) then   exit;
 src:=cvCreateImage(cvSize_(frame1.Width*2,frame1.Height*2),8,3);
 cvResize(frame1,src,CV_INTER_lINEAR);
 src.Origin:=1;
 frame:=cvCloneImage(frame1);
 g_Gray:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,1);
 t_Gray:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,1);
 G_Frame:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,1);
 HSVframe:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,3);
 IF (stor=NIL) then
 begin
 stor := cvCreateMemStorage(0);
 end
 else
 begin
 cvClearMemStorage(stor);
 end;
  src_back:=cvCreateImage(cvSize_(frame.Width,frame.Height),IPL_DEPTH_32F,3);
  src_back:=cvCloneImage(frame);
cvLine(frame, cvPoint_(160,40),cvPoint_(460,40), CV_RGB(0,0,0), 120, 8 );
{
cvFloodFill( frame, seed, brightness, cvRealScalar(lo),
                             cvRealScalar(up), &comp, flags, is_mask ? mask : NULL );
}
     cvThreshold( frame, frame ,TrackBar1.Position, 255, CV_THRESH_BINARY ); //取阀值为50把图像转为二值图像
    // cvSmooth(frame,frame,cv_Gaussian,9,9);
     cvDilate( frame, frame, 0, 4);
     cvErode( frame, frame, 0, 2);
     cvDilate( frame, frame, 0, 3);
     cvErode( frame, frame, 0, 2);
     cvSmooth(frame,frame,CV_MEDIAN,9,9);

     IplImage2Bitmap(frame, bmp);
     rec := Image2.canvas.ClipRect;
    Image2.canvas.StretchDraw(rec , bmp);
     cvCvtColor(frame,g_Gray,CV_BGR2GRAY);  //CV_BGR2HSV
     g_Gray.Origin:=1;
// cvLaplace(g_Gray,t_Gray,1);
cvEqualizeHist(g_Gray,t_Gray);
t_Gray.Origin:=1;
 

      IplImage2Bitmap(t_Gray, bmp);
     rec := Image3.canvas.ClipRect;
    Image3.canvas.StretchDraw(rec , bmp);
cvSmooth(t_Gray,t_Gray,cv_Gaussian,9,9);
     cvThreshold( t_Gray, t_Gray ,TrackBar2.Position, 255, CV_THRESH_BINARY ); //取阀值为50把图像转为二值图像
    cvErode( t_Gray, t_Gray, 0, 2);
    IplImage2Bitmap(t_Gray, bmp);
    rec := Image4.canvas.ClipRect;
    Image4.canvas.StretchDraw(rec , bmp);
    cont := cvCreateSeq(0, sizeof(CvSeq), sizeof(CvPoint), stor);
    Number_Object := cvFindContours( t_Gray, stor, @cont, sizeof(CvContour),
    CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint_(0,0) ); //找到所有轮廓
    label1.Caption:= IntToStr(Number_Object);
  red := CV_RGB(255, 0, 0);
 // cvDrawContours( src, cont,red,red ,CONTOUR_MAX_LEVEL, LINE_THICKNESS,LINE_TYPE,CvPoint_(0, 0));
 cvInitFont(@OCVfont, CV_FONT_VECTOR0, 1.0, 1.0, 0, 1,16);
  while cont <> Nil do begin
   rr:=pCvContour(cont).Rect;
  area:=cont.total;
  if (area<30) or (area>75) then
   begin
   cvRectangle(src_back, cvPoint_(rr.x,rr.y), cvPoint_(rr.x +rr.width,rr.y+rr.height),CV_RGB(255,0,0),2,8,0);
   edit7.Text:=Edit7.Text+':'+IntToStr(area);
    //showmessage('图像面积有不合法的');
   end;// else
  // cvRectangle(src_back, cvPoint_(rr.x,rr.y), cvPoint_(rr.x +rr.width,rr.y+rr.height),CV_RGB(0,255,0),2,8,0);
   // cvContourArea(cont,CV_WHOLE_SEQ ) ;
   edit3.Text:=edit3.Text+':'+intToStr(area);
   count:=count+1;
   cont := cont.h_next;
   if cont=nil then
   begin
   cvInitFont(@OCVfont, CV_FONT_VECTOR0, 1.0, 1.0, 0, 2,16);
   cvPutText(src_back,Pchar(IntToStr(count)),cvPoint_(rr.x+15,rr.y+15), @OCVfont,CV_RGB(255,0,0));
   end else   cvPutText(src_back,Pchar(IntToStr(count)),cvPoint_(rr.x+15,rr.y+15), @OCVfont,CV_RGB(255,255,225));
   end;
    IplImage2Bitmap(src_back, bmp);
    rec := Image1.canvas.ClipRect;
    Image1.canvas.StretchDraw(rec , bmp);
//frame1   src  frame  g_Gray  src_back
//cvReleaseImage(g_Gray);
//cvReleaseCapture(@capture);
cvReleaseImage(src);
cvReleaseImage(frame);
cvReleaseImage(src_back);
cvReleaseImage(g_Gray);
cvReleaseImage(t_Gray);
    //image1.Canvas.Rectangle   g_Gray
end;
 
 
end.


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,OpenCV,IPL,cvCAm, StdCtrls, ExtCtrls, ComCtrls;

const
  THRESHOLD = 10;     
  THRESHOLD_MAX_VALUE = 255; 

  CONTOUR_MAX_LEVEL = 1; 
  LINE_THICKNESS = 2; 
  LINE_TYPE = 8;  

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    Button3: TButton;
    Button4: TButton;
    Label1: TLabel;
    Panel1: TPanel;
    Image1: TImage;
    Panel2: TPanel;
    Image2: TImage;
    Panel3: TPanel;
    Image3: TImage;
    Panel4: TPanel;
    Image4: TImage;
    TrackBar1: TTrackBar;
    Edit1: TEdit;
    TrackBar2: TTrackBar;
    Edit2: TEdit;
    Edit3: TEdit;
    Button5: TButton;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Button6: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure TrackBar2Change(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

 src     : pIplImage = 0;  //定义源图像指针
 tmp     : pIplImage = 0;  //定义临时图像指针
 src_back: pIplImage = 0; //定义源图像背景指针
 dst_gray: pIplImage = 0; //定义源文件去掉背景后的目标灰度图像指针
 dst_bw  : pIplImage = 0; //定义源文件去掉背景后的目标二值图像指针
 dst_contours  : pIplImage = 0;  //定义轮廓图像指针
 element  : pIplConvKernel = 0;   //定义形态学结构指针
 Number_Object :integer=0; //定义目标对象数量
 contour_area_tmp :integer = 0; //定义目标对象面积临时寄存器
 contour_area_sum :integer= 0; //定义目标所有对象面积的和
 contour_area_ave :integer= 0; //定义目标对象面积平均值
 contour_area_max :integer= 0; //定义目标对象面积最大值

 stor:   pCvMemStorage   = nil;
 cont:   pCvSeq   = 0;
 cont1:   pCvSeq   = 0;
 Cfont:   CvFont   ;
 C1font:  pCvFont   ;
 a_contour:   pCvSeq = 0;
 capture: PCvCapture;
 frame: PIplImage=0;
 frame1: PIplImage=0;
 MFrame:PIplImage=0;
 image: PIplImage=0;
 g_Gray: PIplImage=0;
 t_Gray: PIplImage=0;
 bmp: TBitmap;
 sz:PCvSize;
  track_box: CvBox2D;
implementation

{$R *.dfm}

 

 

procedure TForm1.TrackBar1Change(Sender: TObject);
begin

edit1.Text:=IntToStr(TrackBar1.Position);
end;

procedure TForm1.TrackBar2Change(Sender: TObject);
begin
edit2.Text:=IntToStr(TrackBar2.Position);
end;


procedure TForm1.Button5Click(Sender: TObject);
var         rec,rec1: TRect;
            rr:CvRect;
            red: CvScalar;
            yellow: CvScalar;
            count:integer;
            area,x,y:integer;
            ptr:pbyte;
            r,g,b:integer;
            OCVfont: CVFont;
begin
r:=StrToInt(Edit4.Text);
g:=StrToInt(Edit5.Text);
b:=StrToInt(Edit6.Text);
edit3.Text:='';
count:=0;
area:=0;

//1.读取和显示图像

  frame := cvQueryFrame( capture );
  if not(assigned(frame) ) then
      exit;

 IF (stor=NIL) then
 begin

 g_Gray:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,1);
 stor := cvCreateMemStorage(0);
 end
 else
 begin
 cvClearMemStorage(stor);
 end;

 

 src:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,3);
 src:=cvCloneImage(frame);

  src_back:=cvCreateImage(cvSize_(frame.Width,frame.Height),IPL_DEPTH_32F,3);
  src_back:=cvCloneImage(frame);

cvLine(frame, cvPoint_(160,40),cvPoint_(460,40), CV_RGB(0,0,0), 120, 8 );

    IplImage2Bitmap(frame, bmp);
    rec := Image1.canvas.ClipRect;
    Image1.canvas.StretchDraw(rec , bmp);

     cvThreshold( frame, frame ,TrackBar1.Position, 255, CV_THRESH_BINARY ); //取阀值为50把图像转为二值图像
    
     cvDilate( frame, frame, 0, 4);
     cvSmooth(frame,frame,CV_MEDIAN,9,9);
    
     IplImage2Bitmap(frame, bmp);
     rec := Image2.canvas.ClipRect;
    Image2.canvas.StretchDraw(rec , bmp);

     cvCvtColor(frame,g_Gray,CV_BGR2GRAY);  //CV_BGR2HSV
     g_Gray.Origin:=1;


      IplImage2Bitmap(g_Gray, bmp);
     rec := Image3.canvas.ClipRect;
    Image3.canvas.StretchDraw(rec , bmp);

     cvThreshold( g_Gray, g_Gray ,TrackBar2.Position, 255, CV_THRESH_BINARY ); //取阀值为50把图像转为二值图像
    cvErode( g_Gray, g_Gray, 0, 2);
    IplImage2Bitmap(g_Gray, bmp);
    rec := Image4.canvas.ClipRect;
    Image4.canvas.StretchDraw(rec , bmp);

    cont := cvCreateSeq(0, sizeof(CvSeq), sizeof(CvPoint), stor);
    Number_Object := cvFindContours( g_Gray, stor, @cont, sizeof(CvContour),
    CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint_(0,0) ); //找到所有轮廓
    label1.Caption:= IntToStr(Number_Object);

  red := CV_RGB(255, 0, 0);
 // cvDrawContours( src, cont,red,red ,CONTOUR_MAX_LEVEL, LINE_THICKNESS,LINE_TYPE,CvPoint_(0, 0));

 cvInitFont(@OCVfont, CV_FONT_VECTOR0, 0.5, 0.5, 0, 1,16);

  while cont <> Nil do begin
   rr:=pCvContour(cont).Rect;

   cvRectangle(src, cvPoint_(rr.x,rr.y), cvPoint_(rr.x +rr.width,rr.y+rr.height),CV_RGB(0,255,0),1,8,0);


   area:=cont.total;// cvContourArea(cont,CV_WHOLE_SEQ ) ;
   edit3.Text:=edit3.Text+':'+intToStr(area);
   count:=count+1;

   cont := cont.h_next;
   if cont=nil then cvInitFont(@OCVfont, CV_FONT_VECTOR0, 0.5, 0.5, 0, 2,16);
   cvPutText(src,Pchar(IntToStr(count)),cvPoint_(rr.x+15,rr.y+20), @OCVfont,CV_RGB(0,0,225));
  if (area<30) or (area>75) then
   begin
    showmessage('图像面积有不合法的');
    exit;
   end;
   end;

// cvInitFont(@Cfont,CV_FONT_HERSHEY_SCRIPT_SIMPLEX ,1.0,1.0,0.0, 1, 16); //CV_FONT_HERSHEY_SCRIPT_SIMPLEX
// cvPutText(frame,Pchar('easy'),cvPoint_(400,450), @Cfont,CV_RGB(0,255,0));

    IplImage2Bitmap(src, bmp);
    rec := Image1.canvas.ClipRect;
    Image1.canvas.StretchDraw(rec , bmp);

    //image1.Canvas.Rectangle

 

end;

 

 

 

procedure TForm1.Button1Click(Sender: TObject);
var         rec,rec1: TRect;
            rr:CvRect;
            red: CvScalar;
            yellow: CvScalar;
            count:integer;
            area,x,y:integer;
            ptr:pbyte;
            r,g,b:integer;
            OCVfont: CVFont;
begin
r:=StrToInt(Edit4.Text);
g:=StrToInt(Edit5.Text);
b:=StrToInt(Edit6.Text);
edit3.Text:='';
count:=0;
area:=0;

//1.读取和显示图像

  frame1 := cvQueryFrame( capture );
  if not(assigned(frame1) ) then   exit;

 src:=cvCreateImage(cvSize_(frame1.Width*2,frame1.Height*2),8,3);
 cvResize(frame1,src,CV_INTER_lINEAR);
 src.Origin:=1;

 frame:=cvCloneImage(frame1);

 g_Gray:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,1);
 t_Gray:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,1);
 G_Frame:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,1);
 HSVframe:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,3);

 IF (stor=NIL) then
 begin
 stor := cvCreateMemStorage(0);
 end
 else
 begin
 cvClearMemStorage(stor);
 end;

  src_back:=cvCreateImage(cvSize_(frame.Width,frame.Height),IPL_DEPTH_32F,3);
  src_back:=cvCloneImage(frame);

cvLine(frame, cvPoint_(160,40),cvPoint_(460,40), CV_RGB(0,0,0), 120, 8 );

{
cvFloodFill( frame, seed, brightness, cvRealScalar(lo),
                             cvRealScalar(up), &comp, flags, is_mask ? mask : NULL );
}

     cvThreshold( frame, frame ,TrackBar1.Position, 255, CV_THRESH_BINARY ); //取阀值为50把图像转为二值图像
    // cvSmooth(frame,frame,cv_Gaussian,9,9);
     cvDilate( frame, frame, 0, 4);
     cvErode( frame, frame, 0, 2);
     cvDilate( frame, frame, 0, 3);
     cvErode( frame, frame, 0, 2);

     cvSmooth(frame,frame,CV_MEDIAN,9,9);


     IplImage2Bitmap(frame, bmp);
     rec := Image2.canvas.ClipRect;
    Image2.canvas.StretchDraw(rec , bmp);

     cvCvtColor(frame,g_Gray,CV_BGR2GRAY);  //CV_BGR2HSV
     g_Gray.Origin:=1;

// cvLaplace(g_Gray,t_Gray,1);

cvEqualizeHist(g_Gray,t_Gray);
t_Gray.Origin:=1;

 


      IplImage2Bitmap(t_Gray, bmp);
     rec := Image3.canvas.ClipRect;
    Image3.canvas.StretchDraw(rec , bmp);

cvSmooth(t_Gray,t_Gray,cv_Gaussian,9,9);

     cvThreshold( t_Gray, t_Gray ,TrackBar2.Position, 255, CV_THRESH_BINARY ); //取阀值为50把图像转为二值图像
    cvErode( t_Gray, t_Gray, 0, 2);

    IplImage2Bitmap(t_Gray, bmp);
    rec := Image4.canvas.ClipRect;
    Image4.canvas.StretchDraw(rec , bmp);

    cont := cvCreateSeq(0, sizeof(CvSeq), sizeof(CvPoint), stor);
    Number_Object := cvFindContours( t_Gray, stor, @cont, sizeof(CvContour),
    CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint_(0,0) ); //找到所有轮廓
    label1.Caption:= IntToStr(Number_Object);

  red := CV_RGB(255, 0, 0);
 // cvDrawContours( src, cont,red,red ,CONTOUR_MAX_LEVEL, LINE_THICKNESS,LINE_TYPE,CvPoint_(0, 0));

 cvInitFont(@OCVfont, CV_FONT_VECTOR0, 1.0, 1.0, 0, 1,16);

  while cont <> Nil do begin
   rr:=pCvContour(cont).Rect;

  area:=cont.total;
  if (area<30) or (area>75) then
   begin
   cvRectangle(src_back, cvPoint_(rr.x,rr.y), cvPoint_(rr.x +rr.width,rr.y+rr.height),CV_RGB(255,0,0),2,8,0);
   edit7.Text:=Edit7.Text+':'+IntToStr(area);
    //showmessage('图像面积有不合法的');

   end;// else
  // cvRectangle(src_back, cvPoint_(rr.x,rr.y), cvPoint_(rr.x +rr.width,rr.y+rr.height),CV_RGB(0,255,0),2,8,0);

   // cvContourArea(cont,CV_WHOLE_SEQ ) ;
   edit3.Text:=edit3.Text+':'+intToStr(area);
   count:=count+1;

   cont := cont.h_next;
   if cont=nil then
   begin
   cvInitFont(@OCVfont, CV_FONT_VECTOR0, 1.0, 1.0, 0, 2,16);
   cvPutText(src_back,Pchar(IntToStr(count)),cvPoint_(rr.x+15,rr.y+15), @OCVfont,CV_RGB(255,0,0));
   end else   cvPutText(src_back,Pchar(IntToStr(count)),cvPoint_(rr.x+15,rr.y+15), @OCVfont,CV_RGB(255,255,225));

   end;

    IplImage2Bitmap(src_back, bmp);
    rec := Image1.canvas.ClipRect;
    Image1.canvas.StretchDraw(rec , bmp);
//frame1   src  frame  g_Gray  src_back
//cvReleaseImage(g_Gray);
//cvReleaseCapture(@capture);

cvReleaseImage(src);
cvReleaseImage(frame);
cvReleaseImage(src_back);
cvReleaseImage(g_Gray);
cvReleaseImage(t_Gray);
    //image1.Canvas.Rectangle   g_Gray

end;

 

 

end.

你可能感兴趣的:(OpenCV)