基于颜色分量HSV的图像分割:基于opencv的分割调试工具源码

版权声明:本文为博主原创文章,未经博主允许不得转载。欢迎交流,QQ:896922782,微信:15058133936 https://blog.csdn.net/zhubenfulovepoem/article/details/78596552

这里写图片描述

原图为:
这里写图片描述

#include "stdafx.h"

#include <iostream>
#include <fstream>
#include <sstream>
#include <exception>
#include <stdio.h>
#include <vector>
#include <io.h>
#include <windows.h>

#include <opencv/highgui.h> 
#include <opencv/cv.h> 

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"

using namespace cv;
using namespace std;

#define showSteps 1

int lowerH=0;
int lowerS=0;
int lowerV=0;

int upperH=180;
int upperS=256;
int upperV=256;

IplImage *frame = 0;
IplImage* imgThresh = 0;

//This function threshold the HSV image and create a binary image
IplImage* GetThresholdedImage(IplImage* imgHSV){

    IplImage* imgThresh=cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
    cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV), imgThresh);  

    return imgThresh;

}

//This function create two windows and 6 trackbars for the "Ball" window
void setwindowSettings(){
    cvNamedWindow("Video",0);
    cvNamedWindow("Ball",0);

    cvCreateTrackbar("LowerH", "Ball", &lowerH, 180, NULL);
    cvCreateTrackbar("UpperH", "Ball", &upperH, 180, NULL);

    cvCreateTrackbar("LowerS", "Ball", &lowerS, 256, NULL);
    cvCreateTrackbar("UpperS", "Ball", &upperS, 256, NULL);

    cvCreateTrackbar("LowerV", "Ball", &lowerV, 256, NULL);
    cvCreateTrackbar("UpperV", "Ball", &upperV, 256, NULL); 
}



//config.txt为要处理的跟目录,在训练model中,config.txt中的内容为训练是的样本库,1-13分类好了。--
//在样本筛选model中,config.txt里的内容为要筛选的样本库,未分类。---
char * configFile = "config.txt";

//读取config文件里的内容--
char* trainSetPosPath = (char *)malloc(200*sizeof(char));
void readConfig(char* configFile, char* trainSetPosPath){
    fstream f;
    char cstring[1000];
    int readS=0;
    f.open(configFile, fstream::in);
    char param1[200]; strcpy(param1,"");
    char param2[200]; strcpy(param2,"");
    char param3[200]; strcpy(param3,"");

    //--读取第一行:--
    f.getline(cstring, sizeof(cstring));
    readS=sscanf (cstring, "%s %s %s", param1,param2, param3);
    strcpy(trainSetPosPath,param3);
}

//遍历config.txt里的根目录下的所有的文件,包括子目录。--
// 其中子目录的名字就是label,子目录里的文件为label对于的训练测试样本---
vector<string> imgNames;
int labelTemp = 0;

void dfsFolder(string folderPath){
    _finddata_t FileInfo;
    string strfind = folderPath + "\\*";
    long Handle = _findfirst(strfind.c_str(), &FileInfo);
    if (Handle == -1L)
    {
        cerr << "can not match the folder path" << endl;
        exit(-1);
    }
    do{
        //判断是否有子目录--
        if (FileInfo.attrib & _A_SUBDIR)        {
            //  cout<<FileInfo.name<<" "<<FileInfo.attrib<<endl;
            //这个语句很重要--
            if( (strcmp(FileInfo.name,".") != 0 ) &&(strcmp(FileInfo.name,"..") != 0))          {
                string newPath = folderPath + "\\" + FileInfo.name;
                cout<<FileInfo.name<<" "<<newPath<<endl;
                //根目录下下的子目录名字就是label名,如果没有子目录则其为根目录下
                labelTemp = atoi(FileInfo.name);
                //  printf("%d\n",labelTemp);
                dfsFolder(newPath);
            }
        }else  {
            string finalName = folderPath + "\\" + FileInfo.name;
            //将所有的文件名写入一个txt文件--
            //  cout << FileInfo.name << "\t";
            //  printf("%d\t",label);
            //  cout << folderPath << "\\" << FileInfo.name  << " " <<endl;
            //将文件名字和label名字(子目录名字赋值给向量)--

            imgNames.push_back(finalName);

        }
    }while (_findnext(Handle, &FileInfo) == 0);
    _findclose(Handle);

}

void initTrainImage(){
    readConfig(configFile, trainSetPosPath);

    string folderPath = trainSetPosPath;
    dfsFolder(folderPath);

}

//对样本进行一些变换,增加样本数量--
int preProcessing1(){
    initTrainImage();

    setwindowSettings();
    int imgNum = imgNames.size();
    for(int i=0;i<imgNum;i++){
        cout<<i<<endl;
        while(1){
        frame = cvLoadImage(imgNames[i].c_str(),1);
        if(!frame) continue;

        IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);    
        cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV

        imgThresh = GetThresholdedImage(imgHSV);

        cvShowImage("Ball", imgThresh);
        cvShowImage("Video", frame);

        cvReleaseImage(&imgHSV);
        cvReleaseImage(&imgThresh);
        cvReleaseImage(&frame);

        //Wait 80mS
        int c = cvWaitKey(10);
    //  cout<<"["<<lowerH<<","<<upperH<<"]"<<"["<<lowerS<<","<<upperS<<"]"<<"["<<lowerV<<","<<upperV<<"]"<<endl;
        //If 'ESC' is pressed, break the loop
        if((char)c==27 )    break;
        }

    }

    cvDestroyAllWindows();
    return 0;
}

void main(){
    preProcessing1();

}

int main1()  {
    CvCapture* capture =0;  

    capture = cvCaptureFromCAM(0);
    if(!capture){
        printf("Capture failure\n");
        return -1;
    }

    IplImage* frame=0;

    setwindowSettings();

    //iterate through each frames of the video
    while(true){

        frame = cvQueryFrame(capture);
        if(!frame)  break;
        frame=cvCloneImage(frame);  

        IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);    
        cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV

        IplImage* imgThresh = GetThresholdedImage(imgHSV);

        cvShowImage("Ball", imgThresh);
        cvShowImage("Video", frame);

        //Clean up used images
        cvReleaseImage(&imgHSV);
        cvReleaseImage(&imgThresh);
        cvReleaseImage(&frame);

        //Wait 80mS
        int c = cvWaitKey(80);
        //If 'ESC' is pressed, break the loop
        if((char)c==27 )    break;

    }

    cvDestroyAllWindows();
    cvReleaseCapture(&capture);

    return 0;
}






猜你喜欢

转载自blog.csdn.net/zhubenfulovepoem/article/details/78596552