• opencv图像像素值读取


    说到图像像素,肯定要先认识一下图像中的坐标系长什么样。

     1. 坐标体系中的零点坐标为图片的左上角,X轴为图像矩形的上面那条水平线;Y轴为图像矩形左边的那条垂直线。该坐标体系在诸如结构体Mat,Rect,Point中都是适用的。(OpenCV中有些数据结构的坐标原点是在图片的左下角,可以设置的)。

     2. 在使用image.at<TP>(x1, x2)来访问图像中点的值的时候,x1并不是图片中对应点的x轴坐标,而是图片中对应点的y坐标(也就是编程中的pic.rows那行)。x2同理。

     3. 如果所画图像是多通道的,比如说image图像的通道数时n,则使用Mat::at(x, y)时,其x的范围依旧是0到image的height,而y的取值范围则是0到image的width乘以n,因为这个时候是有n个通道,所以每个像素需要占有n列。但是如果在同样的情况下,使用Mat::at(point)来访问的话,则这时候可以不用考虑通道的个数,因为你要赋值给获取Mat::at(point)的值时,都不是一个数字,而是一个对应的n维向量。

     4. 多通道图像在使用minMaxLoc()函数时不能给出其最大最小值坐标的,因为每个像素点其实有多个坐标,所以是不会给出的。因此在编程时,这2个位置应该给NULL。

      5 多通道的图像可以直接赋值,不必每个通道赋值。但是要注意其类型是Vec3b,如果写成uchar,最后的copy图像只会显示源图像的1/3

        char *tempPath="0.jpg";
        Mat src=imread(tempPath);
        Mat copy=Mat::zeros(src.rows,src.cols,src.type());
        for (int nrows=0;nrows<src.rows;nrows++)
        {
            for (int ncols=0;ncols<src.cols;ncols++)
            {
                copy.at<Vec3b>(nrows,ncols)=src.at<Vec3b>(nrows,ncols);
    
            }
        }

     

        Mat src=imread("image/color.jpg");
        imshow("a",src);
        int i,j;
        int cPointR,cPointG,cPointB,cPoint;//currentPoint;
        for(i=1;i<src.rows;i++)
            for(j=1;j<src.cols;j++)
            {
                cPointB=src.at<Vec3b>(i,j)[0];
                cPointG=src.at<Vec3b>(i,j)[1];
                cPointR=src.at<Vec3b>(i,j)[2];
                if(cPointB>100&cPointR<100&cPointG<100)
                    {
                        src.at<Vec3b>(i,j)[0]=0;  //单通道是uchar,没有[0][1][2]
                        src.at<Vec3b>(i,j)[1]=0;
                        src.at<Vec3b>(i,j)[2]=0;
                }
                
            }
        imshow("da",src);

     注意每个点的像素灰度值cPointR,cPointG,cPointB,cPoint的数据类型是int,在单通道图内由于读取像素灰度值的代码是img.at<uchar>(nrows,ncols),所以特别容易把数据类型记成unsigned char型

     ============================================================

    【转】VC++读取图像RGB值(未试验)

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <windows.h>
    #include <gdiplus.h>
    #pragma comment(lib, "gdiplus.lib")
    
    using namespace std;
    using namespace Gdiplus;
    
    int main() 
    {
        GdiplusStartupInput gdiplusstartupinput;
        ULONG_PTR gdiplustoken;
        GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);
    
        wstring infilename(L"1.jpg");
        string outfilename("color.txt");
    
        Bitmap* bmp = new Bitmap(infilename.c_str());
        UINT height = bmp->GetHeight();
        UINT width  = bmp->GetWidth();
        cout << "width " << width << ", height " << height << endl;
    
        Color color;
        ofstream fout(outfilename.c_str());
    
        for (UINT y = 0; y < height; y++)
        for (UINT x = 0; x < width ; x++)
            {
                bmp->GetPixel(x, y, &color);
                fout << x << "," << y << ";"
                     << (int)color.GetRed()   << ","
                     << (int)color.GetGreen() << ","
                     << (int)color.GetBlue()  << endl;
        }
    
        fout.close();
    
        delete bmp;
        GdiplusShutdown(gdiplustoken);
        return 0;
    }

    关于数据的储存:(转)

    Mat_<uchar>对应的是CV_8U,Mat_<char>对应的是CV_8S,Mat_<int>对应的是CV_32S,Mat_<float>对应的是CV_32F,Mat_<double>对应的是CV_64F,对应的数据深度如下:

    • CV_8U - 8-bit unsigned integers ( 0..255 )

    • CV_8S - 8-bit signed integers ( -128..127 )

    • CV_16U - 16-bit unsigned integers ( 0..65535 )

    • CV_16S - 16-bit signed integers ( -32768..32767 )

    • CV_32S - 32-bit signed integers ( -2147483648..2147483647 )

    • CV_32F - 32-bit floating-point numbers ( -FLT_MAX..FLT_MAX, INF, NAN )

    • CV_64F - 64-bit floating-point numbers ( -DBL_MAX..DBL_MAX, INF, NAN )

    这里还需要注意一个问题,很多OpenCV的函数支持的数据深度只有8位和32位的,所以要少使用CV_64F,但是vs的编译器又会把float数据自动变成double型,有些不太爽。

  • 相关阅读:
    c#+oracle存储过程实现分页
    C#中调用Matlab程序
    Oracle 自定义TYPE 的几种用法(转)
    oracle嵌套表示例
    矩阵的秩及矩阵的广义逆
    矩阵的定义及其运算规则
    矩阵微分
    matlab中取模(mod)与取余(rem)的区别
    hog源码分析
    矩阵的转置、求逆及分块
  • 原文地址:https://www.cnblogs.com/Daringoo/p/4483864.html
Copyright © 2020-2023  润新知