逐像素旋转和翻转图像
Rotate and Flip an image pixel by pixel
我有下图,我想旋转和翻转它,使其适合全屏分辨率。我正在进行以下转换,以旋转,但它不起作用。
这是源图像。
size_t bpp = Ogre::PixelUtil::getNumElemBytes(source.getFormat());
const unsigned char *srcData = source.getData();
unsigned char *dstData = new unsigned char[width * height * bpp];
size_t srcPitch = source.getRowSpan();
size_t dstPitch = width * bpp;
ImageDescriptor sourceImage(source.getWidth(), source.getHeight(), bpp);
ImageDescriptor rotatedTarget(source.getHeight(), source.getWidth(), bpp); // note width/height swap
unsigned char *rotated = new unsigned char[source.getHeight() * source.getWidth() * bpp];
for (std::size_t row = 0; row < rotatedTarget.mHeight; ++row) {
for (std::size_t col = 0; col < rotatedTarget.mWidth; ++col) {
for (std::size_t chan = 0; chan < rotatedTarget.mChannels; ++chan) {
rotated[rotatedTarget.offset(col, row, chan)] =
srcData[sourceImage.offset(row, col, chan)];
}
}
}
struct ImageDescriptor {
std::size_t mWidth;
std::size_t mHeight;
std::size_t mChannels;
ImageDescriptor(std::size_t width, std::size_t height, std::size_t channels)
{
mWidth = width;
mHeight = height;
mChannels = channels;
}
std::size_t stride() const { return mWidth * mChannels; }
const std::size_t offset(std::size_t row, std::size_t col, std::size_t chan) {
assert(0 <= row && row < mHeight);
assert(0 <= col && col < mWidth);
assert(0 <= chan && chan < mChannels);
// return row*stride() + col*mChannels + chan;
// or, depending on your coordinate system ...
return (mHeight - row - 1)*stride() + col*mChannels + chan;
}
std::size_t size() const { return mHeight * stride(); }
};
这是结果图像。
答案结果。
有什么想法吗?
很简单,反转行,更改
rotated[rotatedTarget.offset(col, row, chan)] =
srcData[sourceImage.offset(row, col, chan)];
自
rotated[rotatedTarget.offset(rotatedTarget.mWidth-col-1, row, chan)] =
srcData[sourceImage.offset(row, col, chan)];
反正就是这样。我对你的代码有点困惑。
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 平均图像时图像损坏
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 翻转 ppm 图像C++麻烦
- 如何使用 Pthreads 并行化图像翻转?
- 逐像素旋转和翻转图像
- 我正在尝试使用 Windows 映像组件加载 32 位图像,将其翻转,然后将其再次保存回同一文件中
- 如何翻转图像并同时用枪指着鼠标?(SFML)
- stb_image在加载 opengl 纹理的图像时不会垂直翻转
- 如何在QT中翻转图像
- 翻转图像水平C
- 在 C 或 C++ 中翻转灰度图像
- 我应该垂直翻转加载了stb_image的图像的线条以在OpenGL中使用吗
- 确保给定的图像被另一个图像翻转
- 如何在打开的 cv c++ 中翻转带有角度矩形或任何其他角度形状的图像的特定部分
- 垂直翻转图像
- 翻转图像以获得镜像效果