search
HomeDatabaseMysql Tutorial【OpenCV2.4】SVM处理线性不可分的例子

【原文:http://www.cnblogs.com/justany/archive/2012/11/26/2788509.html】 目的 实际事物模型中,并非所有东西都是线性可分的。 需要寻找一种方法对线性不可分数据进行划分。 原理 ,我们推导出对于线性可分数据,最佳划分超平面应满足: 现在我们想引入

【原文:http://www.cnblogs.com/justany/archive/2012/11/26/2788509.html】

目的

  • 实际事物模型中,并非所有东西都是线性可分的。
  • 需要寻找一种方法对线性不可分数据进行划分。

原理

,我们推导出对于线性可分数据,最佳划分超平面应满足:

    【OpenCV2.4】SVM处理线性不可分的例子

现在我们想引入一些东西,来表示那些被错分的数据点(比如噪点),对划分的影响。

如何来表示这些影响呢?

被错分的点,离自己应当存在的区域越远,就代表了,这个点“错”得越严重。

所以我们引入【OpenCV2.4】SVM处理线性不可分的例子,为对应样本离同类区域的距离。

【OpenCV2.4】SVM处理线性不可分的例子

接下来的问题是,如何将这种错的程度,转换为和原模型相同的度量呢?

我们再引入一个常量C,表示【OpenCV2.4】SVM处理线性不可分的例子和原模型度量的转换关系,用C对【OpenCV2.4】SVM处理线性不可分的例子进行加权和,来表征错分点对原模型的影响,这样我们得到新的最优化问题模型:

    【OpenCV2.4】SVM处理线性不可分的例子

关于参数C的选择, 明显的取决于训练样本的分布情况。 尽管并不存在一个普遍的答案,但是记住下面几点规则还是有用的:

  • C比较大时分类错误率较小,但是间隔也较小。 在这种情形下, 错分类对模型函数产生较大的影响,既然优化的目的是为了最小化这个模型函数,那么错分类的情形必然会受到抑制。
  • C比较小时间隔较大,但是分类错误率也较大。 在这种情形下,模型函数中错分类之和这一项对优化过程的影响变小,优化过程将更加关注于寻找到一个能产生较大间隔的超平面。

 说白了,C的大小表征了,错分数据对原模型的影响程度。于是C越大,优化时越关注错分问题。反之越关注能否产生一个较大间隔的超平面。

开始使用

【OpenCV2.4】SVM处理线性不可分的例子

#include <iostream><span>
#include </span><opencv2><span>
#include </span><opencv2><span>
#include </span><opencv2>

<span>#define</span> NTRAINING_SAMPLES   100         <span>//</span><span> 每类训练样本的数量</span>
<span>#define</span> FRAC_LINEAR_SEP     0.9f        <span>//</span><span> 线性可分部分的样本组成比例</span>

<span>using</span> <span>namespace</span><span> cv;
</span><span>using</span> <span>namespace</span><span> std;

</span><span>int</span><span> main(){
    </span><span>//</span><span> 用于显示的数据</span>
    <span>const</span> <span>int</span> WIDTH = <span>512</span>, HEIGHT = <span>512</span><span>;
    Mat I </span>=<span> Mat::zeros(HEIGHT, WIDTH, CV_8UC3);

    </span><span>/*</span><span> 1. 随即产生训练数据 </span><span>*/</span><span>
    Mat trainData(</span><span>2</span>*NTRAINING_SAMPLES, <span>2</span><span>, CV_32FC1);
    Mat labels   (</span><span>2</span>*NTRAINING_SAMPLES, <span>1</span><span>, CV_32FC1);
    
    RNG rng(</span><span>100</span>); <span>//</span><span> 生成随即数

    </span><span>//</span><span> 设置线性可分的训练数据</span>
    <span>int</span> nLinearSamples = (<span>int</span>) (FRAC_LINEAR_SEP *<span> NTRAINING_SAMPLES);

    </span><span>//</span><span> 生成分类1的随机点</span>
    Mat trainClass = trainData.rowRange(<span>0</span><span>, nLinearSamples);
    </span><span>//</span><span> 点的x坐标在[0, 0.4)之间</span>
    Mat c = trainClass.colRange(<span>0</span>, <span>1</span><span>);
    rng.fill(c, RNG::UNIFORM, Scalar(</span><span>1</span>), Scalar(<span>0.4</span> *<span> WIDTH));
    </span><span>//</span><span> 点的y坐标在[0, 1)之间</span>
    c = trainClass.colRange(<span>1</span>,<span>2</span><span>);
    rng.fill(c, RNG::UNIFORM, Scalar(</span><span>1</span><span>), Scalar(HEIGHT));

    </span><span>//</span><span> 生成分类2的随机点</span>
    trainClass = trainData.rowRange(<span>2</span>*NTRAINING_SAMPLES-nLinearSamples, <span>2</span>*<span>NTRAINING_SAMPLES);
    </span><span>//</span><span> 点的x坐标在[0.6, 1]之间</span>
    c = trainClass.colRange(<span>0</span> , <span>1</span><span>); 
    rng.fill(c, RNG::UNIFORM, Scalar(</span><span>0.6</span>*<span>WIDTH), Scalar(WIDTH));
    </span><span>//</span><span> 点的y坐标在[0, 1)之间</span>
    c = trainClass.colRange(<span>1</span>,<span>2</span><span>);
    rng.fill(c, RNG::UNIFORM, Scalar(</span><span>1</span><span>), Scalar(HEIGHT));

    </span><span>/*</span><span> 设置非线性可分的训练数据 </span><span>*/</span>

    <span>//</span><span> 生成分类1和分类2的随机点</span>
    trainClass = trainData.rowRange(  nLinearSamples, <span>2</span>*NTRAINING_SAMPLES-<span>nLinearSamples);
    </span><span>//</span><span> 点的x坐标在[0.4, 0.6)之间</span>
    c = trainClass.colRange(<span>0</span>,<span>1</span><span>);
    rng.fill(c, RNG::UNIFORM, Scalar(</span><span>0.4</span>*WIDTH), Scalar(<span>0.6</span>*<span>WIDTH)); 
    </span><span>//</span><span> 点的y坐标在[0, 1)之间</span>
    c = trainClass.colRange(<span>1</span>,<span>2</span><span>);
    rng.fill(c, RNG::UNIFORM, Scalar(</span><span>1</span><span>), Scalar(HEIGHT));
    
    </span><span>/*</span><span>*/</span><span>
    labels.rowRange(                </span><span>0</span>,   NTRAINING_SAMPLES).setTo(<span>1</span>);  <span>//</span><span> Class 1</span>
    labels.rowRange(NTRAINING_SAMPLES, <span>2</span>*NTRAINING_SAMPLES).setTo(<span>2</span>);  <span>//</span><span> Class 2</span>

    <span>/*</span><span> 设置支持向量机参数 </span><span>*/</span><span>
    CvSVMParams </span><span>params</span><span>;
    </span><span>params</span>.svm_type    =<span> SVM::C_SVC;
    </span><span>params</span>.C           = <span>0.1</span><span>;
    </span><span>params</span>.kernel_type =<span> SVM::LINEAR;
    </span><span>params</span>.term_crit   = TermCriteria(CV_TERMCRIT_ITER, (<span>int</span>)1e7, 1e-<span>6</span><span>);

    </span><span>/*</span><span> 3. 训练支持向量机 </span><span>*/</span><span>
    cout </span>"<span>Starting training process</span><span>"</span>  endl;
    CvSVM svm;
    svm.train(trainData, labels, Mat(), Mat(), <span>params</span><span>);
    cout </span>"<span>Finished training process</span><span>"</span>  endl;
    
    <span>/*</span><span> 4. 显示划分区域 </span><span>*/</span><span>
    Vec3b green(</span><span>0</span>,<span>100</span>,<span>0</span>), blue (<span>100</span>,<span>0</span>,<span>0</span><span>);
    </span><span>for</span> (<span>int</span> i = <span>0</span>; i i)
        <span>for</span> (<span>int</span> j = <span>0</span>; j j){
            Mat sampleMat = (Mat_float>(<span>1</span>,<span>2</span>)  i, j);
            <span>float</span> response =<span> svm.predict(sampleMat);

            </span><span>if</span>      (response == <span>1</span>)    I.at<vec3b>(j, i)  =<span> green;
            </span><span>else</span> <span>if</span> (response == <span>2</span>)    I.at<vec3b>(j, i)  =<span> blue;
        }

    </span><span>/*</span><span> 5. 显示训练数据 </span><span>*/</span>
    <span>int</span> thick = -<span>1</span><span>;
    </span><span>int</span> lineType = <span>8</span><span>;
    </span><span>float</span><span> px, py;
    </span><span>//</span><span> 分类1</span>
    <span>for</span> (<span>int</span> i = <span>0</span>; i i){
        px = trainData.atfloat>(i,<span>0</span><span>);
        py </span>= trainData.atfloat>(i,<span>1</span><span>);
        circle(I, Point( (</span><span>int</span>) px,  (<span>int</span>) py ), <span>3</span>, Scalar(<span>0</span>, <span>255</span>, <span>0</span><span>), thick, lineType);
    }
    </span><span>//</span><span> 分类2</span>
    <span>for</span> (<span>int</span> i = NTRAINING_SAMPLES; i 2*NTRAINING_SAMPLES; ++<span>i){
        px </span>= trainData.atfloat>(i,<span>0</span><span>);
        py </span>= trainData.atfloat>(i,<span>1</span><span>);
        circle(I, Point( (</span><span>int</span>) px, (<span>int</span>) py ), <span>3</span>, Scalar(<span>255</span>, <span>0</span>, <span>0</span><span>), thick, lineType);
    }

    </span><span>/*</span><span> 6. 显示支持向量 */</span>
    thick = <span>2</span><span>;
    lineType  </span>= <span>8</span><span>;
    </span><span>int</span> x     =<span> svm.get_support_vector_count();

    </span><span>for</span> (<span>int</span> i = <span>0</span>; i i)
    {
        <span>const</span> <span>float</span>* v =<span> svm.get_support_vector(i);
        circle( I,  Point( (</span><span>int</span>) v[<span>0</span>], (<span>int</span>) v[<span>1</span>]), <span>6</span>, Scalar(<span>128</span>, <span>128</span>, <span>128</span><span>), thick, lineType);
    }

    imwrite(</span><span>"</span><span>result.png</span><span>"</span>, I);                      <span>//</span><span> 保存图片</span>
    imshow(<span>"</span><span>SVM线性不可分数据划分</span><span>"</span>, I); <span>//</span><span> 显示给用户</span>
    waitKey(<span>0</span><span>);
}</span></vec3b></vec3b></opencv2></opencv2></opencv2></iostream>

【OpenCV2.4】SVM处理线性不可分的例子

设置SVM参数

这里的参数设置可以参考一下的API。

<span>CvSVMParams</span> <span>params</span><span>;</span>
<span>params</span><span>.</span><span>svm_type</span>    <span>=</span> <span>SVM</span><span>::</span><span>C_SVC</span><span>;</span>
<span>params</span><span>.</span><span>C</span>              <span>=</span> <span>0.1</span><span>;</span>
<span>params</span><span>.</span><span>kernel_type</span> <span>=</span> <span>SVM</span><span>::</span><span>LINEAR</span><span>;</span>
<span>params</span><span>.</span><span>term_crit</span>   <span>=</span> <span>TermCriteria</span><span>(</span><span>CV_TERMCRIT_ITER</span><span>,</span> <span>(</span><span>int</span><span>)</span><span>1e7</span><span>,</span> <span>1e-6</span><span>);</span>

 可以看到,这次使用的是C类支持向量分类机。其参数C的值为0.1。

 结果

  • 程序创建了一张图像,在其中显示了训练样本,其中一个类显示为浅绿色圆圈,另一个类显示为浅蓝色圆圈。
  • 训练得到SVM,并将图像的每一个像素分类。 分类的结果将图像分为蓝绿两部分,中间线就是最优分割超平面。由于样本非线性可分, 自然就有一些被错分类的样本。 一些绿色点被划分到蓝色区域, 一些蓝色点被划分到绿色区域。
  • 最后支持向量通过灰色边框加重显示。

【OpenCV2.4】SVM处理线性不可分的例子

被山寨的原文

Support Vector Machines for Non-Linearly Separable Data . OpenCV.org

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools