美文网首页
《OpenCV》二、使用指针的方式,像素点颜色取反

《OpenCV》二、使用指针的方式,像素点颜色取反

作者: GoodTekken | 来源:发表于2021-10-10 22:28 被阅读0次

代码如下:

#include <opencv2/opencv.hpp> 
#include <iostream> 
#include <direct.h>
using namespace cv;
using namespace std;

int main()
{
    Mat src = cv::imread("bin\\图片库\\Test1.jpg");//原图的分辨率是Rows:756,Cols:1008;
    Mat dst;
    src.copyTo(dst);

        // 获取图像宽、高
    int channels = src.channels();
    int rows = src.rows;
    int cols = src.cols * channels;
    if (src.isContinuous()) {
        cols *= rows;
        rows = 1;
    }

    // 每个像素点的每个通道255取反
    uchar* p1;
    uchar* p2;
    for (int row = 0; row < rows; row++) {
        p1 = src.ptr<uchar>(row);// 获取像素指针
        p2 = dst.ptr<uchar>(row);
        for (int col = 0; col < cols; col++) {
            *p2 = 255 - *p1; // 取反
            p2++;
            p1++;
        }
    }

    imshow("src", src);
    imshow("dst", dst);

    //int a = dst.type();
    waitKey(0);
    return 0;
}

相关文章

网友评论

      本文标题:《OpenCV》二、使用指针的方式,像素点颜色取反

      本文链接:https://www.haomeiwen.com/subject/tipqoltx.html