1. 程式人生 > >opencv——操作影象中每一個畫素

opencv——操作影象中每一個畫素

以下均針對opencv1.0
第一種方法:使用cvGet2D及cvSet2D

對於單通道影象:

    IplImage* img = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 1);
    for (int i = 0; i < img->height; i++)//height對應影象的行
    {
        for (int j = 0; j < img->width; j++)//width對應影象的列
        {
            CvScalar s;
            //s = cvGet2D(img, i, j);// get the (i,j) pixel value
s.val[0] = 255; cvSet2D(img, i, j, s);// set the (i,j) pixel value } }

對於多通道影象:

    IplImage* img = cvCreateImage(cvSize(640, 480), IPL_DEPTH_32F, 3);
    for (int i = 0; i < img->height; i++)//height對應影象的行
    {
        for (int j = 0; j < img->width; j++)//width對應影象的列
{ CvScalar s; //s = cvGet2D(img, i, j);// get the (i,j) pixel value s.val[0] = 0;//B s.val[1] = 100;//G s.val[2] = 50;//R cvSet2D(img, i, j, s);// set the (i,j) pixel value } }

第二種方法:直接訪問
對於單通道影象:

    IplImage* img = cvCreateImage(cvSize(640
, 480), IPL_DEPTH_8U, 1); for (int i = 0; i < img->height; i++)//height對應影象的行 { for (int j = 0; j < img->width; j++)//width對應影象的列 { ((uchar*)(img->imageData + i*img->widthStep))[j] = 255;//img->imagedata是指向影象資料第一個畫素的指標,widthStep是指每一行的畫素所佔的位元組數 } }

對於多通道影象:
多通道位元組型:

    IplImage* img = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 3);
    for (int i = 0; i < img->height; i++)//height對應影象的行
    {
        for (int j = 0; j < img->width; j++)//width對應影象的列
        {
            ((uchar*)(img->imageData + i*img->widthStep))[j*img->nChannels + 0] = 0;//img->imagedata是指向影象資料第一個畫素的指標,widthStep是指每一行的畫素所佔的位元組數
            ((uchar*)(img->imageData + i*img->widthStep))[j*img->nChannels + 1] = 100;
            ((uchar*)(img->imageData + i*img->widthStep))[j*img->nChannels + 2] = 50;
        }
    }

多通道浮點型:

    IplImage* img = cvCreateImage(cvSize(640, 480), IPL_DEPTH_32F, 3);
    for (int i = 0; i < img->height; i++)//height對應影象的行
    {
        for (int j = 0; j < img->width; j++)//width對應影象的列
        {
            ((float*)(img->imageData + i*img->widthStep))[j*img->nChannels + 0] = 0;//img->imagedata是指向影象資料第一個畫素的指標,widthStep是指每一行的畫素所佔的位元組數
            ((float*)(img->imageData + i*img->widthStep))[j*img->nChannels + 1] = 100;
            ((float*)(img->imageData + i*img->widthStep))[j*img->nChannels + 2] = 50;
        }
    }

第三種方法:指標訪問
對於單通道影象:

    IplImage* img = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 1);
    for (int i = 0; i < img->height; i++)//height對應影象的行
    {
        for (int j = 0; j < img->width; j++)//width對應影象的列
        {
            int step = img->widthStep / sizeof(uchar);
            uchar*data = (uchar *)img->imageData;
            data[i*step + j] = 255;
        }
    }

對於多通道位元組型影象:

    IplImage* img = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 3);
    for (int i = 0; i < img->height; i++)//height對應影象的行
    {
        for (int j = 0; j < img->width; j++)//width對應影象的列
        {
            for (int k = 0; k < img->nChannels; k++)//width對應影象的列
            {
                int step = img->widthStep / sizeof(uchar);
                int channels = img->nChannels;
                uchar*data = (uchar *)img->imageData;
                data[i*step + j*channels+k] = 255;
            }
        }
    }

對於多通道浮點型:

    IplImage* img = cvCreateImage(cvSize(640, 480), IPL_DEPTH_32F, 3);
    for (int i = 0; i < img->height; i++)//height對應影象的行
    {
        for (int j = 0; j < img->width; j++)//width對應影象的列
        {
            for (int k = 0; k < img->nChannels; k++)//width對應影象的列
            {
                int step = img->widthStep / sizeof(float);
                int channels = img->nChannels;
                float*data = (float *)img->imageData;
                data[i*step + j*channels+k] = 255;
            }
        }
    }