• OpenCV基础(二)---图像像素操作


    图像像素操作

    通过访问图像的像素,对灰度图像和RGB图像进行反差.

    例如,在灰度图像中,某一个像素值为,pixel_value. 那么反差后的pixel_value = 255 - pixel_value.

    对RGB图像处理类似,差别在于要对每一个颜色通道进行反差.

    方法1:

     1 #include <opencv2/opencv.hpp>
     2 #include <iostream>
     3 
     4 using namespace cv;
     5 using namespace std;
     6 
     7 const char* input_title = "input image";
     8 const char* output_title = "output image";
     9 
    10 int main() {
    11 
    12     /*加载并显示图像*/
    13     Mat src;
    14     src = imread("D:/lena.png");
    15     if (src.empty()) {  //图像如果没有加载,结束程序
    16         cout << "could not found image...";
    17         return -1;
    18     }
    19     namedWindow(input_title, CV_WINDOW_AUTOSIZE); //创建窗口
    20     imshow(input_title, src); //显示原始图像
    21 
    22     
    23     Mat dst;
    24     int channels = src.channels();  //获取原图像通道数
    25     int height = src.rows;            //获取原图像高度
    26     int width = src.cols;            //获取原图像宽度
    27     dst.create(src.size(), src.type());  //依照原图像,创建目的图像
    28     
    29     /*访问图像像素,并实现反差图像*/
    30     for (int row = 0; row < height; row++)
    31         for (int col = 0; col < width; col++) {
    32             if (channels == 1)
    33             {
    34                 int gray = src.at<uchar>(row, col);  //获取通道的像素值
    35                 dst.at<uchar>(row, col) = 255 - gray; //像素反差
    36             }
    37             else if (channels == 3)
    38             {
    39                 int b = src.at<Vec3b>(row, col)[0];  //获取blue通道像素值
    40                 int g = src.at<Vec3b>(row, col)[1];     //获取green通道像素值
    41                 int r = src.at<Vec3b>(row, col)[2];     //获取red通道像素值
    42                 dst.at<Vec3b>(row,col)[0] = 255 - b;  
    43                 dst.at<Vec3b>(row, col)[1] = 255 - g;  //像素反差
    44                 dst.at<Vec3b>(row, col)[2] = 255 - r;  
    45             }
    46         }
    47 
    48     /*显示结果*/
    49     namedWindow(output_title, CV_WINDOW_AUTOSIZE);
    50     imshow(output_title, dst);
    51 
    52     waitKey(0);
    53     return 0;
    54 }

    方法2:

    调用函数 bitwise_not()

    函数功能:计算两个数组或数组与标量之间的每个元素的绝对差.

    函数原型:void bitwise_not(InputArray src, OutputArray dst,

         InputArray mask = noArray());

    这个函数和前面的代码功能是一样的,

    代码演示如下.

    #include <opencv2/opencv.hpp>
    #include <iostream>
    
    using namespace cv;
    using namespace std;
    
    const char* input_title = "input image";
    const char* output_title = "output image";
    
    int main() {
    
        /*加载并显示图像*/
        Mat src;
        src = imread("D:/lena.png");
        if (src.empty()) {  //图像如果没有加载,结束程序
            cout << "could not found image...";
            return -1;
        }
        namedWindow(input_title, CV_WINDOW_AUTOSIZE); //创建窗口
        imshow(input_title, src); //显示原始图像
    
        /*图像反差*/
        Mat dst;
        bitwise_not(src, dst);
    
        /*显示结果*/
        namedWindow(output_title, CV_WINDOW_AUTOSIZE);
        imshow(output_title, dst);
    
        waitKey(0);
        return 0;
    }

    效果图:

  • 相关阅读:
    3.5缺少动态连接库.so--cannot open shared object file: No such file or directory
    PHP中的变量详解
    Python类的实例属性详解
    Docker中的镜像分层技术详解
    如何编写优雅的代码?
    Node.js在不同平台的安装方法步骤详解
    Python3.X新特性之print和exec
    Django中如何配置Database缓存?
    Ajax中eval的使用详解
    如何创建和使用XMLHttpRequest对象?
  • 原文地址:https://www.cnblogs.com/zmm1996/p/10661294.html
Copyright © 2020-2023  润新知