C++ 视频所有帧直方图的K均值聚类
我试图对视频的所有帧进行聚类,我计算了hsv直方图,但没有找到kmean聚类。我的代码在k mean命令下崩溃。谁能告诉我我做错了什么C++ 视频所有帧直方图的K均值聚类,c++,visual-studio-2010,opencv,C++,Visual Studio 2010,Opencv,我试图对视频的所有帧进行聚类,我计算了hsv直方图,但没有找到kmean聚类。我的代码在k mean命令下崩溃。谁能告诉我我做错了什么 #include "stdafx.h" #include "highgui.h" #include <stdio.h> #include <cv.h> #include <highgui.h> #include <stdio.h> #include <conio.h> #include <ope
#include "stdafx.h"
#include "highgui.h"
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <conio.h>
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <conio.h>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cv::Mat centers;
Mat src_base, hsv_base;
cv::Mat labels;
vector<Mat> histograms;
string filename = "video.avi";
VideoCapture capture(filename);
if( !capture.isOpened() )
exit(0);
for( ; ; )
{
capture >> src_base;
if(src_base.empty())
break;
/// Convert to HSV
cvtColor( src_base, hsv_base, CV_BGR2HSV );
/// Using 16 bins for hue and 32 for saturation
int h_bins = 16; int s_bins = 8;
int histSize[] = { h_bins, s_bins };
// hue varies from 0 to 256, saturation from 0 to 180
float h_ranges[] = { 0, 256 };
float s_ranges[] = { 0, 180 };
const float* ranges[] = { h_ranges, s_ranges };
// Use the o-th and 1-st channels
int channels[] = { 0, 1 };
/// Histograms
Mat hist_base;
/// Calculate the histograms for the HSV images
calcHist( &hsv_base, 1, channels, Mat(), hist_base, 2, histSize, ranges, true, false );
histograms.push_back(hist_base);
}
cv::kmeans(histograms, 8, labels,cv::TermCriteria(CV_TERMCRIT_ITER, 10, 1.0),3, cv::KMEANS_PP_CENTERS, centers);
cout<<"code executed"<<endl;
return 0;
}
#包括“stdafx.h”
#包括“highgui.h”
#包括
#包括
#包括
#包括
#包括
#包括//高斯模糊
#包括//基本OpenCV结构(cv::Mat、Scalar)
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
int _tmain(int argc,_TCHAR*argv[]
{
cv::垫中心;
垫式src_底座、hsv_底座;
cv::垫标签;
向量直方图;
字符串filename=“video.avi”;
视频捕获(文件名);
如果(!capture.isOpened())
出口(0);
对于(;;)
{
捕获>>src_基地;
if(src_base.empty())
打破
///转乘高速列车
CVT颜色(src_底座、hsv_底座、CV_BGR2HSV);
///使用16个色相箱和32个饱和度箱
整数h_bins=16;整数s_bins=8;
int histSize[]={h_-bins,s_-bins};
//色调从0到256,饱和度从0到180
浮点h_范围[]={0,256};
浮点s_范围[]={0,180};
常量浮点*范围[]={h_范围,s_范围};
//使用第0个和第1个通道
int通道[]={0,1};
///直方图
Mat hist_基地;
///计算HSV图像的直方图
calcHist(&hsv_base,1,通道,Mat(),hist_base,2,histSize,范围,真,假);
直方图。推回(历史基);
}
cv::kmeans(直方图,8,标签,cv::TERMCRIT_ITER,10,1.0),3,cv::kmeans_PP_中心,中心);
coutOpenCV中的kmeans
函数不接受cv::Mat
-s的向量。根据文档:
样本-输入样本的浮点矩阵,每个样本一行
您必须将数据转换为这种格式,例如:
int h_bins = 16; int s_bins = 8;
Mat samples( histograms.size() , h_bins * s_bins , CV_32F);
for( int k = 0; k < histograms.size() ; k++ )
for( int y = 0; y < h_bins; y++)
for( int x = 0; x < s_bins ; x++ )
samples.at<float>( k , y* s_bins + x) = histograms[k].at<float>(y,x);
OpenCV中的kmeans
函数不接受cv::Mat
-s的向量。根据文档:
样本-输入样本的浮点矩阵,每个样本一行
您必须将数据转换为这种格式,例如:
int h_bins = 16; int s_bins = 8;
Mat samples( histograms.size() , h_bins * s_bins , CV_32F);
for( int k = 0; k < histograms.size() ; k++ )
for( int y = 0; y < h_bins; y++)
for( int x = 0; x < s_bins ; x++ )
samples.at<float>( k , y* s_bins + x) = histograms[k].at<float>(y,x);
我面临着一个类似的问题,请分享你是否找到了解决方案。我面临着一个类似的问题,请分享你是否找到了解决方案。