镜像

本文介绍了一个简单的C++程序,该程序使用OpenCV库实现图像的水平镜像效果。通过对原始图像进行像素复制并按相反顺序放置到新图像中,实现了图像的水平翻转。此程序适用于初学者了解基本的图像操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#if 1
#include <iostream>
#include <cv.h>
#include <highgui.h>
#include <imgproc/imgproc.hpp>
#include <iostream>
#include <math.h>


using namespace std;
using namespace cv;
#define  PI 3.1415926



int main(int argc, const char * argv[])
{

    //1:load source image
    Mat image = imread("/Users/hanoi/Desktop/copy.jpeg");
    if(image.empty())
    {
        printf("it can not load image\n");
        return 0;
    }

    int height = image.rows;
    int width = image.cols;


    Mat destImage;
    destImage.create(height, 2*width, image.type());
    memset(destImage.data, 0, 2*height*width*3);

    for (int j=0; j<height; j++)
    {
        for(int i=0; i<width; i++)
        {
            *(destImage.data + j*destImage.step + 3*i) = *(image.data + j*image.step + 3*i);
            *(destImage.data + j*destImage.step + 3*i + 1) = *(image.data + j*image.step + 3*i + 1);
            *(destImage.data + j*destImage.step + 3*i + 2) = *(image.data + j*image.step + 3*i +2);
        }
    }

    for (int j=0; j<height; j++)
    {
        for(int i=0; i<width; i++)
        {
            if(i == 0)
            {
                *(destImage.data + j*destImage.step + 3*(i+width)) = *(image.data + j*image.step + 3*(width-1));
                *(destImage.data + j*destImage.step + 3*(i+width) + 1) = *(image.data + j*image.step + 3*(width-1) + 1);
                *(destImage.data + j*destImage.step + 3*(i+width) + 2) = *(image.data + j*image.step + 3*(width-1) + 2);
            }
            else
            {
                *(destImage.data + j*destImage.step + 3*(i+width)) = *(image.data + j*image.step + 3*(width-i));
                *(destImage.data + j*destImage.step + 3*(i+width) + 1) = *(image.data + j*image.step + 3*(width-i) + 1);
                *(destImage.data + j*destImage.step + 3*(i+width) + 2) = *(image.data + j*image.step + 3*(width-i) + 2);
            }

        }
    }

    imwrite("/Users/hanoi/Desktop/镜像.bmp", destImage);
    return 0;
}
#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值