opencv第4讲--遍历图像中的每一个像素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
void QuickDemo::pixel_visit_demo(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<uchar>(row, col);   //获得像素数值
 
    //          image.at<uchar>(row, col) = 255 - pv;  //重新赋值
    //      }
    //      else             //彩色图像
    //      {
    //          Vec3b bgr = image.at<Vec3b>(row, col);
    //          image.at<Vec3b>(row, col)[0] = 255 - bgr[0];
    //          image.at<Vec3b>(row, col)[1] = 255 - bgr[1];
    //          image.at<Vec3b>(row, col)[2] = 255 - bgr[2];
    //      }
    //  }
    //}
 
    for (int row = 0; row < h; row++)
    {
        uchar* current_row = image.ptr<uchar>(row);    //获取当前行的指针
        for (int col = 0; col < w; col++)
        {
            if (dims == 1)   //灰度图像,一个通道
            {
                int pv = *current_row;   //获得像素数值
 
                *current_row++ = 255 - pv;  //重新赋值
            }
            else             //彩色图像, 三个通道
            {
                *current_row++ = 255 - *current_row;
                *current_row++ = 255 - *current_row;
                *current_row++ = 255 - *current_row;
            }
        }
    }
 
 
 
    imshow("像素读写演示", image);
}

  

posted on   xcxfury001  阅读(102)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示