gpt4 book ai didi

c++ - 视频所有帧的直方图的K均值聚类

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:23 25 4
gpt4 key购买 nike

我正在尝试对视频的所有帧进行聚类我已经计算了 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 <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;
}

最佳答案

OpenCV 中的kmeans 函数不接受cv::Mat-s 的 vector 。根据文档:

samples - Floating-point matrix of input samples, one row per sample

您必须将数据转换成这种格式,例如:

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);

然后调用kmeans聚类:

cv::kmeans(samples, ... );

关于c++ - 视频所有帧的直方图的K均值聚类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19693554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com