将图像缓冲区的指针传递给OpenCV并更改饱和度?

时间:2013-05-02 14:07:38

标签: c++ opencv

我想传递图像缓冲区的指针,更改饱和度并立即查看结果。但是这个改变并没有在我的缓冲区中应用,也没有改变。

void changeSaturation(void* buffer,int width, int height)
{
  Mat matObject(width, height, CV_8UC4, buffer);
  m_matSource = matObject;
  Mat newMat = m_matSource.clone();

  // BGR to HSV
  cvtColor(matSource, matSource, CV_BGR2HSV);
  for(int i = 0; i < newMat.rows; ++i)
   {
     for(int j = 0; j < newMat.cols; ++j)
     {
        newMat.at<cv::Vec3b>(i, j)[1] = 255; //saturationValue;
     }
   }

// HSV back to BGR
 cvtColor(newMat, m_matSource, CV_HSV2BGR); // here m_matSource->data change

}

如何在缓冲区上应用更改?

2 个答案:

答案 0 :(得分:1)

我在尝试重现您的问题时重构了您的代码,并在此过程中我修复了它。您将源克隆到newMat然后更改了原始图像的颜色空间,然后继续完全忽略新修改的图像。试试这个:

void changeSaturation(Mat& image)
{
    Mat result(image.rows, image.cols, image.type());

    // BGR to HSV
    cvtColor(image, result, CV_BGR2HSV);
    for(int i = 0; i < result.rows; ++i)
    {
        for(int j = 0; j < result.cols; ++j)
            result.at<cv::Vec3b>(i, j)[1] = 255; //saturationValue;
    }

// HSV back to BGR
    cvtColor(result, result, CV_HSV2BGR); // here m_matSource->data change

    namedWindow("Original");
    imshow("Original",image);

    namedWindow("Duplicate");
    imshow("Duplicate",result);
}


int main()
{
    Mat image;

    image = imread("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg");

    changeSaturation(image);
    waitKey(0);
}

修改

修改输入图像:

void changeSaturation(Mat& image)
{
    // BGR to HSV
    cvtColor(image, image, CV_BGR2HSV);
    for(int i = 0; i < image.rows; ++i)
    {
        for(int j = 0; j < image.cols; ++j)
            image.at<cv::Vec3b>(i, j)[1] = 255; //saturationValue;
    }

    // HSV back to BGR
    cvtColor(image, image, CV_HSV2BGR); // here m_matSource->data change
}

下次修改

现在有(几乎)原始功能签名:

void changeSaturation(uchar* buffer, int rows, int cols, int type)
{
    Mat image(rows, cols, type, buffer);
    Mat result;
    // BGR to HSV
    cvtColor(image, result, CV_BGR2HSV);
    for(int i = 0; i < result.rows; ++i)
    {
        for(int j = 0; j < result.cols; ++j)
            result.at<cv::Vec3b>(i, j)[1] = 255;
    }

    // HSV back to BGR
    cvtColor(result, image, CV_HSV2BGR);
}

int main()
{
    Mat image;

    image = imread("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg");

    changeSaturation(image.data, image.rows, image.cols, image.type());

    imshow("Original",image);
    waitKey(0);
}

答案 1 :(得分:0)

您的构造函数Mat matObject(width, height, CV_8UC4, buffer);matObject指向的位置分配width大小heightbuffer

在您的函数中,您正在对newMat进行更改,该matObject是从buffer克隆的。但是,newMat并未指向matObject,它指向matObject并且{{1}}未被您的函数更改。