簡體   English   中英

如何訪問3D CV :: Mat的索引

[英]How to access indexes of a 3D CV::Mat

嘗試訪問3D CV :: Mat的索引時遇到分段錯誤。 代碼如下,

int channel = 3;
int sizes[] = { imageheight, imageWidth};
CV::Mat test(2, sizes, CV_8UC3)
for(int i=0;i<image2D->size();i++)
    {
        Point2D &_point = image2D->at(i);       

        test.at<unsigned char>(_point.y,_point.x,0) = _point.rgb.r;
        test.at<unsigned char>(_point.y,_point.x,1) = _point.rgb.g;
        test.at<unsigned char>(_point.y,_point.x,2) = _point.rgb.b; // Segmentation fault in this line
    }

以下方法不會崩潰,但會輸出黑色圖像。 我不確定我是否做得正確,

unsigned char *ptest = test.ptr<unsigned char>(_point.y);
        ptest[channel*_point.x+ 0] = _point.rgb.r;
        ptest[channel*_point.x+ 1] = _point.rgb.g;
        ptest[channel*_point.x+ 2] = _point.rgb.b;

編輯:

將代碼更新為以下代碼,使我可以為數組下標編譯錯誤無效類型'unsigned char [int]'

Matrix test(imageheight, imageWidth, CV_8UC3);
for(int i=0;i<image2D->size();i++)
    {
        Point2D &_point = image2D->at(i);
        // Compile error on the below 3 lines.
        test.at<unsigned char>(_point.y, _point.x)[0] = _point.rgb.b;
        test.at<unsigned char>(_point.y, _point.x)[1] = _point.rgb.g;
        test.at<unsigned char>(_point.y, _point.x)[2] = _point.rgb.r;

    }

編譯錯誤在我使用[]訪問通道索引的位置。 我想這不是訪問頻道的正確方法。

通過多個渠道訪問cv :: Mat的最簡單方法,

cv::Mat3b test(imageheight, imageWidth, CV_8UC3);
for(int i=0;i<image2D->size();i++)
{
    Point2D &_point = image2D->at(i);
    test.at<cv::Vec3b>(_point.y, _point.x)[0] = _point.rgb.b;
    test.at<cv::Vec3b>(_point.y, _point.x)[1] = _point.rgb.g;
    test.at<cv::Vec3b>(_point.y, _point.x)[2] = _point.rgb.r;
}

對於單通道cv :: Mat

cv::Mat test(imageheight, imageWidth, CV_32FC1);

for(int i=0;i<image2D->size();i++)
{
    Point2D &_point = image2D->at(i);
    test.at<float>(_point.y, _point.x) = _point.r;
}

感謝這個答案

暫無
暫無

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

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