2017-06-01 60 views
0

我在Android Studio中有兩個Mat圖像,分別是BGRA(8UC4)。我希望能夠通過逐個檢查每個像素來從兩個圖像中提取BGRA像素值,然後在RGB值中找到它們的絕對差值。我不想減去透明度,所以我不能使用Core.absdiff()。是否有捷徑可尋?我已經使用mat.get()和mat.put()實現了這個功能,但速度非常慢。如何訪問兩個圖像中的每個像素並使用OpenCV查找它們的絕對差異? (Android Studio)

我見過的解決方案貼在下面,但我真的不知道它是如何工作或我如何可以改變它與我的圖片/所需功能的工作:

Mat A ; 
A.convertTo(A,cvType.CV_16SC3); 
int size = (int) (A.total() * A.channels()); 
short[] temp = new short[size]; 
A.get(0, 0, temp); 
for (int i = 0; i < size; i++) 
    temp[i] = (short) (temp[i]/2); 
C.put(0, 0, temp); 

大部分我所讀到這涉及將Mat數據放入Java Primitive類型中。由於我對Java和OpenCV都很陌生,我不確定這是什麼意思?

謝謝

回答

1

這是一個C++實現。

Mat aBGRA[4];  // array of Mats to hold Blue Green Red Alpha channels 
cv::split(A, aBGRA); // split Mat A into channels 
Mat bBGRA[4]; 
cv::split(B, bBGRA); // split Mat B into channels 
Mat cBGR[3];  // Mat array to hold absolute diff of A and B BGR channels 
for (int idx = 0; idx < 3; ++idx) // loop BGR channels 
{ 
    cBGR[idx] = Mat::zeros(A.rows, A.cols, CV_8U); 
    Mat diff(aBGRA[idx] != bBGRA[idx]); // create mask where A & B differ 
    vector<Point> nonZero; 
    cv::findNonZero(diff, nonZero); // collect list of points where A & B differ 
    // for each different point in this channel 
    for (auto itr = nonZero.begin(); itr != nonZero.end(); ++itr) 
    { 
     Point p(*itr); 
     // set cBGR at point to the absolute difference between A and B at this point 
     cBGR[idx].at<uint8_T>(p) = abs(aBGRA[idx].at<uint8_t>(p) - bBGRA[idx].at<uint8_t>(p)); 
    } 
} 
Mat C; 
cv::merge(cBGR, 3, C); // merge BGR channels into C 
+0

我的問題是特定於Java,因爲我使用Android Studio。你介意解釋你的代碼嗎? – ohmygrabovski

+0

對不起,我不知道Java。我添加了註釋來解釋代碼。我認爲Java OpenCV具有與C++ OpenCV相同的功能。 –

相關問題