C++ 如何使用opencv将图像转换为矩阵?

C++ 如何使用opencv将图像转换为矩阵?,c++,visual-c++,opencv,C++,Visual C++,Opencv,我试图在OpenCV中制作一个程序,将图像转换为矩阵形式,每个值代表图像的像素。我已将图像转换为二进制格式,现在我想将其像素值转换为矩阵。您的代码使用OpenCV版本1。我会让别人回答,因为这不是我的专长。在我看来,基于2.0模板的界面更加直观,我建议在所有新的工作中使用它 看看我在这个程序中使用imread()的方式。。。 请检查从imread()返回的值的类型。。。 另外,在代码中搜索originalColor=imageArg(/*行*/chosenX,/*列*/chosenY)这是一种索

我试图在OpenCV中制作一个程序,将图像转换为矩阵形式,每个值代表图像的像素。我已将图像转换为二进制格式,现在我想将其像素值转换为矩阵。

您的代码使用OpenCV版本1。我会让别人回答,因为这不是我的专长。在我看来,基于2.0模板的界面更加直观,我建议在所有新的工作中使用它

看看我在这个程序中使用imread()的方式。。。 请检查从imread()返回的值的类型。。。 另外,在代码中搜索
originalColor=imageArg(/*行*/chosenX,/*列*/chosenY)这是一种索引到imread返回的矩阵的方法

// HW1 Intro to Digital Image Processing 
// used OpenCV 2.3.1 and VS2010 SP1 to develop this solution

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <cassert>

using namespace cv;

Mat_<Vec3b> image;
int discreteAngles = 512;

void on_mouse(int eventCode, int centerX, int centerY, int flags, void* params);
int str2int(const std::string &str);

int main(int argc, char* argv[])
{
   // command itself is one element of argument array...
   if(argc != 1 && argc != 3)
   {
      std::cout << "Expecting two arguments to the application: angular granularity as a whole number and a file name." << std::endl;
      exit(0);
   }

   std::string discreteAnglesStr, fileName;

   if(argc == 3)
   {
      discreteAnglesStr = argv[1];
      fileName          = argv[2];
   }
   else
   {
      discreteAnglesStr = "64";
      fileName          = "boats.tif";
   }

   try
   {
      discreteAngles = str2int(discreteAnglesStr);
      auto image_ = imread(fileName);
      int channels = image_.channels();
      assert(channels == 3);
      image = image_;

      if(image.rows == 0)
         throw new std::exception();

      auto originalImageStr = "Original Image";
      namedWindow(originalImageStr);
      setMouseCallback(originalImageStr, on_mouse);
      imshow(originalImageStr, image);
   }
   catch(std::exception e)
   {
      std::cout << "could not load image." << std::endl;
   }
   waitKey(0);
   return -1;
}

// borrowed from http://stackoverflow.com/q/194465/90475, courtesy of Luka Marinko
int str2int(const std::string &str)
{
   std::stringstream ss(str);
   int num;
   if((ss >> num).fail())
   { 
      throw new std::exception("could not parse user input!");
   }
   return num;
}

double compute_max_madius(int imageRows, int imageCols, int centerX, int centerY)
{
   auto otherX = imageCols - centerX;
   auto otherY = imageRows - centerY;

   auto a = sqrt((double)centerX * centerX + centerY * centerY);
   auto b = sqrt((double)otherX * otherX + centerY * centerY);
   auto c = sqrt((double)centerX * centerX + otherY * otherY);
   auto d = sqrt((double)otherX * otherX + otherY * otherY);

   return max(max(a,b), max(c,d));
}

Vec3b interpolate_with_nearest(const Mat_<Vec3b>& imageArg, double x, double y)
{
   auto x0 = static_cast<int>(floor(x)); auto y0 = static_cast<int>(floor(y));
   auto x1 = static_cast<int>(ceil(x));  auto y1 = static_cast<int>(ceil(y));

   // Rolls over to the other side, esp. for angles
   if(x0 < 0) x0 = imageArg.rows - 1;
   if(y0 < 0) y0 = imageArg.cols - 1;

   if (x1 == imageArg.rows) x1 = 0;
   if (y1 == imageArg.cols) y1 = 0;

   int chosenX, chosenY;
   if (x - x0 < 0.5) chosenX = x0; else chosenX = x1;
   if (y - y0 < 0.5) chosenY = y0; else chosenY = y1;

   Vec3b originalColor = Vec3b(0, 0, 0);
   if (chosenX >= 0 && chosenX < imageArg.rows &&
      chosenY >= 0 && chosenY < imageArg.cols)
   {
      originalColor = imageArg(/*row*/chosenX, /*column*/chosenY);
   }

   return originalColor;
}

Vec3b interpolate_with_bilinear(const Mat_<Vec3b>& imageArg, double x, double y)
{
   auto x0 = static_cast<int>(floor(x)); auto y0 = static_cast<int>(floor(y));
   auto x1 = static_cast<int>(ceil(x));  auto y1 = static_cast<int>(ceil(y));

   // Rolls over to the other side, esp. for angles
   if(x0 < 0) x0 = imageArg.rows - 1;
   if(y0 < 0) y0 = imageArg.cols - 1;

   if (x1 == imageArg.rows) x1 = 0;
   if (y1 == imageArg.cols) y1 = 0;

   if (!(
      x0 >= 0 && x0 < imageArg.rows &&
      x1 >= 0 && x1 < imageArg.rows &&
      y0 >= 0 && y0 < imageArg.cols &&
      y1 >= 0 && y1 < imageArg.cols))
      return Vec3b(0, 0, 0);

   auto f00 = imageArg(x0, y0);
   auto f01 = imageArg(x0, y1);
   auto f10 = imageArg(x1, y0);
   auto f11 = imageArg(x1, y1);

   auto b1 = f00;
   auto b2 = f10 - f00;
   auto b3 = f01 - f00;
   auto b4 = f00 + f11 - f01 - f10;

   x = x - x0;
   y = y - y0;

   return b1 + b2 * x + b3 * y + b4 * x * y;
}

void on_mouse(int eventCode, int centerX, int centerY, int flags, void* params)
{
   if(eventCode == 0)
      return;

   switch( eventCode )
   {
   case CV_EVENT_LBUTTONDOWN:
      {
         std::cout << "Center was (" << centerX << ", " << centerY << ")" << std::endl;

         auto maxRadiusXY = compute_max_madius(image.rows, image.cols, centerX, centerY);
         int discreteRadii = static_cast<int>(floor(maxRadiusXY));

         Mat_<Vec3b> polarImg1;
         polarImg1.create(/*rows*/discreteRadii, /*cols*/discreteAngles);

         Mat_<Vec3b> polarImg2;
         polarImg2.create(/*rows*/discreteRadii, /*cols*/discreteAngles);

         for (int radius = 0; radius < discreteRadii; radius++) // radii
         {
            for (int discreteAngle = 0; discreteAngle < discreteAngles; discreteAngle++) // discreteAngles
            {
               // 3
               auto angleRad = discreteAngle * 2.0 * CV_PI / discreteAngles;

               // 2
               auto xTranslated = cos(angleRad) * radius;
               auto yTranslated = sin(angleRad) * radius;

               // 1
               auto x = centerX + xTranslated;
               auto y = centerY - yTranslated;

               polarImg1(/*row*/ radius, /*column*/ discreteAngle) = interpolate_with_nearest(image, /*row*/y, /*column*/x);
               polarImg2(/*row*/ radius, /*column*/ discreteAngle) = interpolate_with_bilinear(image, /*row*/y, /*column*/x);
            }
         }

         auto polarImage1Str = "Polar (nearest)";
         namedWindow(polarImage1Str);
         imshow(polarImage1Str, polarImg1);

         auto polarImage2Str = "Polar (bilinear)";
         namedWindow(polarImage2Str);
         imshow(polarImage2Str, polarImg2);

         Mat_<Vec3b> reprocessedImg1;
         reprocessedImg1.create(Size(image.rows, image.cols));

         Mat_<Vec3b> reprocessedImg2;
         reprocessedImg2.create(Size(image.rows, image.cols));

         for(int y = 0; y < image.rows; y++)
         {
            for(int x = 0; x < image.cols; x++)
            {
               // 1
               auto xTranslated = x - centerX;
               auto yTranslated = -(y - centerY);

               // 2
               auto radius = sqrt((double)xTranslated * xTranslated + yTranslated * yTranslated);

               double angleRad;
               if(xTranslated != 0)
               {
                  angleRad = atan((double)abs(yTranslated) / abs(xTranslated));

                  // I Quadrant
                  if (xTranslated > 0 && yTranslated > 0)
                     angleRad = angleRad;
                  // II Quadrant
                  if (xTranslated < 0 && yTranslated > 0)
                     angleRad = CV_PI - angleRad;
                  // III Quadrant
                  if (xTranslated < 0 && yTranslated < 0)
                     angleRad = CV_PI + angleRad;
                  /// IV Quadrant
                  if (xTranslated > 0 && yTranslated < 0)
                     angleRad = 2 * CV_PI - angleRad;

                  if (yTranslated == 0)
                     if (xTranslated > 0) angleRad = 0;
                     else angleRad = CV_PI;
               }
               else
               {
                  if (yTranslated > 0) angleRad = CV_PI / 2;
                  else angleRad = 3 * CV_PI / 2;
               }

               // 3
               auto discreteAngle = angleRad * discreteAngles / (2.0 * CV_PI);

               reprocessedImg1(/*row*/ y, /*column*/ x) = interpolate_with_nearest(polarImg1, /*row*/radius, /*column*/discreteAngle);
               reprocessedImg2(/*row*/ y, /*column*/ x) = interpolate_with_bilinear(polarImg2, /*row*/radius, /*column*/discreteAngle);
            }
         }

         auto reprocessedImg1Str = "Re-processed (nearest)";
         namedWindow(reprocessedImg1Str);
         imshow(reprocessedImg1Str, reprocessedImg1);

         auto reprocessedImg2Str = "Re-processed (bilinear)";
         namedWindow(reprocessedImg2Str);
         imshow(reprocessedImg2Str, reprocessedImg2);

      } break;
   }
}
//HW1数字图像处理简介
//使用OpenCV 2.3.1和VS2010 SP1开发此解决方案
#包括“opencv2/core/core.hpp”
#包括“opencv2/highgui/highgui.hpp”
#包括
#包括
使用名称空间cv;
物象;
int离散角=512;
鼠标上的void(int-eventCode、int-centerX、int-centerY、int-flags、void*params);
int str2int(const std::string&str);
int main(int argc,char*argv[])
{
//命令本身是参数数组的一个元素。。。
如果(argc!=1&&argc!=3)
{
std::cout=0&&chosenY=0&&x0=0&&x1=0&&y0=0&&y1
如果需要使用CvMat对象,可能需要尝试使用函数。它将CvArr*作为参数,因此IPLImage和CvMat都适用。如果你将离开C API并进入更现代的东西,你可以使用对象加载图像并使用C++。p>
问题是,为什么要转换已有的矩阵格式(IPLImage以及所有其他都是矩阵)。如果您想要布尔类型的矩阵,请使用或模板类进行此操作。

第一眼看到您的问题会引发更多问题。。。尝试指定一点(我似乎看不到您的代码示例,我是stackoverflow新手) 例如,您的open cv版本和IDE(如代码块或Microsoft Visual Studio)。但是在你的问题中包括它。我还想知道的是,这样做的目的是什么?为什么需要矩阵等等:)

试图回答

据我所知
“但是我在Visual C++ 2010上安装了OpenCV版本2.3.1——Ayesha Khan”

OpenCV使用名为Mat的类,您应该经常遇到这个类。这个类本质上已经是一个矩阵了。如果我没记错的话,它和向量非常相似,我在这里不讨论

因此,如果您需要访问中的任何像素值,可以这样说

Mat Img;
您将在类的这个实例中使用一个函数

cout << Img.at<uchar>(x,y);

cout您应该做的第一件事是添加一个代码示例,说明您到目前为止所获得的以及您想要的。我已经添加了进一步的解释。谢谢,但我正在寻找将图像转换为矩阵的方法,我应该如何做?我已经编辑了我的摘要以进一步解释。你能看到这个,让我KNW?但是我已经安装了OpenCV版本2.3.1在VisualC++ 2010年,我想,像CVMAT*MAT= CVCRATATEMAT(IMGBBW->高度,IMGBBW->宽度,CVY8UC3);需要完成的工作,可建议阅读www.amazon.com/OpenCV-Computer-Application-Programming-Cookbook/dp/1849513244