簡體   English   中英

c ++ OpenCV如何將過濾器應用於Mat的子矩形?

[英]c++ OpenCV How to apply filter to sub rect of Mat?

這個問題很相似,但從未得到過回答: OpenCV過濾圖像的一部分

我正在使用opencv2和c ++。 我有一個Mat,比如300x200,我想模糊矩形區域左上角= 50,50大小= 100,50。 我一直在瀏覽opencv.org上的示例和文檔,但我無法確定如何過濾,或僅在Mat的子矩形上執行其他操作。

有問題的代碼如下,其中surf是SDL_Surface, rect是SDL_Rect(int x,y,w,h)。 從表面創建Mat src_mat的線很好,因為它在其他地方運行良好。 這會編譯,但會出現以下錯誤。

{ // Extra scoping used for the surface_lock.
    using namespace cv;
    surface_lock surf_lock(surf);

    //int rows, int cols, int type, void* data, size_t step=AUTO_STEP
    Mat src_mat = Mat(surf->h, surf->w, CV_8UC4, src->pixels, Mat::AUTO_STEP);
    Mat cropmat(src_mat, Rect(rect.y, rect.y + rect.h, rect.x, rect.x + rect.w));

    blur(crop_mat, crop_mat, Size((depth + 1), (depth + 1)), Point(-1,-1));
}

錯誤:

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file /build/opencv/src/opencv-2.4.6.1/modules/core/src/matrix.cpp, line 323
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/opencv/src/opencv-2.4.6.1/modules/core/src/matrix.cpp:323: error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function Mat

subrect也是Mat。

Mat larger; 
Mat roi(larger, Rect(50,50,100,50));

// apply whatever algo on 'roi'
blur( roi,roi, cv::Size(5,5) );

暫無
暫無

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

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