2013-12-23 913 views
0

我基本上尝试了一些方法来修改这里已经存在的问题的像素数据。然而,没有什么只是工作。如何在openCV中修改cv :: Mat的像素数据?

我正在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. 

任何人都可以让我知道我错过了什么吗?

回答

0

问题是,您正在复制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; 
+0

但在这里,即使你写了相同的代码,我做了第一个1 ..和它不工作。然而,我会尝试第二个。 – CodenameLambda1

+0

对不起。错字。现在修复 – kamjagin

+0

嗯我看..现在它的一个参考。 – CodenameLambda1