• Opencv_4_图像像素的读写操作


    1)opencv.hpp 头文件:

    #pragma once
    #include <opencv.hpp>
    using namespace std;
    #include
    using namespace cv;
    using namespace std;
    class ColorInvert{
    public :
        void pixel_visit(Mat& image);
    };

    2)opencv.cpp 文件:

    void ColorInvert::pixel_visit(Mat& image)
    {
        int w = image.cols;
        int h = image.rows;
        int dims = image.channels();
        /*
        for (int row = 0; row < h; row++)
        {
         for (int col = 0; col < w; col++)
            {
             if (dims == 1) {
                 int pv = image.at(row, col);
                 image.at(row, col) = 255 - pv;
            }
             if (dims == 3)
             {
                 Vec3b bgr = image.at(row, col);
                 image.at(row, col)[0] = 255 - bgr[0];
                 image.at(row, col)[1] = 255 - bgr[1];
                 image.at(row, col)[2] = 255 - bgr[2];

             }
            }
        }
        */

        for (int row = 0; row < h; row++)
        {
            uchar* current_row = image.ptr(row);
            for (int col = 0; col < w; col++)
            {
                if (dims == 1) {
                    int pv = *current_row;
                    *current_row++ = 255 - pv;
                }
                if (dims == 3)
                {
                    *current_row++ = 255 - *current_row;
                    *current_row++ = 255 - *current_row;
                    *current_row++ = 255 - *current_row;
                }
            }
        }
        imshow("像素读写演示", image);
    }

    3)main 函数:

    #include
    #include
    #include "ColorInvert.h"
    using namespace std;
    using namespace cv;

    int main()
    {
        Mat src = imread("E:/2024/Test/opencv/test2.png");
        if (src.empty()) {
            cout << "load pic fail" << endl;
            return -1;
        }
        namedWindow("输入窗口", WINDOW_FREERATIO);
        imshow("输入窗口", src);
        ColorInvert coInvert;
        coInvert.pixel_visit(src);
        //coInvert.colorSpaceInvert(src);

        waitKey(0);
        destroyAllWindows();
    }

    显示结果如下:

  • 相关阅读:
    C++-关键字:auto
    Hive集合函数 collect_set 和 collect_list 使用示例
    阿里云服务器如何关闭防火墙?阿里云安全组怎么设置端口?
    jzoj1212 重建道路
    Go语言:基础练习--删除数组指定元素
    卷积导向快速傅里叶变换(FFT/NTT)教程
    Docker启动失败报错Failed to start Docker Application Container Engine解决方案
    某马旅游网站开发(对servlet的优化)
    动物主题网页设计(学生期末作业必看)
    架构师日记-从技术角度揭露电商大促备战的奥秘
  • 原文地址:https://blog.csdn.net/EveryDayOneHour/article/details/138075040