[英]How to modify pixel data of cv::Mat in openCV?
I basically tried some ways of modifying the pixel data from the questions already existing here. 我基本上尝试了一些根据此处已经存在的问题修改像素数据的方法。 However, nothing is just working.
但是,没有什么是有效的。
I am working on iOS OpenCV2.framework. 我正在使用iOS OpenCV2.framework。
I've implemented the following method, but its not modifying the outputImage. 我实现了以下方法,但未修改outputImage。
UIImage *inputUIImage = [UIImage imageName:@"someImage.png"];
UIImage *outputUIImage = nil;
cv::Mat inputImage = <Getting this from a method that converts UIImage to cv::Mat: Working properly>
cv::Mat outputImage;
inputImage.copyTo(outputImage);
//processing...
for(int i=0; i < outputImage.rows; i++)
{
for (int j=0; j<outputImage.cols; j++)
{
Vec4b bgrColor = outputImage.at<Vec4b>(i,j);
//converting the 1st channel <assuming the sequence to be BGRA>
// to a very small value of blue i.e. 1 (out of 255)
bgrColor.val[0] = (uchar)1.0f;
}
}
outputUIImage = <Converting cv::Mat to UIImage via a local method : Working properly>
self.imageView1.image = inputUIImage;
self.imageView2.image = outputUIImage;
//here both images are same in colour. no changes.
Can anyone let me know what am I missing ? 谁能让我知道我在想什么?
The problem is that you are copying the reference that outputImage.at<Vec4b>(i,j)
returns into a local variable bgrColor and modify that one. 问题是,您
outputImage.at<Vec4b>(i,j)
返回的引用复制到局部变量bgrColor中,并对其进行修改。 So nothing in outputImage actually gets modified. 因此outputImage中的任何内容实际上都没有被修改。 What you want to do is to directly modify the reference returned by
Mat::at
. 您要做的是直接修改
Mat::at
返回的引用。
Solutions: 解决方案:
Vec4b& bgrColor = outputImage.at<Vec4b>(i,j);
bgrColor.val[0] = (uchar)1.0f;
or 要么
outputImage.at<Vec4b>(i,j)[0] = (uchar)1.0f;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.