OpenCV中获取图像某一像素值

This is a basic example for the OpenCV.

First we must know the structure of IplImage:

  • IPL image:
    IplImage
      |-- int  nChannels;     // Number of color channels (1,2,3,4)
      |-- int  depth;         // Pixel depth in bits: 
      |                       //   IPL_DEPTH_8U, IPL_DEPTH_8S, 
      |                       //   IPL_DEPTH_16U,IPL_DEPTH_16S, 
      |                       //   IPL_DEPTH_32S,IPL_DEPTH_32F, 
      |                       //   IPL_DEPTH_64F
      |-- int  width;         // image width in pixels
      |-- int  height;        // image height in pixels
      |-- char* imageData;    // pointer to aligned image data
      |                       // Note that color images are stored in BGR order
      |-- int  dataOrder;     // 0 - interleaved color channels, 
      |                       // 1 - separate color channels
      |                       // cvCreateImage can only create interleaved images
      |-- int  origin;        // 0 - top-left origin,
      |                       // 1 - bottom-left origin (Windows bitmaps style)
      |-- int  widthStep;     // size of aligned image row in bytes
      |-- int  imageSize;     // image data size in bytes = height*widthStep
      |-- struct _IplROI *roi;// image ROI. when not NULL specifies image
      |                       // region  to be processed.
      |-- char *imageDataOrigin; // pointer to the unaligned origin of image data
      |                          // (needed for correct image deallocation)
      |
      |-- int  align;         // Alignment of image rows: 4 or 8 byte alignment
      |                       // OpenCV ignores this and uses widthStep instead
      |-- char colorModel[4]; // Color model - ignored by OpenCV
    //------------------------------------------------------------------------------
    int  main( int  argc,  char *  argv[])


        IplImage *img=cvLoadImage("c://fruitfs.bmp",1);
        CvScalar s;
        for(int i=0;i<img->height;i++){
            for(int j=0;j<img->width;j++){
            s=cvGet2D(img,i,j); // get the (i,j) pixel value
            printf("B=%f, G=%f, R=%f ",s.val[0],s.val[1],s.val[2]);
            s.val[0]=111;
            s.val[1]=111;
            s.val[2]=111;
            cvSet2D(img,i,j,s);//set the (i,j) pixel value
            }

        }

        cvNamedWindow("Image",1); 

        cvShowImage("Image",img);
        cvWaitKey(0); //等待按键 

        cvDestroyWindow( "Image" );//销毁窗口
        cvReleaseImage( &img ); //释放图像 

        return 0;
    }
     

     

     其实还有更好的方法,例如将其封装成类,调用起来更加方便,效率也很高。
由于文件大小超过csdn限制的70M. Delphi-OpenCV OpenCV version - 2.4.13 Development environment - Delphi 2010-10.1 Contributors: Laentir Valetov (email: laex@bk.ru) Mikhail Grigorev (email: sleuthhound@gmail.com) Requirements: Visual C++ redistributable for Visual Studio 2013 Files: msvcp120.dll, msvcr120.dll, msvcp120d.dll, msvcr120d.dll (1) 32-bit in the "Delphi-OpenCV\redist\VC2013x86\" (2) 64-bit in the "Delphi-OpenCV\redist\VC2013x64\" Shared library FFMPEG 3.2 for Windows can be downloaded from here (3) FFmpeg 32-bit Shared (4) FFmpeg 64-bit Shared Dynamic library OpenCV need to download here Files: _2413.dll and _2413d.dll After installing OpenCV: (5) 32-bit in the C:\OpenCV\build\x86\vc12\bin\ (6) 64-bit in the C:\OpenCV\build\x64\vc12\bin\ Some examples (FFMPEG) required SDL 2.0 and SDL 1.2 (7) SDL.dll and SDL2.dll Copy files OS Windows 64-bit Target platform 64-bit: (2),(4),(6) -> "C:\Windows\System32\" Target platform 32-bit: (1),(3),(5),(7) -> "C:\Windows\SysWOW64\" OS Windows 32-bit Target platform 32-bit: (1),(3),(5),(7) -> "C:\Windows\System32\" How to install: Download the archive. Unzip it to a convenient directory, thus get the following directory structure <PROJECT_ROOT> - Directory, for example, "C:\Delphi\OpenCV\" <bin> <redist> <resource> <samples> <source> Add the search path for the modules of the project in Delphi IDE (Tools-Options-Delphi Options-Library-Library path) <PROJECT_ROOT>\source <PROJECT_ROOT>\source\utils <PROJECT_ROOT>\source\component <PROJECT_ROOT>\source\sdl <PROJECT_ROOT>\source\opengl <PROJECT_ROOT>\source\ffmpeg <PROJECT_ROOT>\resource\facedetectxml where <PROJECT_ROOT> directory, which was unzipped project. To install the components, open and install <PROJECT_ROOT>\source\component\DelphiXX\OpenCVXXX.dpk <PROJECT_ROOT>\source\component\DelphiXX\dclCommonOpenCVXXX.dpk <PROJECT_ROOT>\source\component\DelphiXX\dclVCLOpenCVXXX.dpk <PROJECT_ROOT>\source\component\DelphiXX\dclFMXOpenCVXXX.dpk Open in Delphi IDE and compile: Examples of the use of certain functions and procedures <PROJECT_ROOT>\samples\LibDemo\LibDemo.groupproj Examples of the use of video processing algorithms <PROJECT_ROOT>\samples\MultiDemo\MultiDemo.groupproj Examples of the use of video processing algorithms using VCL.Forms <PROJECT_ROOT>\samples\VCLDemo\VCLDemo.groupproj Examples of using FFMPEG library header files are in the <PROJECT_ROOT>\samples\FFMpeg\FFMPEG.groupproj Examples of use of components <PROJECT_ROOT>\samples\Components\ComponentsDemo.groupproj Donate (PayPal USD) Donate (PayPal EUR) Donate (PayPal RUB) Yandex Money: 410012802258318
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值