手动访问/构建OpenCV Mat数据

时间:2014-04-12 06:49:43

标签: c++ c opencv

我是OpenCV的新手,我创建了一个用于学习目的的简单程序,这里是代码:

cv::Mat image;

std::string imageName = "test.jpg";
image = cv::imread(imageName, cv::IMREAD_COLOR); // Read the file

if(! image.data ) // Check for invalid input
{
    std::cout << "Could not open or find the image" << std::endl ;
    return false;
}


uchar* mData = image.data;//input buffer data
uchar* nData = (uchar*)malloc(image.rows * image.cols * image.channels() * sizeof(uchar));//output buffer data

std::cout << image.rows << " " << image.cols << " " << image.channels() << " " << sizeof(uchar) << std::endl;
std::cout << image.step << std::endl;

//int r,g,b;
for(int i = 0;i < image.rows ;i++)
{
    for(int j = 0;j < image.step ;j++)
    {
        nData[image.step * i + j ] = mData[image.step * i + j ] / 3;
        nData[image.step * i + j + 1] = mData[image.step * i + j + 1] / 3;
        nData[image.step * i + j + 2] = mData[image.step * i + j + 2] / 3;
    }
}

cv::Mat outputImage(image.rows, image.cols, CV_8UC3, (void*)nData);
if(! outputImage.data ) // Check
{
    std::cout << "output mat failure" << std::endl ;
    return false;
}

cv::namedWindow( "Input image window", cv::WINDOW_AUTOSIZE ); // Create a window for display.
cv::imshow( "Input image window", image ); // Show our image inside it.

cv::namedWindow( "Output image window", cv::WINDOW_AUTOSIZE ); // Create a window for display.
cv::imshow( "Output image window", outputImage ); // Show our image inside it.
cv::waitKey(0); // Wait for a keystroke in the window

free(nData);

1-这样可以安全地访问输入Mat数据吗? isContinuous()需要什么?

2-这是进行这种手动复制的正确方法吗?

3- free(nData)我是否必须手动处理自己的记忆?

4-我将使用GPU,因此我需要确保输入指针&#34;是安全的&#34;所以我可以使用memcpy而不用担心

0 个答案:

没有答案