使用纯c++的方式实现图片平移算法

#include <iostream>
#include <vector>

using namespace std;

// 代表图像中的像素,这里假设每个像素有三个通道(RGB)
struct Pixel {
    unsigned char r, g, b;
};

// 图像平移函数
void translateImage(std::vector<Pixel>& srcPixels, std::vector<Pixel>& dstPixels, int width, int height, int dx, int dy) {
    dstPixels.resize(srcPixels.size());  // 确保目标像素数组大小与源像素数组相同

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int srcIndex = y * width + x;
            int dstX = x + dx;
            int dstY = y + dy;

            if (dstX >= 0 && dstX < width && dstY >= 0 && dstY < height) {
                int dstIndex = dstY * width + dstX;
                dstPixels[dstIndex] = srcPixels[srcIndex];  // 对图像的每个像素进行平移
            }
        }
    }
}

int main() {
    int width = 800;
    int height = 600;
    int dx = 50;  // x轴平移距离
    int dy = 50;  // y轴平移距离

    // 创建源图像像素数组,这里用一个示例颜色填充
    std::vector<Pixel> srcPixels(width * height, {255, 0, 0});  // 红色

    std::vector<Pixel> translatedPixels;
    translateImage(srcPixels, translatedPixels, width, height, dx, dy);  // 调用图像平移函数

    // 在这里,你可以将translatedPixels转换回图像或进行其他处理

    return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值