簡體   English   中英

如何在指向圖像的指針上應用 cv::Rect?

[英]How do I apply cv::Rect on a pointer to image?

我正在嘗試顯示圖像的一半(從指向圖像的指針):

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

void main() {

    // Load image
    std::string path = "Resources/test.png";
    cv::Mat img = cv::imread(path);
    
    // Pointer to image
    cv::Mat* img_p = new cv::Mat(img.size(), CV_8UC1);
    *img_p = img;

    // Find size of rows and columns
    int nRows = img_p->rows;
    int nCols = img_p->cols;

    // Display left half of image
    cv::imshow("left image", *img_p(cv::Rect(0, 0, nCols / 2, nRows)));
    
    cv::waitKey(5000);
    delete img_p;
}

我在這一行得到一個錯誤:

cv::imshow("left image", *img_p(cv::Rect(0, 0, nCols / 2, nRows)));

錯誤是:

expression preceding parantheses of apparent call must have (pointer-to-) function type

如何從指針訪問圖像的一半? 編輯我想從指針訪問圖像的一半以獲得更好的性能。

您不需要使用 new/delete 或指針來執行您要執行的操作。

void main() {

    // Load image
    std::string path = "Resources/test.png";
    cv::Mat img = cv::imread(path);
    
    cv::Mat subImg = img(cv::Rect(0, 0, img.cols / 2, img.rows));

    // Display left half of image
    cv::imshow("left image", subImg);
    
    cv::waitKey(5000);
}

所有cv::Mat副本共享相同的數據/緩沖區,除非您明確使用cv::Mat::clone()

暫無
暫無

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

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