gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:38:19 24 4
gpt4 key购买 nike

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

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

如何将更改应用到我的缓冲区?

最佳答案

我在尝试重现您的问题时重构了您的代码,并在此过程中修复了它。您将源代码克隆到 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);
}

关于c++ - 将图像缓冲区的指针传递给 OpenCV 并更改饱和度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16340249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com