久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 OpenCV 和 SVM 處理圖像

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

問(wèn)題描述

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

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)行分類(lèi),則需要指定訓(xùn)練矩陣中的哪一行對(duì)應(yīng)于眼睛和非眼睛.這被指定為一維矩陣,其中一維矩陣中的每個(gè)元素對(duì)應(yīng)于二維矩陣中的每一行.為每個(gè)類(lèi)選擇值(例如,-1 代表非眼睛,1 代表眼睛)并將它們?cè)O(shè)置在標(biāo)簽矩陣中.

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

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

接下來(lái)要做的是設(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ù)的例子,就像你在問(wèn)題中發(fā)布的鏈接一樣.

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

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

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

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

要使用經(jīng)過(guò)訓(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ù)傳遞.不過(guò),返回值會(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)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(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 中打開(kāi) avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無(wú)法設(shè)置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 日韩a | 欧美日韩国产中文 | 色播久久 | 伊人精品 | 日韩在线精品 | 成人免费视频播放 | 视频一区二区在线 | 黄色免费在线观看网站 | 国产精品一区二区三区四区五区 | 免费亚洲视频 | 成人免费在线视频 | 日韩2020狼一二三 | 日本不卡一区二区三区 | 一级黄色绿像片 | 久久毛片| 国产精品成人在线 | 国产激情视频在线 | 国产亚洲精品久久久久动 | 欧美日韩一卡 | 国产超碰人人爽人人做人人爱 | 久久久久成人精品 | 国产精品国产馆在线真实露脸 | 国产精品免费一区二区三区四区 | 亚洲综合首页 | 国产精品久久久久久久三级 | 欧美成视频在线观看 | 亚洲午夜精品 | 九一国产精品 | 欧美视频一区二区三区 | 九九免费视频 | 精品少妇v888av | 亚洲毛片在线观看 | 日韩手机在线看片 | 日本黄色大片免费看 | 台湾a级理论片在线观看 | 国产乱xxav | 色播久久| 欧美成人综合 | 一级毛片免费视频观看 | 超碰婷婷| 精品一区二区三区在线视频 |