当前位置:   article > 正文

delphi opencv 物品数量识别_delphi opencv 货物识别

delphi opencv 货物识别

  1. unit Unit1;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs,OpenCV,IPL,cvCAm, StdCtrls, ExtCtrls, ComCtrls;
  6. const
  7. THRESHOLD = 10;
  8. THRESHOLD_MAX_VALUE = 255;
  9. CONTOUR_MAX_LEVEL = 1;
  10. LINE_THICKNESS = 2;
  11. LINE_TYPE = 8;
  12. type
  13. TForm1 = class(TForm)
  14. Button1: TButton;
  15. Button2: TButton;
  16. Memo1: TMemo;
  17. Button3: TButton;
  18. Button4: TButton;
  19. Label1: TLabel;
  20. Panel1: TPanel;
  21. Image1: TImage;
  22. Panel2: TPanel;
  23. Image2: TImage;
  24. Panel3: TPanel;
  25. Image3: TImage;
  26. Panel4: TPanel;
  27. Image4: TImage;
  28. TrackBar1: TTrackBar;
  29. Edit1: TEdit;
  30. TrackBar2: TTrackBar;
  31. Edit2: TEdit;
  32. Edit3: TEdit;
  33. Button5: TButton;
  34. Edit4: TEdit;
  35. Edit5: TEdit;
  36. Edit6: TEdit;
  37. Button6: TButton;
  38. procedure Button1Click(Sender: TObject);
  39. procedure Button2Click(Sender: TObject);
  40. procedure Button3Click(Sender: TObject);
  41. procedure FormCreate(Sender: TObject);
  42. procedure Button4Click(Sender: TObject);
  43. procedure TrackBar1Change(Sender: TObject);
  44. procedure TrackBar2Change(Sender: TObject);
  45. procedure Button5Click(Sender: TObject);
  46. procedure Button6Click(Sender: TObject);
  47. private
  48. { Private declarations }
  49. public
  50. { Public declarations }
  51. end;
  52. var
  53. Form1: TForm1;
  54. src : pIplImage = 0; //定义源图像指针
  55. tmp : pIplImage = 0; //定义临时图像指针
  56. src_back: pIplImage = 0; //定义源图像背景指针
  57. dst_gray: pIplImage = 0; //定义源文件去掉背景后的目标灰度图像指针
  58. dst_bw : pIplImage = 0; //定义源文件去掉背景后的目标二值图像指针
  59. dst_contours : pIplImage = 0; //定义轮廓图像指针
  60. element : pIplConvKernel = 0; //定义形态学结构指针
  61. Number_Object :integer=0; //定义目标对象数量
  62. contour_area_tmp :integer = 0; //定义目标对象面积临时寄存器
  63. contour_area_sum :integer= 0; //定义目标所有对象面积的和
  64. contour_area_ave :integer= 0; //定义目标对象面积平均值
  65. contour_area_max :integer= 0; //定义目标对象面积最大值
  66. stor: pCvMemStorage = nil;
  67. cont: pCvSeq = 0;
  68. cont1: pCvSeq = 0;
  69. Cfont: CvFont ;
  70. C1font: pCvFont ;
  71. a_contour: pCvSeq = 0;
  72. capture: PCvCapture;
  73. frame: PIplImage=0;
  74. frame1: PIplImage=0;
  75. MFrame:PIplImage=0;
  76. image: PIplImage=0;
  77. g_Gray: PIplImage=0;
  78. t_Gray: PIplImage=0;
  79. bmp: TBitmap;
  80. sz:PCvSize;
  81. track_box: CvBox2D;
  82. implementation
  83. {$R *.dfm}
  84. procedure TForm1.TrackBar1Change(Sender: TObject);
  85. begin
  86. edit1.Text:=IntToStr(TrackBar1.Position);
  87. end;
  88. procedure TForm1.TrackBar2Change(Sender: TObject);
  89. begin
  90. edit2.Text:=IntToStr(TrackBar2.Position);
  91. end;
  92. procedure TForm1.Button5Click(Sender: TObject);
  93. var rec,rec1: TRect;
  94. rr:CvRect;
  95. red: CvScalar;
  96. yellow: CvScalar;
  97. count:integer;
  98. area,x,y:integer;
  99. ptr:pbyte;
  100. r,g,b:integer;
  101. OCVfont: CVFont;
  102. begin
  103. r:=StrToInt(Edit4.Text);
  104. g:=StrToInt(Edit5.Text);
  105. b:=StrToInt(Edit6.Text);
  106. edit3.Text:='';
  107. count:=0;
  108. area:=0;
  109. //1.读取和显示图像
  110. frame := cvQueryFrame( capture );
  111. if not(assigned(frame) ) then
  112. exit;
  113. IF (stor=NIL) then
  114. begin
  115. g_Gray:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,1);
  116. stor := cvCreateMemStorage(0);
  117. end
  118. else
  119. begin
  120. cvClearMemStorage(stor);
  121. end;
  122. src:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,3);
  123. src:=cvCloneImage(frame);
  124. src_back:=cvCreateImage(cvSize_(frame.Width,frame.Height),IPL_DEPTH_32F,3);
  125. src_back:=cvCloneImage(frame);
  126. cvLine(frame, cvPoint_(160,40),cvPoint_(460,40), CV_RGB(0,0,0), 120, 8 );
  127. IplImage2Bitmap(frame, bmp);
  128. rec := Image1.canvas.ClipRect;
  129. Image1.canvas.StretchDraw(rec , bmp);
  130. cvThreshold( frame, frame ,TrackBar1.Position, 255, CV_THRESH_BINARY ); //取阀值为50把图像转为二值图像
  131. cvDilate( frame, frame, 0, 4);
  132. cvSmooth(frame,frame,CV_MEDIAN,9,9);
  133. IplImage2Bitmap(frame, bmp);
  134. rec := Image2.canvas.ClipRect;
  135. Image2.canvas.StretchDraw(rec , bmp);
  136. cvCvtColor(frame,g_Gray,CV_BGR2GRAY); //CV_BGR2HSV
  137. g_Gray.Origin:=1;
  138. IplImage2Bitmap(g_Gray, bmp);
  139. rec := Image3.canvas.ClipRect;
  140. Image3.canvas.StretchDraw(rec , bmp);
  141. cvThreshold( g_Gray, g_Gray ,TrackBar2.Position, 255, CV_THRESH_BINARY ); //取阀值为50把图像转为二值图像
  142. cvErode( g_Gray, g_Gray, 0, 2);
  143. IplImage2Bitmap(g_Gray, bmp);
  144. rec := Image4.canvas.ClipRect;
  145. Image4.canvas.StretchDraw(rec , bmp);
  146. cont := cvCreateSeq(0, sizeof(CvSeq), sizeof(CvPoint), stor);
  147. Number_Object := cvFindContours( g_Gray, stor, @cont, sizeof(CvContour),
  148. CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint_(0,0) ); //找到所有轮廓
  149. label1.Caption:= IntToStr(Number_Object);
  150. red := CV_RGB(255, 0, 0);
  151. // cvDrawContours( src, cont,red,red ,CONTOUR_MAX_LEVEL, LINE_THICKNESS,LINE_TYPE,CvPoint_(0, 0));
  152. cvInitFont(@OCVfont, CV_FONT_VECTOR0, 0.5, 0.5, 0, 1,16);
  153. while cont <> Nil do begin
  154. rr:=pCvContour(cont).Rect;
  155. cvRectangle(src, cvPoint_(rr.x,rr.y), cvPoint_(rr.x +rr.width,rr.y+rr.height),CV_RGB(0,255,0),1,8,0);
  156. area:=cont.total;// cvContourArea(cont,CV_WHOLE_SEQ ) ;
  157. edit3.Text:=edit3.Text+':'+intToStr(area);
  158. count:=count+1;
  159. cont := cont.h_next;
  160. if cont=nil then cvInitFont(@OCVfont, CV_FONT_VECTOR0, 0.5, 0.5, 0, 2,16);
  161. cvPutText(src,Pchar(IntToStr(count)),cvPoint_(rr.x+15,rr.y+20), @OCVfont,CV_RGB(0,0,225));
  162. if (area<30) or (area>75) then
  163. begin
  164. showmessage('图像面积有不合法的');
  165. exit;
  166. end;
  167. end;
  168. // cvInitFont(@Cfont,CV_FONT_HERSHEY_SCRIPT_SIMPLEX ,1.0,1.0,0.0, 1, 16); //CV_FONT_HERSHEY_SCRIPT_SIMPLEX
  169. // cvPutText(frame,Pchar('easy'),cvPoint_(400,450), @Cfont,CV_RGB(0,255,0));
  170. IplImage2Bitmap(src, bmp);
  171. rec := Image1.canvas.ClipRect;
  172. Image1.canvas.StretchDraw(rec , bmp);
  173. //image1.Canvas.Rectangle
  174. end;
  175. procedure TForm1.Button1Click(Sender: TObject);
  176. var rec,rec1: TRect;
  177. rr:CvRect;
  178. red: CvScalar;
  179. yellow: CvScalar;
  180. count:integer;
  181. area,x,y:integer;
  182. ptr:pbyte;
  183. r,g,b:integer;
  184. OCVfont: CVFont;
  185. begin
  186. r:=StrToInt(Edit4.Text);
  187. g:=StrToInt(Edit5.Text);
  188. b:=StrToInt(Edit6.Text);
  189. edit3.Text:='';
  190. count:=0;
  191. area:=0;
  192. //1.读取和显示图像
  193. frame1 := cvQueryFrame( capture );
  194. if not(assigned(frame1) ) then exit;
  195. src:=cvCreateImage(cvSize_(frame1.Width*2,frame1.Height*2),8,3);
  196. cvResize(frame1,src,CV_INTER_lINEAR);
  197. src.Origin:=1;
  198. frame:=cvCloneImage(frame1);
  199. g_Gray:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,1);
  200. t_Gray:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,1);
  201. G_Frame:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,1);
  202. HSVframe:=cvCreateImage(cvSize_(frame.Width,frame.Height),8,3);
  203. IF (stor=NIL) then
  204. begin
  205. stor := cvCreateMemStorage(0);
  206. end
  207. else
  208. begin
  209. cvClearMemStorage(stor);
  210. end;
  211. src_back:=cvCreateImage(cvSize_(frame.Width,frame.Height),IPL_DEPTH_32F,3);
  212. src_back:=cvCloneImage(frame);
  213. cvLine(frame, cvPoint_(160,40),cvPoint_(460,40), CV_RGB(0,0,0), 120, 8 );
  214. {
  215. cvFloodFill( frame, seed, brightness, cvRealScalar(lo),
  216. cvRealScalar(up), &comp, flags, is_mask ? mask : NULL );
  217. }
  218. cvThreshold( frame, frame ,TrackBar1.Position, 255, CV_THRESH_BINARY ); //取阀值为50把图像转为二值图像
  219. // cvSmooth(frame,frame,cv_Gaussian,9,9);
  220. cvDilate( frame, frame, 0, 4);
  221. cvErode( frame, frame, 0, 2);
  222. cvDilate( frame, frame, 0, 3);
  223. cvErode( frame, frame, 0, 2);
  224. cvSmooth(frame,frame,CV_MEDIAN,9,9);
  225. IplImage2Bitmap(frame, bmp);
  226. rec := Image2.canvas.ClipRect;
  227. Image2.canvas.StretchDraw(rec , bmp);
  228. cvCvtColor(frame,g_Gray,CV_BGR2GRAY); //CV_BGR2HSV
  229. g_Gray.Origin:=1;
  230. // cvLaplace(g_Gray,t_Gray,1);
  231. cvEqualizeHist(g_Gray,t_Gray);
  232. t_Gray.Origin:=1;
  233. IplImage2Bitmap(t_Gray, bmp);
  234. rec := Image3.canvas.ClipRect;
  235. Image3.canvas.StretchDraw(rec , bmp);
  236. cvSmooth(t_Gray,t_Gray,cv_Gaussian,9,9);
  237. cvThreshold( t_Gray, t_Gray ,TrackBar2.Position, 255, CV_THRESH_BINARY ); //取阀值为50把图像转为二值图像
  238. cvErode( t_Gray, t_Gray, 0, 2);
  239. IplImage2Bitmap(t_Gray, bmp);
  240. rec := Image4.canvas.ClipRect;
  241. Image4.canvas.StretchDraw(rec , bmp);
  242. cont := cvCreateSeq(0, sizeof(CvSeq), sizeof(CvPoint), stor);
  243. Number_Object := cvFindContours( t_Gray, stor, @cont, sizeof(CvContour),
  244. CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint_(0,0) ); //找到所有轮廓
  245. label1.Caption:= IntToStr(Number_Object);
  246. red := CV_RGB(255, 0, 0);
  247. // cvDrawContours( src, cont,red,red ,CONTOUR_MAX_LEVEL, LINE_THICKNESS,LINE_TYPE,CvPoint_(0, 0));
  248. cvInitFont(@OCVfont, CV_FONT_VECTOR0, 1.0, 1.0, 0, 1,16);
  249. while cont <> Nil do begin
  250. rr:=pCvContour(cont).Rect;
  251. area:=cont.total;
  252. if (area<30) or (area>75) then
  253. begin
  254. 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);
  255. edit7.Text:=Edit7.Text+':'+IntToStr(area);
  256. //showmessage('图像面积有不合法的');
  257. end;// else
  258. // 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);
  259. // cvContourArea(cont,CV_WHOLE_SEQ ) ;
  260. edit3.Text:=edit3.Text+':'+intToStr(area);
  261. count:=count+1;
  262. cont := cont.h_next;
  263. if cont=nil then
  264. begin
  265. cvInitFont(@OCVfont, CV_FONT_VECTOR0, 1.0, 1.0, 0, 2,16);
  266. cvPutText(src_back,Pchar(IntToStr(count)),cvPoint_(rr.x+15,rr.y+15), @OCVfont,CV_RGB(255,0,0));
  267. end else cvPutText(src_back,Pchar(IntToStr(count)),cvPoint_(rr.x+15,rr.y+15), @OCVfont,CV_RGB(255,255,225));
  268. end;
  269. IplImage2Bitmap(src_back, bmp);
  270. rec := Image1.canvas.ClipRect;
  271. Image1.canvas.StretchDraw(rec , bmp);
  272. //frame1 src frame g_Gray src_back
  273. //cvReleaseImage(g_Gray);
  274. //cvReleaseCapture(@capture);
  275. cvReleaseImage(src);
  276. cvReleaseImage(frame);
  277. cvReleaseImage(src_back);
  278. cvReleaseImage(g_Gray);
  279. cvReleaseImage(t_Gray);
  280. //image1.Canvas.Rectangle g_Gray
  281. end;
  282. 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.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/580829
推荐阅读
相关标签
  

闽ICP备14008679号