天天看点

OpenCV遍历访问图像的每一个元素

// my8.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include "highgui.h"

#include "cv.h"

#include <iostream>

using namespace std;

int _tmain(int argc, _tchar* argv[])

{

 iplimage* image = cvloadimage("e:\\wali2.jpg");

 cvnamedwindow("s");

 //用指针指向图像的数据区头部

 uchar* pchar;

 int width = image->width; cout<<width<<endl;

 int heigh = image->height; cout<<heigh<<endl;

 int channel = image->nchannels;  cout<<channel<<endl;

 int widthstep = image->widthstep;  cout<<widthstep<<endl;

 int i,j;

 for(i=0; i<heigh; i++)

 {

  pchar = (uchar*) image->imagedata +  i*widthstep;

        for (j=0; j<width; j++)

        {

   uchar* temp = pchar +j*channel;

   temp[0] -= 10; //通道b

      temp[1] -= 10; //通道g

   temp[2] -= 10; //通道r

        }

 }

    cvshowimage("s",image);

 cvwaitkey(0);

 cvreleaseimage(&image);

 cvdestroywindow("s");

 return 0;

}