2013-12-23 60 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