簡體   English   中英

C ++:openCV錯誤

[英]C++:openCV error

大家好,我正在嘗試使用C ++ openCV 2.4.5獲取圖像中像素的RGB

但是我編譯時遇到這個錯誤。

它會加載圖像,但是當我嘗試獲取像素的RGB時會出現異常

有人可以幫我嗎?

在此處輸入圖片說明

以下代碼加載圖像並找到索引25,4處像素的RGB
我的代碼是:

 #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv/cv.h>
    #include <opencv/highgui.h>
    #include <iostream>
    using namespace std;
    using namespace cv;
    int main()
    {
        int x;
        Mat input = imread("C:/red.jpg");
        if (input.empty())
            {
                std::cout << "!!! Failed imread(): image not found" << std::endl;
                cin>>x;
                return 0;
                // don't let the execution continue, else imshow() will crash.
            }
        imshow("input", input);
         Vec3f pixel = input.at<Vec3f>(25, 40);
         if( !pixel[0])
         {
             std::cout << "!!! Failed pixel(): image not found" << std::endl;
                cin>>x;
                return 0;
         }
            int b = pixel[0];
            int g = pixel[1];
            int r = pixel[2];
        cout<<b;
        cout <<" ";
        cout<<r;
        cout <<" ";
        cout <<g;
        cin>>b;

        /*




        // detect squares after filtering...
        */
        return 0;
    }

您的圖片可能是CV_8UC3類型,這意味着像素值存儲為3通道8位uchar。

Vec3f pixel = input.at<Vec3f>(25, 40);

您正在嘗試以浮點形式訪問像素值,因為在OpenCV中, Vec3f被定義為typedef Vec<float, 3> Vec3f; 這會使您的程序崩潰。 相反,它應該是:

Vec3b pixel = input.at<Vec3b>(25, 40);

在OpenCV中, Vec3b被定義為typedef Vec<uchar, 3> Vec3b; ,這就是您想要的。

這是cv :: Vec數據類型的文檔

編輯您可以簡單地使用輸出像素數據

cout << pixel << endl;

或像這樣:

printf("[%u, %u, %u]", pixel[0], pixel[1], pixel[2]);

或像這樣:

int b = static_cast<int>(pixel[0]);
int g = static_cast<int>(pixel[1]);
int r = static_cast<int>(pixel[2]);
cout<< "[" << b <<", " << g << ", " << r << "]" << endl;

您應該使用:

Mat input = imread("C://red.jpg");

代替 :

Mat input = imread("C:/red.jpg");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM