基于颜色分量HSV的图像分割:基于opencv的分割调试工具源码
来源:互联网 发布:java 游戏引擎 编辑:程序博客网 时间:2024/05/17 06:14
原图为:
#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 1int 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 imageIplImage* 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" windowvoid 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;}
阅读全文
0 0
- 基于颜色分量HSV的图像分割:基于opencv的分割调试工具源码
- 【OpenCV】基于HSV的肤色分割
- 基于颜色信息的图像分割算法
- 图像分割—基于图的图像分割(OpenCV源码注解)
- 基于OpenCV 图像分割
- 基于图像分割的方法
- 基于纹理的分割图像
- 基于DL的图像分割
- 基于图的图像分割
- 基于opencv的阈值分割
- 爬山法处理基于颜色的图像分割
- OpenCV 基于超像素分割的图像区域选取方法及源码
- 基于图像分块的图像分割
- 图像分割—基于图的图像分割
- 基于Ohta颜色空间的火焰分割
- 基于颜色分割的盲道识别算法
- 基于水平集的图像分割方法
- 基于阈值法的图像分割技术
- NOIP提高组2017小结
- spring基础
- HDU 4734 F[x] (数位dp)
- 【JavaScript学习】DOM操作技术
- 数据结构实验之图论三:判断可达性(DFS算法)
- 基于颜色分量HSV的图像分割:基于opencv的分割调试工具源码
- CodeForces
- java 线程的五种状态
- C++ bind
- C语言学习心得与体会1
- 阿里云 linux搭建git服务器
- bos项目day04快递员条件查询:ArrayList的toArray(T[] a)方法
- 用Java实现斐波拉契函数
- Mysql中转化固定日期类似Oracle中的to_char方法