1. 程式人生 > >opencv影象畫素值讀取

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型

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

複製程式碼
#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型,有些不太爽。

相關推薦

opencv影象讀取

說到影象畫素,肯定要先認識一下影象中的座標系長什麼樣。 1. 座標體系中的零點座標為圖片的左上角,X軸為影象矩形的上面那條水平線;Y軸為影象矩形左邊的那條垂直線。該座標體系在諸如結構體Mat,Rect,

opencv讀取影象讀取並儲存到txt檔案(二)灰度圖

#include "stdafx.h" #include"cv.h" #include <stdlib.h> #include <stdio.h> #include <math.h> #include <fstream> #include &l

opencv讀取影象讀取並儲存到txt檔案(一)RGB

#include “stdafx.h” #include"cv.h" #include <stdlib.h> #include <stdio.h> #include <math.h> #include #include #include “iost

OpenCV讀取影象

OpenCV中用於讀取影象畫素點的值的方法很多,這裡主要提供了兩種常用的方法。 方法一 利用IplImage資料型別的imageData定位資料緩衝區來實現,imageData包含指向影象第一個畫素資料的指標 例: If( imgSource != 0 )//imgSource為IplImage* { fo

opencv對於bmp影象讀取和賦

#include <stdio.h> #include <cv.h> #include <highgui.h> void main(){     IplImage *img=cvLoadImage("c://fruitfs.bmp",1

影象讀取和賦

//取IplImage影象畫素值 int main() {     IplImage* src = cvLoad("filename",0); //-1預設讀取原通道,0 灰度圖,1彩色圖     if(src!=0)  &nb

opencv 通過指標訪問影象,輸出為空的問題

for (int i = 0; i < img_roi_gray_at.rows; ++i) { uchar* datatemp = img_roi_gray_at.ptr<uchar>(i);

opencv影象讀取與顯示

讀取畫素  單通道 :  ycbr.at<uchar>(i,j) 三通道:ycbr.at<Vec3b>(i,j) 四通道:ycbr.at<Vec4b>(i,j) 返回的應該是個 <> 中引數的量,Vec3b。 無法直接顯示

關於opencv的Mat型別的imwrite()函式寫入.jpg影象會改變的解決方案

 #include <string> #include <opencv2/opencv.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace std; using na

OpenCV】訪問影象並修改--IplImage

1.IplImage的結構: typedef struct _IplImage { int nSize; /* IplImage大小 */

OpenCV 獲取的幾個方法

http://tmjfzy.blog.163.com/blog/static/6644702520126157403724/ Fn 1 : Code 1 : int main() { //新建一個uchar型別的單通道矩陣(grayscale image

OpenCV影象操作及效率分析

        學習OpenCV也幾個月了,雖然對OpenCV有些瞭解,但是感覺基礎還是沒打實,在這在介紹一下OpenCV的畫素操作,以及OpenCV讀取影象的格式和讀取影象的效率分析。當然文章也有很多沒有介紹到的地方,希望大家多多指教,相互交流。         在計

opencv影象操作方法

影象容器Mat Mat和Matlab裡的陣列格式有點像,但一般是二維向量,如果是灰度圖,一般存放<uchar>型別;如果是RGB彩色圖,存放<Vec3b>型別。 單通道灰度圖資料存放格式: 多通道的影象中,每列並列存放通道數量的子列,如RGB三通

OpenCV--讀取影象中任意點的,並顯示座標

需求:在滑鼠左鍵按下時,顯示該位置的畫素值和座標。 在vs2010下新建了Win32控制檯專案,在此只處理灰度影象,顯示的畫素值為灰度值 #include "iostream"

OpenCV影象操作

二值化影象畫素不是0就是255,資料型別為uchar。所以訪問方法是: // 這裡inputmat是二值化影象的mat inputmat.at<uchar>(y, x); 判斷是否為白色的方法: if (inputmat.at<uchar&g

opencv修改影象

本節知識點 1,讀寫影象 a,imread可以載入灰度圖或者RGB影象 b,imwrite儲存影象,型別由副檔名決定 2,讀寫影象的畫素 a,讀取灰度影象畫素點的值(CV_8UC1) Scalar i

opencv讀取彩色/灰度圖片並存儲在本地檔案中c++程式碼例項及執行結果

c++程式碼彩色圖片#include<opencv2/opencv.hpp> #include<fstream> using namespace std; using namespace cv; int main(int argc, char* ar

Opencv獲取影象上的(Ubuntu版)

前言:當需要製作自己的訓練集的時候,標註圖片上的檢測目標,是一件比較耗時費力的事情。下面記錄的就是其中一種方法。 1.程式碼: import argparse import cv2 refPt=[] cropping=False def click_and_crop(e

OpenCV影象中的每個

import cv2import numpy as pydef salt(img): IMAGE_FILE = img img = cv2.imread(IMAGE_FILE) print(img.shape[1]) for i in

Opencv獲取並改變影象

#include <iostream> using namespace std; #include "cv.h" #include "highgui.h" #pragma  comment(lib,"cxcore.lib") #pragma  comment(l