opencv遍历图片元素

#include "cv.h"
#include "highgui.h"

int main()
{
    unsigned char *buffer=NULL ;
    IplImage *tmpImg = cvLoadImage("night.png");
    
    int width = tmpImg->width;
    int height = tmpImg->height;
    int widthStep = tmpImg->widthStep;
    int nChannels = tmpImg->nChannels;
    printf("%d
",tmpImg->nChannels);
    buffer = (unsigned char *)malloc(sizeof(char )*width*height*nChannels);
    if( buffer==NULL )
        return 1;
    int count=0;
    for( int i=0; i<height; ++i )
    {
        for( int j=0; j<width; ++j )
        {
            
            for( int k=0; k<nChannels; ++k )
            {
                buffer[count++] = tmpImg->imageData[i*tmpImg->widthStep+j*nChannels+k];
            }

        }
    }
    IplImage *src = cvCreateImageHeader(cvSize(width,height),IPL_DEPTH_8U,nChannels);
    cvSetData(src,buffer,width*nChannels);//*sizeof(IPL_DEPTH_8U)
    cvShowImage("src",src);
    cvWaitKey(0);
    return 0;
}
原文地址:https://www.cnblogs.com/jesse-deng/p/3467009.html