[英]How to modify pixel data of cv::Mat in openCV?
我基本上尝试了一些根据此处已经存在的问题修改像素数据的方法。 但是,没有什么是有效的。
我正在使用iOS OpenCV2.framework。
我实现了以下方法,但未修改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.
谁能让我知道我在想什么?
问题是,您outputImage.at<Vec4b>(i,j)
返回的引用复制到局部变量bgrColor中,并对其进行修改。 因此outputImage中的任何内容实际上都没有被修改。 您要做的是直接修改Mat::at
返回的引用。
解决方案:
Vec4b& bgrColor = outputImage.at<Vec4b>(i,j);
bgrColor.val[0] = (uchar)1.0f;
要么
outputImage.at<Vec4b>(i,j)[0] = (uchar)1.0f;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.