opencv提取彩色图像所有像素点的RGB值方法实现

最近需要一个项目是对图像识别,学习了一下opencv。对opencv了一个简单理解:
代码只能读取已知图像大小的像素RGB值,例如375*500,其他大小可将CvPoint pt2 = cvPoint(375,a)和for(a=0;a<=500;a++)数字替换。

#include "stdafx.h"
#include 
#include 
#include 
void help() {
printf("\nRead out RGB pixel values and store them to disk\nCall:\n"
"\n This will store to files blines.csv, glines.csv and rlines.csv\n\n");
}
int main( int argc, char** argv  )
{
    IplImage *img = cvLoadImage( argv[1] );
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    cvShowImage( "Example1", img );
    cvWaitKey(0);
    if(!img){printf("\nCouldn't open %s\n",argv[1]); help(); return -1;}
    int a;
    int max_buffer;
    int r[10000],g[10000],b[10000];
    FILE *fptrb = fopen("blines.csv","w"); //Store the data here
    FILE *fptrg = fopen("glines.csv","w"); // for each color channel
    FILE *fptrr = fopen("rlines.csv","w");
    CvLineIterator iterator;
    for(a=0;a<=500;a++)
    {
        CvPoint pt1 = cvPoint(1,a);
        CvPoint pt2 = cvPoint(375,a);
        max_buffer = cvInitLineIterator(img,pt1,pt2,&iterator,8,0);
        for(int j=0; jfprintf(fptrb,"%d,", iterator.ptr[0]); //Write blue value
            fprintf(fptrg,"%d,", iterator.ptr[1]); //green
            fprintf(fptrr,"%d,", iterator.ptr[2]); //red
            iterator.ptr[2] = 0;  //Mark this sample in red
            CV_NEXT_LINE_POINT(iterator); //Step to the next pixel
        }
        //OUTPUT THE DATA IN ROWS:
        fprintf(fptrb,"\n");fprintf(fptrg,"\n");fprintf(fptrr,"\n");
    }
    printf("\nData stored to files: blines.csv, glines.csv and rlines.csv\n\n");
    fclose(fptrb); fclose(fptrg); fclose(fptrr);
    cvShowImage( "Example2", img );
    cvWaitKey(0);
}

还望大佬们能给我指正更多的好办法

你可能感兴趣的:(opencv学习之路,图像处理)