gpt4 book ai didi

c++ - 在输出窗口中显示三个图像而不是一个

转载 作者:行者123 更新时间:2023-11-28 06:03:35 28 4
gpt4 key购买 nike

大家好,我尝试使用 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 个 channel 更改为 1 个 channel 。

另外,如果你改变内存顺序,它可能会更快(在两个地方都改为 (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 );
}

关于c++ - 在输出窗口中显示三个图像而不是一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32820499/

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