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

标签 c++ opencv image-processing computer-vision

大家好,我尝试使用 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/

相关文章:

python - 手动实现OpenCV Sobel函数

iphone - iPhone 4 上的 cvFindExtrinsicCameraParams2() 错误

c++ - 异常处理构造函数

c++ - 覆盖单个文件的编译标志

c++ - 为什么将数组的地址分配给指针 "my_pointer = &my_array"是编译错误?

python - 如何在 CNN 中绘制 epoch 与 val_acc 和 epoch 与 val_loss 图?

c++ - 如何检测并在眼睛的虹膜区域周围画一个圆圈?

c++ - 对一个类中的一个 vector 进行排序,第二个 vector 应与第一个 vector 一起移动

image - 使用 DLib 提取感兴趣区域

c++ - 如何在 OpenCV 中找到 x 和 y 梯度?