所有新聞群組討論區列表 風禹科技驗證有限公司 Web News Reader

目前新聞群組:tw.bbs.comp.language

項目 內容
發文者 @_@
日期 2011/7/20 下午 03:21:41
標題 補上程式
Message Id 56lXZa$7qL@ptt.cc
內文 ※ 引述《SUPERERIC (@_@)》之銘言:
: 現在正用OpenCV 寫影像辨識,參考OpenCV中文網站
: http://www.opencv.org.cn/index.php/Cv%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86#Smooth
: 看了些資料,都說到第一步要平滑化(smooth)
: 在OpenCV中文網站提到的OpenCV
: 那裏雖然提到基本語法,但沒有範例
: 請問有Visual C++ OpenCV 平滑化的範例嗎?
: 而且我要做的是影片辨識,不知道處理法跟靜態辨識差異在哪?

現在我找到一系列:二值化、灰階化、取背、取輪廓圖像的程式

http://ppt.cc/m5hr

我是利用Visual C++ 配合OpenCV

現在我想要輸入一個"動態影像"去做辨識

請問我該從何下手(我想要一秒大概抓30個圖片)

我想問:

1.如何抓到檔案

2.如何建立記憶體空間

3.抓到我想要顏色、輪廓

以下是程式(我用一個button觸發)
抱歉!可能閱讀困難,不好意思,稍後附上排版好的原檔
#pragma once
#include "stdafx.h"#include <stdlib.h>#include <math.h>#include
<string>#include <stdio.h>#include <cv.h>#include <highgui.h>#include
<direct.h>

CvCapture *open_webcam;
IplImage *webcam;
IplImage *open_Image;

         int argc;
         char* argv[50];


    IplImage *src = 0; //定義源圖像指針

    IplImage *tmp = 0; //定義臨時圖像指針

    IplImage *src_back = 0; //定義源圖像背景指針

    IplImage *dst_gray = 0; //定義源文件去掉背景後的目標灰度圖像指針

    IplImage *dst_bw = 0; //定義源文件去掉背景後的目標二值圖像指針

    IplImage *dst_contours = 0; //定義輪廓圖像指針

    IplConvKernel *element = 0; //定義形態學結構指針

    int Number_Object =0; //定義目標對象數量

    int contour_area_tmp = 0; //定義目標對象面積臨時寄存器

    int contour_area_sum = 0; //定義目標所有對象面積的和

    int contour_area_ave = 0; //定義目標對象面積平均值

    int contour_area_max = 0; //定義目標對象面積最大值

    CvMemStorage *stor = 0;
    CvSeq * cont = 0;
    CvContourScanner contour_scanner;
    CvSeq * a_contour= 0;

namespace OPENCV {


        using namespace System;
        using namespace System::ComponentModel;
        using namespace System::Collections;
        using namespace System::Windows::Forms;
        using namespace System::Data;
        using namespace System::Drawing;



        /// <summary>
        /// Form1 的摘要
        ///
        /// 警告: 如果您變更這個類別的名稱,就必須變更與這個類別所依據之所有 .resx 檔
案關聯的
        ///          Managed 資源編譯器工具的 'Resource File Name' 屬性。
        ///          否則,這些設計工具
        ///          將無法與這個表單關聯的當地語系化資源
        ///          正確互動。
        /// </summary>
        public ref class Form1 : public System::Windows::Forms::Form
        {
        public:
                Form1(void)
                {
                        InitializeComponent();
                        //
                        //TODO: 在此加入建構函式程式碼
                        //
                }

        protected:
                /// <summary>
                /// 清除任何使用中的資源。
                /// </summary>
                ~Form1()
                {
                        if (components)
                        {
                                delete components;
                        }
                }
        private: System::Windows::Forms::Button^  button1;
        private: System::Windows::Forms::Button^  button2;
        private: System::Windows::Forms::Button^  button3;

        protected:

        private:
                /// <summary>
                /// 設計工具所需的變數。
                /// </summary>
                System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
                /// <summary>
                /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
                ///
                /// </summary>
                void InitializeComponent(void)
                {
                        this->button1 = (gcnew System::Windows::Forms::Button());
                        this->button2 = (gcnew System::Windows::Forms::Button());
                        this->button3 = (gcnew System::Windows::Forms::Button());
                        this->SuspendLayout();
                        //
                        // button1
                        //
                        this->button1->Location = System::Drawing::Point(12, 182);
                        this->button1->Name = L"button1";
                        this->button1->Size = System::Drawing::Size(84, 23);
                        this->button1->TabIndex = 0;
                        this->button1->Text = L"open_webcam";
                        this->button1->UseVisualStyleBackColor = true;
                        this->button1->Click += gcnew System::EventHandler(this,
&Form1::button1_Click);
                        //
                        // button2
                        //
                        this->button2->Location = System::Drawing::Point(188, 182);
                        this->button2->Name = L"button2";
                        this->button2->Size = System::Drawing::Size(84, 23);
                        this->button2->TabIndex = 1;
                        this->button2->Text = L"showpicture";
                        this->button2->UseVisualStyleBackColor = true;
                        this->button2->Click += gcnew System::EventHandler(this,
&Form1::button2_Click);
                        //
                        // button3
                        //
                        this->button3->Location = System::Drawing::Point(112, 143);
                        this->button3->Name = L"button3";
                        this->button3->Size = System::Drawing::Size(75, 23);
                        this->button3->TabIndex = 2;
                        this->button3->Text = L"show1";
                        this->button3->UseVisualStyleBackColor = true;
                        this->button3->Click += gcnew System::EventHandler(this,
&Form1::button3_Click);
                        //
                        // Form1
                        //
                        this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
                        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                        this->ClientSize = System::Drawing::Size(284, 262);
                        this->Controls->Add(this->button3);
                        this->Controls->Add(this->button2);
                        this->Controls->Add(this->button1);
                        this->Name = L"Form1";
                        this->Text = L"Form1";
                        this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
                        this->ResumeLayout(false);

                }
#pragma endregion
        private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^
e) {



                         }
        private: System::Void button1_Click(System::Object^  sender,
System::EventArgs^  e) {

                                 //************************************************//                            // Open Camera
//                               open_webcam =cvCaptureFromCAM(0);
cvNamedWindow("open_webcam",1);

                                while(1)
                                {
                                 webcam = cvQueryFrame(open_webcam);
                                 cvShowImage("open_webcam",webcam);

                                    if(cvWaitKey(10)>=0)
                                        {
                                        break;
                                        }
                                }

                                 cvReleaseCapture(&open_webcam);
                                 cvDestroyWindow("open_webcam");

                         }
        private: System::Void button2_Click(System::Object^  sender,
System::EventArgs^  e) {

                                 //************************************************//                            // Open Image
//

                                open_Image = cvLoadImage("lenna_256.bmp",1);
                                cvNamedWindow("showpicture", 0);

                                while(1)
                                {
                                cvShowImage("showpicture", open_Image);

                                        if(cvWaitKey(10)>=0)
                                        {
                                        break;
                                        }
                                }

                                 cvDestroyWindow("showpicture");

                         }

private: System::Void button3_Click(System::Object^  sender,
System::EventArgs^  e) {


    //1.讀取和顯示圖像

    /* the first command line parameter must be image file name */
    if ( argc == 2 && (src = cvLoadImage(argv[1], 0))!=0 )
    {
        ;
    }
    else
    {
        src = cvLoadImage("rice.jpg", 0);
    }
    cvNamedWindow( "src", CV_WINDOW_AUTOSIZE );
    cvShowImage( "src", src );
    //cvSmooth(src, src, CV_MEDIAN, 3, 0, 0, 0); //中值濾波,消除小的雜訊;

    //2.估計圖像背景

    tmp = cvCreateImage( cvGetSize(src), src->depth, src->nChannels);
    src_back = cvCreateImage( cvGetSize(src), src->depth, src->nChannels);
    //創建結構元素

    element = cvCreateStructuringElementEx( 4, 4, 1, 1, CV_SHAPE_ELLIPSE, 0);
    //用該結構對源圖象進行數學形態學的開操作後,估計背景亮度

    cvErode( src, tmp, element, 10);
    cvDilate( tmp, src_back, element, 10);
    cvNamedWindow( "src_back", CV_WINDOW_AUTOSIZE );
    cvShowImage( "src_back", src_back );

    //3.從源圖象中減區背景圖像

    dst_gray = cvCreateImage( cvGetSize(src), src->depth, src->nChannels);
    cvSub( src, src_back, dst_gray, 0);
    cvNamedWindow( "dst_gray", CV_WINDOW_AUTOSIZE );
    cvShowImage( "dst_gray", dst_gray );

    //4.使用閾值操作將圖像轉換為二值圖像

    dst_bw = cvCreateImage( cvGetSize(src), src->depth, src->nChannels);
    cvThreshold( dst_gray, dst_bw ,50, 255, CV_THRESH_BINARY ); //取閾值為50把
圖像轉為二值圖像

    //cvAdaptiveThreshold( dst_gray, dst_bw, 255, CV_ADAPTIVE_THRESH_MEAN_C,
CV_THRESH_BINARY, 3, 5 );

    cvNamedWindow( "dst_bw", CV_WINDOW_AUTOSIZE );
    cvShowImage( "dst_bw", dst_bw );

    //5.檢查圖像中的目標對象數量

    stor = cvCreateMemStorage(0);
    cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint),
stor);
    Number_Object = cvFindContours( dst_bw, stor, &cont, sizeof(CvContour),
    CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) ); //找到所有輪廓

    printf("Number_Object: %d\n", Number_Object);

    //6.計算圖像中對象的統計屬性

    dst_contours = cvCreateImage( cvGetSize(src), src->depth, src->nChannels);
    cvThreshold( dst_contours, dst_contours ,0, 255, CV_THRESH_BINARY); //在畫
輪廓前先把圖像變成白色

    for(;cont;cont = cont->h_next)
    {
        cvDrawContours( dst_contours, cont, CV_RGB(255, 0, 0), CV_RGB(255, 0,
0), 0, 1, 8, cvPoint(0, 0) ); //繪製當前輪廓

        contour_area_tmp = fabs(cvContourArea( cont, CV_WHOLE_SEQ )); //獲取當
前輪廓面積

        if( contour_area_tmp > contour_area_max )
        {
            contour_area_max = contour_area_tmp; //找到面積最大的輪廓

        }
        contour_area_sum += contour_area_tmp; //求所有輪廓的面積和

    }
    contour_area_ave = contour_area_sum/ Number_Object; //求出所有輪廓的平均值

    printf("contour_area_ave: %d\n", contour_area_ave );
    printf("contour_area_max: %d\n", contour_area_max );
    cvNamedWindow( "dst_contours", CV_WINDOW_AUTOSIZE );
    cvShowImage( "dst_contours", dst_contours );

    cvWaitKey(-1); //等待退出

    cvReleaseImage(&src);
    cvReleaseImage(&tmp);
    cvReleaseImage(&src_back);
    cvReleaseImage(&dst_gray);
    cvReleaseImage(&dst_bw);
    cvReleaseImage(&dst_contours);
    cvReleaseMemStorage(&stor);
    cvDestroyWindow( "src" );
    cvDestroyWindow( "src_back" );
    cvDestroyWindow( "dst_gray" );
    cvDestroyWindow( "dst_bw" );
    cvDestroyWindow( "dst_contours" );
    //void cvDestroyAllWindows(void);

    //return 0;

                 }
};
}


--
--
                        ㄧ○︿
                            \
                           √ ﹀.......GO!GO!GO!

--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 120.96.45.46
※ 編輯: SUPERERIC       來自: 120.96.45.46         (07/20 15:20)
※ 編輯: SUPERERIC       來自: 120.96.45.46         (07/20 15:21)

基本條件

.Net 原始碼 | ASP.NET News Reader Beta 0.2.9

請參閱

個人資料 | 發表新文章 | 回覆 | 回信 | 轉寄 | 同標題 | 搜尋 | 列印 預覽 直接

重要訊息通知

2007/06/21 由於微軟新聞伺服器移除多數新聞群組 (newsgroup),目前遭移除之群組暫時改為隱藏純瀏覽,若狀況已定案時,將會將隱藏中的群組重新調整。[討論]