pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

使用 OpenCV 和 SVM 處理圖像

using OpenCV and SVM with images(使用 OpenCV 和 SVM 處理圖像)
本文介紹了使用 OpenCV 和 SVM 處理圖像的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我在使用 SVM 讀取圖像、提取用于訓(xùn)練的特征以及在 OpenCV 中測(cè)試新圖像方面遇到困難.有人可以指點(diǎn)我一個(gè)很好的鏈接嗎?我看過,但由于矩陣不連續(xù),我遇到了問題.根據(jù)我的經(jīng)驗(yàn),我做過這樣的事情:

Mat img_mat = imread(imgname,0);//我用 0 表示灰度整數(shù) ii = 0;//training_mat 中的當(dāng)前列for (int i = 0; i<img_mat.rows; i++) {for (int j = 0; j 

對(duì)每個(gè)訓(xùn)練圖像執(zhí)行此操作(記住增加 file_num).在此之后,您應(yīng)該正確設(shè)置訓(xùn)練矩陣以傳遞給 SVM 函數(shù).其余的步驟應(yīng)該和網(wǎng)上的例子非常相似.

請(qǐng)注意,在執(zhí)行此操作時(shí),您還必須為每個(gè)訓(xùn)練圖像設(shè)置標(biāo)簽.因此,例如,如果您根據(jù)圖像對(duì)眼睛和非眼睛進(jìn)行分類,則需要指定訓(xùn)練矩陣中的哪一行對(duì)應(yīng)于眼睛和非眼睛.這被指定為一維矩陣,其中一維矩陣中的每個(gè)元素對(duì)應(yīng)于二維矩陣中的每一行.為每個(gè)類選擇值(例如,-1 代表非眼睛,1 代表眼睛)并將它們?cè)O(shè)置在標(biāo)簽矩陣中.

Mat 標(biāo)簽(num_files,1,CV_32FC1);

因此,如果此 labels 矩陣中的第三個(gè)元素為 -1,則表示訓(xùn)練矩陣中的第三行屬于非眼睛"類.您可以在評(píng)估每個(gè)圖像的循環(huán)中設(shè)置這些值.您可以做的一件事是將訓(xùn)練數(shù)據(jù)分類到每個(gè)類的單獨(dú)目錄中,并遍歷每個(gè)目錄中的圖像,并根據(jù)目錄設(shè)置標(biāo)簽.

接下來要做的是設(shè)置您的 SVM 參數(shù).這些值會(huì)因您的項(xiàng)目而異,但基本上您會(huì)聲明一個(gè) CvSVMParams 對(duì)象并設(shè)置這些值:

CvSVMParams 參數(shù);params.svm_type = CvSVM::C_SVC;params.kernel_type = CvSVM::POLY;params.gamma = 3;//...等等

網(wǎng)上有幾個(gè)關(guān)于如何設(shè)置這些參數(shù)的例子,就像你在問題中發(fā)布的鏈接一樣.

接下來,您創(chuàng)建一個(gè) CvSVM 對(duì)象并根據(jù)您的數(shù)據(jù)對(duì)其進(jìn)行訓(xùn)練!

CvSVM 支持向量機(jī);svm.train(training_mat, 標(biāo)簽, Mat(), Mat(), params);

這可能需要很長時(shí)間,具體取決于您擁有的數(shù)據(jù)量.但是,在完成訓(xùn)練后,您可以保存經(jīng)過訓(xùn)練的 SVM,這樣您就不必每次都重新訓(xùn)練它.

svm.save("svm_filename");//保存svm.load("svm_filename");//加載

要使用經(jīng)過訓(xùn)練的 SVM 測(cè)試您的圖像,只需讀取圖像,將其轉(zhuǎn)換為一維矩陣,然后將其傳遞給 svm.predict():

svm.predict(img_mat_1d);

它將根據(jù)您設(shè)置為標(biāo)簽的內(nèi)容返回一個(gè)值(例如,-1 或 1,基于我上面的眼睛/非眼睛示例).或者,如果您想一次測(cè)試多個(gè)圖像,您可以創(chuàng)建一個(gè)與之前定義的訓(xùn)練矩陣具有相同格式的矩陣,并將其作為參數(shù)傳遞.不過,返回值會(huì)有所不同.

祝你好運(yùn)!

I am having difficulty with reading an image, extracting features for training, and testing on new images in OpenCV using SVMs. can someone please point me to a great link? I have looked at the OpenCV Introduction to Support Vector Machines. But it doesn't help with reading in images, and I am not sure how to incorporate it.


My goals are to classify pixels in an image. These pixel would belong to a curves. I understand forming the training matrix (for instance, image A 1,1 1,2 1,3 1,4 1,5 2,1 2,2 2,3 2,4 2,5 3,1 3,2 3,3 3,4 3,5

I would form my training matrix as a [3][2]={ {1,1} {1,2} {1,3} {1,4} {1,5} {2,1} ..{} }

However, I am a little confuse about the labels. From my understanding, I have to specify which row (image) in the training matrix corresponds, which corresponds to a curve or non-curve. But, how can I label a training matrix row (image) if there are some pixels belonging to the curve and some not belonging to a curve. For example, my training matrix is [3][2]={ {1,1} {1,2} {1,3} {1,4} {1,5} {2,1} ..{} }, pixels {1,1} and {1,4} belong to the curve but the rest does not.

解決方案

I've had to deal with this recently, and here's what I ended up doing to get SVM to work for images.

To train your SVM on a set of images, first you have to construct the training matrix for the SVM. This matrix is specified as follows: each row of the matrix corresponds to one image, and each element in that row corresponds to one feature of the class -- in this case, the color of the pixel at a certain point. Since your images are 2D, you will need to convert them to a 1D matrix. The length of each row will be the area of the images (note that the images must be the same size).

Let's say you wanted to train the SVM on 5 different images, and each image was 4x3 pixels. First you would have to initialize the training matrix. The number of rows in the matrix would be 5, and the number of columns would be the area of the image, 4*3 = 12.

int num_files = 5;
int img_area = 4*3;
Mat training_mat(num_files,img_area,CV_32FC1);

Ideally, num_files and img_area wouldn't be hardcoded, but obtained from looping through a directory and counting the number of images and taking the actual area of an image.

The next step is to "fill in" the rows of training_mat with the data from each image. Below is an example of how this mapping would work for one row.

I've numbered each element of the image matrix with where it should go in the corresponding row in the training matrix. For example, if that were the third image, this would be the third row in the training matrix.

You would have to loop through each image and set the value in the output matrix accordingly. Here's an example for multiple images:

As for how you would do this in code, you could use reshape(), but I've had issues with that due to matrices not being continuous. In my experience I've done something like this:

Mat img_mat = imread(imgname,0); // I used 0 for greyscale
int ii = 0; // Current column in training_mat
for (int i = 0; i<img_mat.rows; i++) {
    for (int j = 0; j < img_mat.cols; j++) {
        training_mat.at<float>(file_num,ii++) = img_mat.at<uchar>(i,j);
    }
}

Do this for every training image (remembering to increment file_num). After this, you should have your training matrix set up properly to pass into the SVM functions. The rest of the steps should be very similar to examples online.

Note that while doing this, you also have to set up labels for each training image. So for example if you were classifying eyes and non-eyes based on images, you would need to specify which row in the training matrix corresponds to an eye and a non-eye. This is specified as a 1D matrix, where each element in the 1D matrix corresponds to each row in the 2D matrix. Pick values for each class (e.g., -1 for non-eye and 1 for eye) and set them in the labels matrix.

Mat labels(num_files,1,CV_32FC1);

So if the 3rd element in this labels matrix were -1, it means the 3rd row in the training matrix is in the "non-eye" class. You can set these values in the loop where you evaluate each image. One thing you could do is to sort the training data into separate directories for each class, and loop through the images in each directory, and set the labels based on the directory.

The next thing to do is set up your SVM parameters. These values will vary based on your project, but basically you would declare a CvSVMParams object and set the values:

CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::POLY;
params.gamma = 3;
// ...etc

There are several examples online on how to set these parameters, like in the link you posted in the question.

Next, you create a CvSVM object and train it based on your data!

CvSVM svm;
svm.train(training_mat, labels, Mat(), Mat(), params);

Depending on how much data you have, this could take a long time. After it's done training, however, you can save the trained SVM so you don't have to retrain it every time.

svm.save("svm_filename"); // saving
svm.load("svm_filename"); // loading

To test your images using the trained SVM, simply read an image, convert it to a 1D matrix, and pass that in to svm.predict():

svm.predict(img_mat_1d);

It will return a value based on what you set as your labels (e.g., -1 or 1, based on my eye/non-eye example above). Alternatively, if you want to test more than one image at a time, you can create a matrix that has the same format as the training matrix defined earlier and pass that in as the argument. The return value will be different, though.

Good luck!

這篇關(guān)于使用 OpenCV 和 SVM 處理圖像的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉(zhuǎn)圖像而不使用 OpenCV 函數(shù))
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設(shè)置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 旋振筛_不锈钢旋振筛_气旋筛_旋振筛厂家—新乡市大汉振动机械有限公司 | MOOG伺服阀维修,ATOS比例流量阀维修,伺服阀维修-上海纽顿液压设备有限公司 | 河南正规膏药生产厂家-膏药贴牌-膏药代加工-修康药业集团官网 | 合肥卓创建筑装饰,专业办公室装饰、商业空间装修与设计。 | 台湾阳明固态继电器-奥托尼克斯光电传感器-接近开关-温控器-光纤传感器-编码器一级代理商江苏用之宜电气 | 电缆接头_防水接头_电缆防水接头_防水电缆接头_上海闵彬 | 多米诺-多米诺世界纪录团队-多米诺世界-多米诺团队培训-多米诺公关活动-多米诺创意广告-多米诺大型表演-多米诺专业赛事 | China plate rolling machine manufacturer,cone rolling machine-Saint Fighter | 橡胶电子拉力机-塑料-微电脑电子拉力试验机厂家-江苏天源 | 皮带输送机-大倾角皮带输送机-皮带输送机厂家-河南坤威机械 | 农业四情_农业气象站_田间小型气象站_智慧农业气象站-山东风途物联网 | 压装机-卧式轴承轮轴数控伺服压装机厂家[铭泽机械] | 沉降天平_沉降粒度仪_液体比重仪-上海方瑞仪器有限公司 | 氢氧化钾厂家直销批发-济南金昊化工有限公司 | 钢绞线万能材料试验机-全自动恒应力两用机-混凝土恒应力压力试验机-北京科达京威科技发展有限公司 | ORP控制器_ORP电极价格-上优泰百科 | 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 | 合肥制氮机_合肥空压机厂家_安徽真空泵-凯圣精机 | 清水-铝合金-建筑模板厂家-木模板价格-铝模板生产「五棵松」品牌 | 气体检测仪-氢气检测仪-可燃气体传感器-恶臭电子鼻-深国安电子 | 石家庄律师_石家庄刑事辩护律师_石家庄取保候审-河北万垚律师事务所 | 非标压力容器_碳钢储罐_不锈钢_搪玻璃反应釜厂家-山东首丰智能环保装备有限公司 | 英思科GTD-3000EX(美国英思科气体检测仪MX4MX6)百科-北京嘉华众信科技有限公司 | 福州时代广告制作装饰有限公司-福州广告公司广告牌制作,福州展厅文化墙广告设计, | 发电机组|柴油发电机组-批发,上柴,玉柴,潍柴,康明斯柴油发电机厂家直销 | 动力配电箱-不锈钢配电箱-高压开关柜-重庆宇轩机电设备有限公司 聚天冬氨酸,亚氨基二琥珀酸四钠,PASP,IDS - 远联化工 | 贝壳粉涂料-内墙腻子-外墙腻子-山东巨野七彩贝壳漆业中心 | 领袖户外_深度旅游、摄影旅游、小团慢旅行、驴友网 | LINK FASHION 童装·青少年装展 河南卓美创业科技有限公司-河南卓美防雷公司-防雷接地-防雷工程-重庆避雷针-避雷器-防雷检测-避雷带-避雷针-避雷塔、机房防雷、古建筑防雷等-山西防雷公司 | MES系统工业智能终端_生产管理看板/安灯/ESOP/静电监控_讯鹏科技 | 高低温试验箱-模拟高低温试验箱订制-北京普桑达仪器科技有限公司【官网】 | 水上浮桥-游艇码头-浮动码头-游船码头-码瑞纳游艇码头工程 | 液压扳手-高品质液压扳手供应商 - 液压扳手, 液压扳手供应商, 德国进口液压拉马 | 临朐空调移机_空调维修「空调回收」临朐二手空调 | 糖衣机,除尘式糖衣机,全自动糖衣机,泰州市长江制药机械有限公司 体感VRAR全息沉浸式3D投影多媒体展厅展会游戏互动-万展互动 | 内六角扳手「厂家」-温州市威豪五金工具有限公司 | 汝成内控-行政事业单位内部控制管理服务商 | 水质传感器_水质监测站_雨量监测站_水文监测站-山东水境传感科技有限公司 | 承插管件_不锈钢承插管件_锻钢高压管件-温州科正阀门管件有限公司 | 衬塑管道_衬四氟管道厂家-淄博恒固化工设备有限公司 | 右手官网|右手工业设计|外观设计公司|工业设计公司|产品创新设计|医疗产品结构设计|EMC产品结构设计 |