输出窗口显示三个图像,而不是一个
Three images are displayed in output window instead of one
大家好,我尝试使用kmeans聚类对对象进行分组。这样我就可以使用这种聚类方法来检测对象。我得到输出,但问题是它太慢了{我怎么能解决这个??}得到的输出窗口如下面的链接所示。三个输出图像显示,而不是一个,我如何解决这个问题。我不知道错误在哪里。
http://tinypic.com/view.php?pic=30bd7dc& s = 8 # .VgkSIPmqqko
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( )
{
Mat src = imread( "Light.jpg", 0 );
// imshow("fff",src);
// cvtColor(src,src,COLOR_BGR2GRAY);
Mat dst;
// pyrDown(src,src,Size( src.cols/2, src.rows/2 ),4);
// src=dst;
resize(src,src,Size(128,128),0,0,1);
Mat samples(src.rows * src.cols, 3, CV_32F);
for( int y = 0; y < src.rows; y++ )
for( int x = 0; x < src.cols; x++ )
// for( int z = 0; z < 3; z++)
samples.at<float>(y + x*src.rows) = src.at<uchar>(y,x);
cout<<"aaa"<<endl;
int clusterCount = 15;
Mat labels;
int attempts = 2;
Mat centers;
cout<<"aaa"<<endl;
kmeans(samples, clusterCount, labels, TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 10000, 0.0001), attempts, KMEANS_PP_CENTERS, centers );
Mat new_image( src.size(), src.type() );
cout<<"aaa"<<endl;
for( int y = 0; y < src.rows; y++ )
for( int x = 0; x < src.cols; x++ )
{
int cluster_idx = labels.at<int>(y + x*src.rows,0);
new_image.at<uchar>(y,x) = centers.at<float>(cluster_idx,0);
//new_image.at<Vec3b>(y,x)[1] = centers.at<float>(cluster_idx, 1);
// new_image.at<Vec3b>(y,x)[2] = centers.at<float>(cluster_idx, 2);
}
imshow( "clustered image", new_image );
waitKey( 0 );
}
在初始代码中,如果使用灰度图像,则必须将中间介质Mat sample
从3通道更改为1通道。
此外,如果您更改内存顺序,它可能会更快(在两个地方都更改为(y*src.cols + x, 0)
):
int main( )
{
clock_t start = clock();
Mat src = imread( "Light.jpg", 0 );
Mat dst;
resize(src,src,Size(128,128),0,0,1);
Mat samples(src.rows * src.cols, 1, CV_32F);
for( int y = 0; y < src.rows; y++ )
for( int x = 0; x < src.cols; x++ )
samples.at<float>(y*src.cols + x, 0) = src.at<uchar>(y,x);
int clusterCount = 15;
Mat labels;
int attempts = 2;
Mat centers;
kmeans(samples, clusterCount, labels, TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 10000, 0.0001), attempts, KMEANS_PP_CENTERS, centers );
Mat new_image( src.size(), src.type() );
for( int y = 0; y < src.rows; y++ )
for( int x = 0; x < src.cols; x++ )
{
int cluster_idx = labels.at<int>(y*src.cols + x,0);
new_image.at<uchar>(y,x) = centers.at<float>(cluster_idx,0);
}
imshow( "clustered image", new_image );
clock_t end = clock();
std::cout << "time: " << (end - start)/(float)CLOCKS_PER_SEC << std::endl;
waitKey( 0 );
}
相关文章:
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- 如何检查第三个 API 是否在 Linux 中为 c/c++ 程序创建了一个新线程?
- 如何通过通用引用或std::forward将这三个c++模板函数合并为一个
- 从一个文件读取到三个并行阵列
- 访问三个不同的变量在C++中形成三个不同函数中的一个文件
- 将三个 QGraphicsRectItems 连接到一个对象中
- 设计具有变体字段的文本类型类,其中可以存储一个或三个对象
- 创建一个程序,要求用户输入两个数字,并有三个可能的答案.代码未编译.数字(作为int)
- (C++)如何创建一个函数来接收两个排序的链表并返回出现在两个列表中的第三个元素列表
- 如何从另一个函数中访问一个功能,从C 中的第三个访问
- 将三个文本文件组合成一个文本文件,形成一个句子C++
- Boost Spirit Parser用三个字符串的矢量编译成一个结构,自适应不工作
- 编译三个文件以创建一个用于平均值的程序(取消注释?)
- 在OpenGL中为顶点、uvs和法线使用一个缓冲区是否比使用三个缓冲区性能更好
- 三个整数组成一个带空格的字符串;
- 如何按第一个,然后是第二个,然后是第三个对 2d 数组进行排序,..使用 C++ 的列
- 将动态数组的三个元素复制到另一个元素中
- 调用接受三个参数的函数出现问题,其中一个是 int const
- C++ 如何从一个整数值中获取三个单独的日期值(日、月、年)
- 找到两个阵列中最大的一个并存储 iN 第三个阵列