c++ - C++ 方法中的图像旋转

标签 c++ image image-processing image-rotation

我之前在 Stack Overflow 上发帖询问如何在 C++ 程序中精确地旋转 BMP 图像。然而,现在我有更多的进步要展示。

我想知道我的程序如何(或为什么)在我进行图像计算后不输出图像:

void BMPImage::Rotate45Left(float point1, float point2, float point3)
{
  float radians = (2 * 3.1416*45) / 360;

  float cosine = (float)cos(radians);
  float sine = (float)sin(radians);

  float point1Xtreme = 0;
  float point1Yearly = 0;
  float point2Xtreme = 0;
  float point2Yearly = 0;
  float point3Xtreme = 0;
  float point3Yearly = 0;

int SourceBitmapHeight = m_BIH.biHeight;
int SourceBitmapWidth = m_BIH.biWidth;

point1Xtreme = (-m_BIH.biHeight*sine);
point1Yearly = (m_BIH.biHeight*cosine);
point2Xtreme = (m_BIH.biWidth*cosine - m_BIH.biHeight*sine);
point2Yearly = (m_BIH.biHeight*cosine + m_BIH.biWidth*sine);
point3Xtreme = (m_BIH.biWidth*cosine);
point3Yearly = (m_BIH.biWidth*sine);

float Minx = min(0, min(point1Xtreme, min(point2Xtreme, point3Xtreme)));
float Miny = min(0, min(point1Yearly, min(point2Yearly, point3Yearly)));
float Maxx = max(point1Xtreme, max(point2Xtreme, point3Xtreme));
float Maxy = max(point1Yearly, max(point2Yearly, point3Yearly));

int FinalBitmapWidth = (int)ceil(fabs(Maxx) - Minx);
int FinalBitmapHeight = (int)ceil(fabs(Maxy) - Miny);
FinalBitmapHeight = m_BIH.biHeight;
FinalBitmapWidth = m_BIH.biWidth;
int finalBitmap;

如果有人有任何有用的指示,那就太好了。 我应该提一下:

  • 我不能为了这个程序的目的使用其他外部库
  • 这是一个带有菜单系统的小型图像处理程序

最佳答案

图像变换通常是通过将目标像素投影到源像素然后计算该目标像素的值来完成的。这样您就可以轻松地合并不同的插值方法。

template <typename T>
struct Image {
    Image(T* data, size_t rows, size_t cols) : 
        data_(data), rows_(rows), cols_(cols) {}
    T* data_;
    size_t rows_;
    size_t cols_;
    T& operator()(size_t row, size_t col) {
        return data_[col + row * cols_];
    }
 };

template <typename T>
T clamp(T value, T lower_bound, T upper_bound) {
    value = std::min(std::max(value, lower_bound), upper_bound);
}

void rotate_image(Image const &src, Image &dst, float ang) {
    // Affine transformation matrix 
    // H = [a, b, c]
    //     [d, e, f]

    // Remember, we are transforming from destination to source, 
    // thus the negated angle. 
    float H[] = {cos(-ang), -sin(-ang), dst.cols_/2 - src.cols_*cos(-ang)/2,
                 sin(-ang),  cos(-ang), dst.rows_/2 - src.rows_*cos(-ang)/2}; 


    for (size_t row = 0; row < dst.rows_; ++row) {
       for (size_t col = 0; col < dst.cols_; ++cols) {
           int src_col = round(H[0] * col + H[1] * row + H[2]);
           src_col = clamp(src_col, 0, src.cols_ - 1);
           int src_row = round(H[3] * col + H[4] * row + H[5]);
           src_row = clamp(src_row, 0, src.rows_ - 1);               

           dst(row, col) = src(src_row, src_col);
       }
    }
}

上述方法以任意角度旋转图像并使用最近邻插值。我是直接输入到stackoverflow里面的,所以满是bug;尽管如此,概念还是存在的。

关于c++ - C++ 方法中的图像旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22022484/

相关文章:

c++ - unordered_map<TYPE, bool> 与 set<TYPE>

jquery - div的图像叠加

jquery - 使用 html/css 将文本放在每个可点击图像的中心

python - TypeError:labels不是numpy数组,也不是标量

二值图像的泛洪填充C++算法

c++ - 这看起来与旧问题相似但不同。给定一个大小为 n 的数组(允许重复的数字),找到缺失的 2 个数字

c++ - 向 Java 开发人员的 Eclipse Oxygen IDE 添加 C/C++ 语言

c++ - 调用 `source`命令时如何传递参数

python - 边缘检测后从各个方向裁剪图像

Python打开jp2医学图像——Scipy、glymur