在 C++ 中旋转图像而不使用 OpenCV 函数

2024-04-13

描述 :-我正在尝试在不使用 C++ 中的 OpenCV 函数的情况下旋转图像。旋转中心不必是图像的中心。它可能是不同的点(偏离图像中心)。到目前为止,我遵循各种来源进行图像插值,我知道source https://stackoverflow.com/a/1843931/10019073它在 MATLAB 中完美地完成了这项工作。我尝试在没有 OpenCV 函数的情况下在 C++ 中模仿相同的内容。但我没有得到预期的旋转图像。相反,我的输出在屏幕上看起来像一条小水平线。

void RotateNearestNeighbor(cv::Mat src, double angle) {
int oldHeight = src.rows;
int oldWidth = src.cols;
int newHeight = std::sqrt(2) * oldHeight;
int newWidth = std::sqrt(2) * oldWidth;
cv::Mat output = cv::Mat(newHeight, newWidth, src.type());
double ctheta = cos(angle);
double stheta = sin(angle);

for (size_t i = 0; i < newHeight; i++) {
    for (size_t j = 0; j < newWidth; j++) {

        int oldRow = static_cast<int> ((i - newHeight / 2) * ctheta +
                                       (j - newWidth / 2) * stheta + oldHeight / 2);
        int oldCol = static_cast<int> (-(i - newHeight / 2) * stheta +
                                       (j - newWidth / 2) * ctheta + oldWidth / 2);

        if (oldRow > 0 && oldCol > 0 && oldRow <= oldHeight && oldCol <= oldWidth)
            output.at<cv::Vec3b>(i, j) = src.at<cv::Vec3b>(oldRow, oldCol);
        else
            output.at<cv::Vec3b>(i, j) = cv::Vec3b(0, 0, 0);
    }
}
cv::imshow("Rotated cat", output);
}

以下是我的输入(左侧)和输出(右侧)图像


更新 : -

在受到与这个问题相关的许多答案以及下面最详尽、最有帮助和慷慨的答案的启发后,我可以修复我的 OpenCV 代码以获得所需的结果。

修改后的代码:

// Trivial constant
constexpr double Pi = 3.1415926535897932384626433832795;

/*!
* \brief Function to generate transformation matrix
* \param angle is the angle of rotation from user input
* \param pivot is the amount of translation in x and y axes
* \return translation matrix
*/
cv::Mat CreateTransMat(double angle, std::pair<int, int> &pivot) {
    angle = Pi * angle / 180;
    return (cv::Mat_<double>(3, 3) << cos(angle), -sin(angle), pivot.first,
            sin(angle), cos(angle), pivot.second, 0, 0, 1);
}

/*!
* \brief Function to apply coordinate transform from destination to     source
* \param inv_mat being the inverse transformation matrix for the transform needed
* \return pos being the homogeneous coordinates for transformation
*/
cv::Mat CoordTransform(const cv::Mat &inv_mat, const cv::Mat &pos) {
    assert(inv_mat.cols == pos.rows);
    cv::Mat trans_mat = inv_mat * pos;
    return (cv::Mat_<double>(1, 2) <<
            trans_mat.at<double>(0, 0) / trans_mat.at<double>(0, 2),
            trans_mat.at<double>(0, 1) / trans_mat.at<double>(0, 2));
}

/*!
* \brief Function to transform an image based on a rotation angle and translation
         matrix. When rotation and translation happen at the same time, the
         two matrices can be combined
* \param src being source image
* \param dest being destination image
* \param trans_mat being the transformation (rotation/ translation) matrix
*/
void ImageTransform(const cv::Mat &src, const cv::Mat &trans_mat, cv::Mat &dest) {
    int src_rows = src.rows;
    int src_cols = src.cols;
    int dest_rows = dest.rows;
    int dest_cols = dest.cols;
    const cv::Mat inverse_mat = trans_mat.inv();
    //#pragma omp parallel for simd
    for (int row = 0; row < dest_rows; row++) {
        //#pragma omp parallel for simd
        for (int col = 0; col < dest_cols; col++) {
            cv::Mat src_pos = CoordTransform(inverse_mat,
                                         (cv::Mat_<double>(3, 1) << col, row, 1));
            const int x_actual = static_cast<int>(src_pos.at<double>(0, 0) + 0.5);
            const int y_actual = static_cast<int>(src_pos.at<double>(0, 1) + 0.5);

            if (x_actual >= 0 && x_actual < src_cols &&
                y_actual >= 0 && y_actual < src_rows)
                dest.at<cv::Vec3b>(row, col) = src.at<cv::Vec3b>(y_actual, x_actual);
            else
                dest.at<cv::Vec3b>(row, col) = cv::Vec3b(0, 0, 0);
        }
    }    
}

/*!
* \brief User manual for command-line args input
*/
void Usage() {
    std::cout << "COMMAND INPUT : - \n\n" <<
              "          ./ImageTransform <image> <rotation-angle>" <<
              std::endl;
}
/*!
* \brief main function to read a user input location for an image and then apply the
         required transformations (rotation / translation)
*/
int main(int argc, char *argv[])
{
    auto start = std::chrono::steady_clock::now();
    if (argc == 0 || argc < 3)
        Usage();
    else {
        double degree = std::stod(argv[2]);
        double angle = degree * CV_PI / 180.;
        cv::Mat src_img = cv::imread(argv[1]);
        std::pair<int, int> null_trans = std::make_pair(0, 0);
        std::pair<int, int> translation_initial =
            std::make_pair(src_img.cols / 2 + 1, src_img.rows / 2 + 1);
        std::pair<int, int> translation_final =
            std::make_pair(0, -src_img.rows / 2 - 4);
        if (!src_img.data)
        {
            std::cout << "image null" << std::endl;
            cv::waitKey(0);
        }
        cv::imshow("Source", src_img);
        cv::Mat dest_img = cv::Mat(static_cast<int>(2 * src_img.rows),
                                   static_cast<int>(2 * src_img.cols),
                                   src_img.type());
        cv::Mat trans_mat1 = CreateTransMat(degree, translation_initial);
        ImageTransform(src_img, trans_mat1, dest_img);
        cv::imshow("Interim", dest_img);
        cv::Mat interim_img = dest_img;
        dest_img.release();
        dest_img = cv::Mat(src_img.rows, src_img.cols, src_img.type());
        cv::Mat trans_mat2 = CreateTransMat(0, translation_final);
        ImageTransform(interim_img, trans_mat2, dest_img);
        cv::imshow("Final image", dest_img);
        cv::waitKey(10);
    }
    auto end = std::chrono::steady_clock::now();
    auto diff = end - start;
    std::cout << std::chrono::duration <double, std::milli> (diff).count() <<
              " ms" << std::endl;
}

输入图像

旋转图像


首先,我必须承认我同意generic_opto_guy https://stackoverflow.com/users/9400869/generic-opto-guy:

循环方法看起来不错,所以我们需要检查数学。我注意到的事情是: if (oldRow > 0 && oldCol > 0 && oldRow

尽管如此,我还是忍不住回答。 (也许,这只是我的一个形象阶段。)

我建议使用矩阵变换,而不是摆弄 sin() 和 cos()。乍一看,这可能显得过度设计,但后来您会发现它具有更大的灵活性。使用变换矩阵,您可以表达多种变换(平移、旋转、缩放、剪切、投影)以及将多个变换组合到一个矩阵中。

(可能发生的事情的预告片:SO:如何在 2D 中绘制/变形 QImage? https://stackoverflow.com/a/56970955/7478597)

在图像中,像素可以通过二维坐标来寻址。因此,我们想到了 2×2 矩阵,但 2×2 矩阵无法表达平移。为了这,齐次坐标 https://en.wikipedia.org/wiki/Homogeneous_coordinates引入了一种数学技巧,通过将维度扩展一来处理同一空间中的位置和方向。

简而言之,二维位置 (x, y) 具有齐次坐标 (x, y, 1)。

用变换矩阵变换的位置:

= M · v.

This may or may not change the value of third component. To convert the homogeneous coordinate to 2D position again, x and y has to be divided by 3rd component.

Vec2 transform(const Mat3x3 &mat, const Vec2 &pos)
{
  const Vec3 pos_ = mat * Vec3(pos, 1.0);
  return Vec2(pos_.x / pos_.z, pos_.y / pos_.z);
}

要将源图像转换为目标图像,可以使用以下函数:

void transform(
  const Image &imgSrc, const Mat3x3 &mat, Image &imgDst,
  int rgbFail = 0x808080)
{
  const Mat3x3 matInv = invert(mat);
  for (int y = 0; y < imgDst.h(); ++y) {
    for (int x = 0; x < imgDst.w(); ++x) {
      const Vec2 pos = transform(matInv, Vec2(x, y));
      const int xSrc = (int)(pos.x + 0.5), ySrc = (int)(pos.y + 0.5);
      imgDst.setPixel(x, y,
        xSrc >= 0 && xSrc < imgSrc.w() && ySrc >= 0 && ySrc < imgSrc.h()
        ? imgSrc.getPixel(xSrc, ySrc)
        : rgbFail);
    }
  }
}

Note:

变换矩阵mat描述源图像坐标到目标图像坐标的转换。嵌套循环迭代目标图像。因此,必须使用逆矩阵(表示逆变换)来获取映射到当前目标坐标的相应源图像坐标。

…以及旋转的矩阵构造函数:

enum ArgInitRot { InitRot };

template <typename VALUE>
struct Mat3x3T {
  union {
    VALUE comp[3 * 3];
    struct {
      VALUE _00, _01, _02;
      VALUE _10, _11, _12;
      VALUE _20, _21, _22;
    };
  };

  // constructor to build a matrix for rotation
  Mat3x3T(ArgInitRot, VALUE angle):
    _00(std::cos(angle)), _01(-std::sin(angle)), _02((VALUE)0),
    _10(std::sin(angle)), _11( std::cos(angle)), _12((VALUE)0),
    _20(       (VALUE)0), _21(        (VALUE)0), _22((VALUE)1)
  { }

可以用来构造一个旋转angle(以度为单位):

Mat3x3T<double> mat(InitRot, degToRad(30.0));

Note:

我想强调一下变换后的坐标是如何使用的:

      const Vec2 pos = transform(matInv, Vec2(x, y));
      const int xSrc = (int)(pos.x + 0.5), ySrc = (int)(pos.y + 0.5);

对结果进行四舍五入以产生一个离散像素位置实际上就是所谓的最近邻。或者,现在丢弃的小数部分可以用于相邻像素之间的线性插值。


为了制作一个小样本,我首先复制了image.h, image.cc, imagePPM.h, and imagePPM.cc https://stackoverflow.com/a/56850226/7478597来自我最近写的另一个答案。 (这PPM 文件格式 https://en.wikipedia.org/wiki/Netpbm_format已被使用,因为它需要最少的文件 I/O 代码。)

接下来,我用了linMath.h https://github.com/scheff173/NoGL3dDemo/blob/master/linmath.h(我的 3D 变换的最小数学集合)为 2D 变换创建一个最小的数学集合 –linMath.h:

#ifndef LIN_MATH_H
#define LIN_MATH_H

#include <iostream>
#include <cassert>
#include <cmath>

extern const double Pi;

template <typename VALUE>
inline VALUE degToRad(VALUE angle)
{
  return (VALUE)Pi * angle / (VALUE)180;
}

template <typename VALUE>
inline VALUE radToDeg(VALUE angle)
{
  return (VALUE)180 * angle / (VALUE)Pi;
}

enum ArgNull { Null };

template <typename VALUE>
struct Vec2T {
  typedef VALUE Value;
  Value x, y;
  // default constructor (leaving elements uninitialized)
  Vec2T() { }
  Vec2T(ArgNull): x((Value)0), y((Value)0) { }
  Vec2T(Value x, Value y): x(x), y(y) { }
};

typedef Vec2T<float> Vec2f;
typedef Vec2T<double> Vec2;

template <typename VALUE>
struct Vec3T {
  typedef VALUE Value;
  Value x, y, z;
  // default constructor (leaving elements uninitialized)
  Vec3T() { }
  Vec3T(ArgNull): x((Value)0), y((Value)0), z((Value)0) { }
  Vec3T(Value x, Value y, Value z): x(x), y(y), z(z) { }
  Vec3T(const Vec2T<Value> &xy, Value z): x(xy.x), y(xy.y), z(z) { }
  explicit operator Vec2T<Value>() const { return Vec2T<Value>(x, y); }
  const Vec2f xy() const { return Vec2f(x, y); }
  const Vec2f xz() const { return Vec2f(x, z); }
  const Vec2f yz() const { return Vec2f(y, z); }
};

typedef Vec3T<float> Vec3f;
typedef Vec3T<double> Vec3;

enum ArgInitIdent { InitIdent };
enum ArgInitTrans { InitTrans };
enum ArgInitRot { InitRot };
enum ArgInitScale { InitScale };
enum ArgInitFrame { InitFrame };

template <typename VALUE>
struct Mat3x3T {
  union {
    VALUE comp[3 * 3];
    struct {
      VALUE _00, _01, _02;
      VALUE _10, _11, _12;
      VALUE _20, _21, _22;
    };
  };

  // default constructor (leaving elements uninitialized)
  Mat3x3T() { }
  // constructor to build a matrix by elements
  Mat3x3T(
    VALUE _00, VALUE _01, VALUE _02,
    VALUE _10, VALUE _11, VALUE _12,
    VALUE _20, VALUE _21, VALUE _22):
    _00(_00), _01(_01), _02(_02),
    _10(_10), _11(_11), _12(_12),
    _20(_20), _21(_21), _22(_22)
  { }
  // constructor to build an identity matrix
  Mat3x3T(ArgInitIdent):
    _00((VALUE)1), _01((VALUE)0), _02((VALUE)0),
    _10((VALUE)0), _11((VALUE)1), _12((VALUE)0),
    _20((VALUE)0), _21((VALUE)0), _22((VALUE)1)
  { }
  // constructor to build a matrix for translation
  Mat3x3T(ArgInitTrans, const Vec2T<VALUE> &t):
    _00((VALUE)1), _01((VALUE)0), _02((VALUE)t.x),
    _10((VALUE)0), _11((VALUE)1), _12((VALUE)t.y),
    _20((VALUE)0), _21((VALUE)0), _22((VALUE)1)
  { }
  // constructor to build a matrix for rotation
  Mat3x3T(ArgInitRot, VALUE angle):
    _00(std::cos(angle)), _01(-std::sin(angle)), _02((VALUE)0),
    _10(std::sin(angle)), _11( std::cos(angle)), _12((VALUE)0),
    _20(       (VALUE)0), _21(        (VALUE)0), _22((VALUE)1)
  { }
  // constructor to build a matrix for translation/rotation
  Mat3x3T(ArgInitFrame, const Vec2T<VALUE> &t, VALUE angle):
    _00(std::cos(angle)), _01(-std::sin(angle)), _02((VALUE)t.x),
    _10(std::sin(angle)), _11( std::cos(angle)), _12((VALUE)t.y),
    _20(       (VALUE)0), _21(        (VALUE)0), _22((VALUE)1)
  { }
  // constructor to build a matrix for scaling
  Mat3x3T(ArgInitScale, VALUE sx, VALUE sy):
    _00((VALUE)sx), _01( (VALUE)0), _02((VALUE)0),
    _10( (VALUE)0), _11((VALUE)sy), _12((VALUE)0),
    _20( (VALUE)0), _21( (VALUE)0), _22((VALUE)1)
  { }
  // operator to allow access with [][]
  VALUE* operator [] (int i)
  {
    assert(i >= 0 && i < 3);
    return comp + 3 * i;
  }
  // operator to allow access with [][]
  const VALUE* operator [] (int i) const
  {
    assert(i >= 0 && i < 3);
    return comp + 3 * i;
  }

  // multiply matrix with matrix -> matrix
  Mat3x3T operator * (const Mat3x3T &mat) const
  {
    return Mat3x3T(
      _00 * mat._00 + _01 * mat._10 + _02 * mat._20,
      _00 * mat._01 + _01 * mat._11 + _02 * mat._21,
      _00 * mat._02 + _01 * mat._12 + _02 * mat._22,
      _10 * mat._00 + _11 * mat._10 + _12 * mat._20,
      _10 * mat._01 + _11 * mat._11 + _12 * mat._21,
      _10 * mat._02 + _11 * mat._12 + _12 * mat._22,
      _20 * mat._00 + _21 * mat._10 + _22 * mat._20,
      _20 * mat._01 + _21 * mat._11 + _22 * mat._21,
      _20 * mat._02 + _21 * mat._12 + _22 * mat._22);
  }
  // multiply matrix with vector -> vector
  Vec3T<VALUE> operator * (const Vec3T<VALUE> &vec) const
  {
    return Vec3T<VALUE>(
      _00 * vec.x + _01 * vec.y + _02 * vec.z,
      _10 * vec.x + _11 * vec.y + _12 * vec.z,
      _20 * vec.x + _21 * vec.y + _22 * vec.z);
  }
};

typedef Mat3x3T<float> Mat3x3f;
typedef Mat3x3T<double> Mat3x3;

template <typename VALUE>
std::ostream& operator<<(std::ostream &out, const Mat3x3T<VALUE> &m)
{
  return out
    << m._00 << '\t' << m._01 << '\t' << m._02 << '\n'
    << m._10 << '\t' << m._11 << '\t' << m._12 << '\n'
    << m._20 << '\t' << m._21 << '\t' << m._22 << '\n';
}

/* computes determinant of a matrix.
 *
 * det = |M|
 *
 * mat ... the matrix
 */
template <typename VALUE>
VALUE determinant(const Mat3x3T<VALUE> &mat)
{
  return mat._00 * mat._11 * mat._22
    + mat._01 * mat._12 * mat._20
    + mat._02 * mat._10 * mat._21
    - mat._20 * mat._11 * mat._02
    - mat._21 * mat._12 * mat._00
    - mat._22 * mat._10 * mat._01;
}

/* returns the inverse of a regular matrix.
 *
 * mat matrix to invert
 * eps epsilon for regularity of matrix
 */
template <typename VALUE>
Mat3x3T<VALUE> invert(
  const Mat3x3T<VALUE> &mat, VALUE eps = (VALUE)1E-10)
{
  assert(eps >= (VALUE)0);
  // compute determinant and check that it its unequal to 0
  // (Otherwise, matrix is singular!)
  const VALUE det = determinant(mat);
  if (std::abs(det) < eps) throw std::domain_error("Singular matrix!");
  // reciproke of determinant
  const VALUE detInvPos = (VALUE)1 / det, detInvNeg = -detInvPos;
  // compute each element by determinant of sub-matrix which is build
  // striking out row and column of pivot element itself
  // BTW, the determinant is multiplied with -1 when sum of row and column
  // index is odd (chess board rule)
  // (This is usually called cofactor of related element.)
  // transpose matrix and multiply with 1/determinant of original matrix
  return Mat3x3T<VALUE>(
    detInvPos * (mat._11 * mat._22 - mat._12 * mat._21),
    detInvNeg * (mat._01 * mat._22 - mat._02 * mat._21),
    detInvPos * (mat._01 * mat._12 - mat._02 * mat._11),
    detInvNeg * (mat._10 * mat._22 - mat._12 * mat._20),
    detInvPos * (mat._00 * mat._22 - mat._02 * mat._20),
    detInvNeg * (mat._00 * mat._12 - mat._02 * mat._10),
    detInvPos * (mat._10 * mat._21 - mat._11 * mat._20),
    detInvNeg * (mat._00 * mat._21 - mat._01 * mat._20),
    detInvPos * (mat._00 * mat._11 - mat._01 * mat._10));
}

#endif // LIN_MATH_H

和定义Pi in linMath.cc:

#include "linmath.h"

const double Pi = 3.1415926535897932384626433832795;

有了所有可用的工具,我制作了示例应用程序xformRGBImg.cc:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

#include "linMath.h"
#include "image.h"
#include "imagePPM.h"

typedef unsigned int uint;

struct Error {
  const std::string text;
  Error(const char *text): text(text) { }
};

const char* readArg(int &i, int argc, char **argv)
{
  ++i;
  if (i >= argc) throw Error("Missing argument!");
  return argv[i];
}

uint readArgUInt(int &i, int argc, char **argv)
{
  const char *arg = readArg(i, argc, argv); char *end;
  const unsigned long value = strtoul(arg, &end, 0);
  if (arg == end || *end) throw Error("Unsigned integer value expected!");
  if ((uint)value != value) throw Error("Unsigned integer overflow!");
  return (uint)value;
}

double readArgDouble(int &i, int argc, char **argv)
{
  const char *arg = readArg(i, argc, argv); char *end;
  const double value = strtod(arg, &end);
  if (arg == end || *end) throw Error("Floating point value expected!");
  return value;
}

std::pair<uint, uint> resize(int &i, int argc, char **argv)
{
  const uint w = readArgUInt(i, argc, argv);
  const uint h = readArgUInt(i, argc, argv);
  return std::make_pair(w, h);
}

Mat3x3 translate(int &i, int argc, char **argv)
{
  const double x = readArgDouble(i, argc, argv);
  const double y = readArgDouble(i, argc, argv);
  return Mat3x3(InitTrans, Vec2(x, y));
}

Mat3x3 rotate(int &i, int argc, char **argv)
{
  const double angle = readArgDouble(i, argc, argv);
  return Mat3x3(InitRot, degToRad(angle));
}

Mat3x3 scale(int &i, int argc, char **argv)
{
  const double x = readArgDouble(i, argc, argv);
  const double y = readArgDouble(i, argc, argv);
  return Mat3x3(InitScale, x, y);
}

Vec2 transform(const Mat3x3 &mat, const Vec2 &pos)
{
  const Vec3 pos_ = mat * Vec3(pos, 1.0);
  return Vec2(pos_.x / pos_.z, pos_.y / pos_.z);
}

void transform(
  const Image &imgSrc, const Mat3x3 &mat, Image &imgDst,
  int rgbFail = 0x808080)
{
  const Mat3x3 matInv = invert(mat);
  for (int y = 0; y < imgDst.h(); ++y) {
    for (int x = 0; x < imgDst.w(); ++x) {
      const Vec2 pos = transform(matInv, Vec2(x, y));
      const int xSrc = (int)(pos.x + 0.5), ySrc = (int)(pos.y + 0.5);
      imgDst.setPixel(x, y,
        xSrc >= 0 && xSrc < imgSrc.w() && ySrc >= 0 && ySrc < imgSrc.h()
        ? imgSrc.getPixel(xSrc, ySrc)
        : rgbFail);
    }
  }
}

const char *const usage =
  "Usage:\n"
  "  xformRGBImg IN_FILE OUT_FILE [[CMD]...]\n"
  "\n"
  "Commands:\n"
  "  resize W H\n"
  "  translate X Y\n"
  "  rotate ANGLE\n"
  "  scale SX SY\n";

int main(int argc, char **argv)
{
  // read command line arguments
  if (argc <= 2) {
    std::cerr << "Missing arguments!\n";
    std::cout << usage;
    return 1;
  }
  const std::string inFile = argv[1];
  const std::string outFile = argv[2];
  std::pair<uint, uint> sizeOut(0, 0);
  Mat3x3 mat(InitIdent);
  for (int i = 3; i < argc; ++i) try {
    const std::string cmd = argv[i];
    if (cmd == "resize") sizeOut = resize(i, argc, argv);
    else if (cmd == "translate") mat = translate(i, argc, argv) * mat;
    else if (cmd == "rotate") mat = rotate(i, argc, argv) * mat;
    else if (cmd == "scale") mat = scale(i, argc, argv) * mat;
    else {
      std::cerr << "Wrong command!\n";
      std::cout << usage;
      return 1;
    }
  } catch (const Error &error) {
    std::cerr << "Wrong argument at $" << i << "\n"
      << error.text << '\n';
    std::cout << usage;
    return 1;
  }
  // read image
  Image imgSrc;
  { std::ifstream fIn(inFile.c_str(), std::ios::binary);
    if (!readPPM(fIn, imgSrc)) {
      std::cerr << "Reading '" << inFile << "' failed!\n";
      return 1;
    }
  }
  // set output image size
  if (sizeOut.first * sizeOut.second == 0) {
    sizeOut = std::make_pair(imgSrc.w(), imgSrc.h());
  }
  // transform image
  Image imgDst;
  imgDst.resize(sizeOut.first, sizeOut.second, 3 * sizeOut.second);
  transform(imgSrc, mat, imgDst);
  // write image
  { std::ofstream fOut(outFile.c_str(), std::ios::binary);
    if (!writePPM(fOut, imgDst) || (fOut.close(), !fOut.good())) {
      std::cerr << "Writing '" << outFile << "' failed!\n";
      return 1;
    }
  }
  // done
  return 0;
}

Note:

命令行参数按顺序处理。每个变换命令从单位矩阵开始从左乘到已经组合的变换矩阵。这是因为变换的串联会导致矩阵的逆序乘法。 (矩阵乘法是右结合的。)

例如。对应的矩阵转换:

x' = 翻译(x)
x" = rotate(x')
x"' = scale(x")

which is

x"' = scale(rotate(翻译(x)))

is

Mtransform = Mscale · Mrotate · Mtranslate

and

x"' = Mscale · Mrotate · Mtranslate · x = Mtransform · x

编译并测试于cygwin http://www.cygwin.org:

$ g++ -std=c++11 -o xformRGBImg image.cc imagePPM.cc linMath.cc xformRGBImg.cc

$ ./xformRGBImg                                                               
Missing arguments!
Usage:
  xformRGBImg IN_FILE OUT_FILE [[CMD]...]

Commands:
  resize W H
  translate X Y
  rotate ANGLE
  scale SX SY

$

最后是示例图像cat.jpg(转换成PPM https://en.wikipedia.org/wiki/Netpbm_format in GIMP https://www.gimp.org/):

尺寸为300×300。

Note:

所有嵌入图像均从 PPM 转换为 JPEG(格式为GIMP https://www.gimp.org/再次)。 (图片上传不支持PPM,也无法想象任何浏览器都能正常显示。)

从最低限度开始:

$ ./xformRGBImg cat.ppm cat.copy.ppm

$

它看起来像原来的——身份转换应该是这样的。

现在,旋转 30°:

$ ./xformRGBImg cat.ppm cat.rot30.ppm rotate 30

$

要绕某个中心旋转,就有一个 resp。前后需要翻译:

$ ./xformRGBImg cat.ppm cat.rot30c150,150.ppm \
  translate -150 -150 rotate 30 translate 150 150

$

输出图像可以使用 w · √2 × h · √2 调整大小以适应任何中心旋转。

因此,输出图像的大小调整为 425 × 425,其中最后的平移分别调整为translate 212.5 212.5:

$ ./xformRGBImg cat.ppm cat.rot30c150,150.425x425.ppm \
  resize 425 425 translate -150 -150 rotate 30 translate 212.5 212.5

$

缩放比例尚未检查:

$ ./xformRGBImg cat.ppm cat.rot30c150,150s0.7,0.7.ppm \
  translate -150 -150 rotate 30 scale 0.7 0.7 translate 150 150

$

最后,公平地说,我想提一下我的小玩具工具的“大哥”:图像魔术师 https://imagemagick.org/index.php.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 C++ 中旋转图像而不使用 OpenCV 函数 的相关文章

  • C# 创建函数队列

    我写了一个名为 QueueManager 的类 class QueueManager Queue functionsQueue public bool IsEmpty get if functionsQueue Count 0 return
  • 为什么 VB.NET 和 C# 中针对值检查 null 存在差异?

    In VB NET http en wikipedia org wiki Visual Basic NET有时候是这样的 Dim x As System Nullable Of Decimal Nothing Dim y As System
  • 获取 std::variant 当前持有的 typeid(如 boost::variant type())

    我已经从 boost variant 迁移到 std variant 但遇到了障碍 我在 boost type 中使用了一个很好的函数 它可以让你获取当前持有的 typeid 看https www boost org doc libs 1
  • 使用API​​隐藏程序标题栏

    它可以使用 c 和 windows api 删除窗口控制台标题栏 如果是的话如何 请 这个简单的应用程序隐藏并显示其所在控制台的标题栏 它会立即将控制台标题更改为 guid 以查找窗口句柄 然后 它使用 ToggleTitleBar 使用找
  • 是否允许将类模板类型参数键入相同的名称?

    这似乎可以在 MSVC 中按预期编译甚至工作 但它是合法的 C 代码吗 它是否能保证执行此处所期望的操作 即将模板类型导出到结构体的同名用户 template
  • 在Application_AquireRequestState事件中用POST数据重写Url

    我有一个在其中注册路线的代码Application AcquireRequestState应用程序的事件 注册路由后 我会在 Http 运行时缓存中设置一个标志 这样我就不会再次执行路由注册代码 在此事件中注册路线有特定原因Applicat
  • 如何在编译C代码时禁用警告?

    我正在使用 32 位 Fedora 14 系统 我正在使用编译我的源代码gcc 有谁知道如何在编译c代码时禁用警告 EDIT 是的 我知道 最好的办法是修复这些警告以避免任何未定义 未知的行为 但目前在这里 我第一次编写了巨大的代码 并且在
  • Android 如何从我的应用程序使用 SD 卡中的文件路径预览图像

    文件存在于sdcard image jpg我想创建我自己的应用程序 活动 按下按钮时 需要使用内置图像查看器显示存储在 SD 卡中的图像 按图像查看器中的后退按钮后 它应该返回到我正在运行的应用程序 需要一些帮助 您可以为此创建一个具有适当
  • 序列化和反序列化 Visual Studio 解决方案文件 - 或以编程方式编辑?

    我想以编程方式添加和删除项目 解决方案文件夹和其他项目 例如解决方案的资源文件 但我不确定最好的方法是什么 对于那些不知道的人 高度简化 解决方案文件 sln 通常如下所示 Microsoft Visual Studio Solution
  • 在 MATLAB 中创建共享库

    一位研究人员在 MATLAB 中创建了一个小型仿真 我们希望其他人也能使用它 我的计划是进行模拟 清理一些东西并将其变成一组函数 然后我打算将其编译成C库并使用SWIG https en wikipedia org wiki SWIG创建一
  • 指向字节数组的指针

    由于 Misra C 的要求 我的一位同事想要使用指针声明 但我遇到了一些问题 Misra 安全关键指南 不会让我们纯粹的程序员使用指针 但会让我们对数组字节进行操作 他打算获取一个指向字节数组的指针 因此我们不会在堆栈上传递实际的数组 T
  • 防止GDB中的PLT(过程链接表)断点

    在最新版本的 GDB 中 在库函数调用上设置断点会导致多个实际断点 调用过程链接表 PLT 实际的函数调用 这意味着当调用库函数时 我们每次都会经历两次中断 在以前的 GDB 版本中 只会创建 2 因此您只能得到一次中断 那么问题来了 是否
  • 更改 IdentityServer4 实体框架表名称

    我正在尝试更改由 IdentityServer4 的 PersistedGrantDb 和 ConfigurationDb 创建的默认表名称 并让实体框架生成正确的 SQL 例如 而不是使用实体IdentityServer4 EntityF
  • 如何用 C 语言练习 Unix 编程?

    经过五年的专业 Java 以及较小程度上的 Python 编程并慢慢感觉到我的计算机科学教育逐渐消失 我决定要拓宽我的视野 对世界的一般用处 并做一些 对我来说 感觉更重要的事情就像我真的对机器有影响一样 我选择学习 C 和 Unix 编程
  • 错误左值需要作为赋值C++的左操作数

    整个程序基本上只允许用户移动光标 如果用户位于给定的坐标范围 2 2 内 则允许用户键入输入 我刚刚提供了一些我认为足以解决问题的代码 我不知道是什么导致了这个问题 你能解释一下为什么会发生吗 void goToXY int int 创建一
  • 不兼容的类型 - 是因为数组已经是指针吗?

    在下面的代码中 我创建一个基于书籍结构的对象 并让它保存多个 书籍 我设置的是一个数组 即定义 启动的对象 然而 每当我去测试我对指针的了解 实践有帮助 并尝试创建一个指向创建的对象的指针时 它都会给我错误 C Users Justin D
  • 使用(linq to sql)更新错误

    我有两个表 通过外键 CarrierID 绑定 Carrier CarrierID CarrierName CarrierID 1 CarrierName DHL CarrierID 2 CarrierName Fedex Vendor V
  • 使用 foreach 循环和 XmlNodeList C# 将新节点附加到节点列表

    目前我处理的是这样的XML类型 XML FILE http 20drive google com open id 0By5BxgNi9eGcRldxcEZNU0FDTzQ 参考XML文件 我想检查一个节点 如果找不到该节点 我必须将该节点附
  • “int i=1,2,3”和“int i=(1,2,3)”之间的区别 - 使用逗号运算符的变量声明[重复]

    这个问题在这里已经有答案了 int i 1 2 3 int i 1 2 3 int i i 1 2 3 这些说法有什么区别 我无法找出任何具体原因 Statement 1 Result Compile error 运算符的优先级高于 运算符
  • FindAsync 很慢,但是延迟加载很快

    在我的代码中 我曾经使用加载相关实体await FindAsync 希望我能更好地遵守 C 异步指南 var activeTemplate await exec DbContext FormTemplates FindAsync exec

随机推荐

  • 查找两个数组之间的共同值

    如果我想比较两个数组并创建一个插值输出字符串 如果数组中的数组变量 y存在于x如何获得每个匹配元素的输出 这就是我正在尝试的 但没有完全得到结果 x 1 2 4 y 5 2 4 x each do num puts The number n
  • 将 0xFF 写入文件时出现问题

    我正在尝试使用 PrintStream 将 0xFF 写入 java 文件 当我使用十六进制编辑器打开文件时 其他值会正确写入文件 但应该显示 0xFF 的值却显示为 0xC3BF 使用的变量类型是int 经过几次尝试后 我还发现我可以输入
  • List>Remove() 方法

    我想在列表列表上使用 Remove 方法 但它对我不起作用 简单的例子应该说明一切 List
  • Laravel 多个 WHERE 子句

    我需要向 Laravel SQL 查询添加多个 where 子句 到目前为止我的 PHP 代码是 date default timezone set America Los Angeles today getdate year today
  • Magento 购物车未更新阿拉伯语商店视图中的数量

    我在 Magento 1 8 1 安装中遇到以下问题 我有两种商店视图 英语 默认 和阿拉伯语 在英文商店视图中 如果我将产品添加到购物车 我可以通过在数量框中输入新数量并单击更新购物车来修改数量 这会更改数量和总数 但是 当我切换到阿拉伯
  • @font-face 在 Firefox 中不配合

    我尝试了很多事情 包括单击与我的问题相关的所有问题 有很多 并尝试了他们所有的 解决方案 但没有一个对我有用 我尝试将 eot 文件包装在条件 IE 语句中 但这也不起作用 有人说如果你不在自己的服务器上托管文件 font face 将无法
  • 尽管被捕获,生成器内部抛出的错误还是完成了它

    我有一个异步生成器函数 它内部调用几个可能引发错误的异步函数 我想要的是 当错误发生时 生成器只是记录它 然后继续进一步工作 所以我有这样的代码 async getAll somestuff try const thing await fe
  • 通过 Git 推送大文件的问题

    目前 当我尝试推送到 Git 存储库时 出现以下错误 remote error GH001 Large files detected remote error Trace 7bbfe5c1099cfe679aa3cd1eee13e10a r
  • 汇编语言中数组的冒泡排序

    我需要对一个无组织的数组进行冒泡排序 其中包含从最大到最小的 7 个整数 因此它看起来像 9 6 5 4 3 2 1 我通过编译器运行我的代码 它说 我不明白这段代码有什么问题 code segment assume ds code cs
  • 下标迭代器内的 lambda

    在下标运算符中使用 lambda 似乎不适用于 g 和 clang 这是 C 标准中的实现错误还是 不愉快 的规则 Example class A public template
  • XAML 绑定到属性

    我的 XAML C Windows 应用商店应用程序中有复选框 我还有 bool 属性 WindowsStoreTestApp SessionData RememberUser这是公共的和静态的 我想要复选框的属性IsChecked与此 b
  • jQuery 中的多个 AJAX 请求

    我有一个函数 可以从两个位置提取数据并将返回的内容放置在向用户显示的模式对话框上 这两个请求都是异步的 因为它们是跨域的 问题在于我不想在两个请求完成加载之前显示模式 在加载模式之前 如何检查以确保两个请求都已完成 我尝试将 openMod
  • 无法找到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/batch]

    情况 我正在使用 Spring Batch 为我们的数据仓库构建累积快照 但我遇到了一个我无法弄清楚的配置障碍 我使用 STS SpringSource Tool Suite 2 8 1 创建了一个简单的 Spring Batch 项目春季
  • Keras 错误:调用 Predict_on_batch 时“优化循环失败:已取消:操作已取消”

    我有一些使用 keras 的旧工作代码 我最近把它掸掉并尝试使用它 但使用的是当前版本的 keras tensorflow 调用 Predict on batch 时收到警告 错误 W tensorflow core data root d
  • ValidateRequest = False 但实际上它仍然是 True 并且忽略了它?

    我想禁用请求验证特别是在ASP NET MVC 2 0 RTM 所以我添加了一些必要的查看页面指令部分 如下所示 but 请求验证不是残疾人 我还添加了请求验证属性控制器中的相关操作如下 System Web Mvc ValidateInp
  • ASP.NET MVC 中的 RouteLink 和 ActionLink 有什么区别?

    我认为标题已经概括了这一点 有什么区别RouteLink and ActionLink 在 ASP NET MVC 中 即你什么时候使用Html RouteLink 你什么时候使用Html ActionLink 在你看来 操作和路由不必具有
  • 如何诊断护照真伪?

    由于该函数没有返回值 我如何确定导致该函数失败的原因 我有以下代码 function test req IncomingMessage res ServerResponse next err any gt void passport aut
  • 在jquery中创建循环背景动画

    我想要的是 当页面加载时 背景颜色为红色 10 秒后 bgColor 变为绿色 并带有淡入淡出动画 再过 10 秒后 它会变成橙色 然后又变成红色 依此类推 有人可以帮忙吗 Use 设置间隔 http www w3schools com j
  • Firestore - 如何在数据库中自动减少整数值?

    Firestore 最近推出了一项新功能 可以自动递增和递减整数值 我可以使用增加整数值 例如 FieldValue increment 50 但如何减量呢 我尝试使用FieldValue decrement 50 但FieldValue中
  • 在 C++ 中旋转图像而不使用 OpenCV 函数

    描述 我正在尝试在不使用 C 中的 OpenCV 函数的情况下旋转图像 旋转中心不必是图像的中心 它可能是不同的点 偏离图像中心 到目前为止 我遵循各种来源进行图像插值 我知道source https stackoverflow com a