我沒有找到Filter2D的源代碼,但找不到它。 Visual C++也不是。 這裏有filter2D算法的專家嗎?我知道how it's supposed to work,但不知道它是如何工作的。我做了我自己的filter2d()函數來測試,結果與opencvs filter2D()有很大的不同。這裏是我的代碼:Opencv - filter2D()方法如何實際工作?
Mat myfilter2d(Mat input, Mat filter){
Mat dst = input.clone();
cout << " filter data successfully found. Rows:" << filter.rows << " cols:" << filter.cols << " channels:" << filter.channels() << "\n";
cout << " input data successfully found. Rows:" << input.rows << " cols:" << input.cols << " channels:" << input.channels() << "\n";
for (int i = 0-(filter.rows/2);i<input.rows-(filter.rows/2);i++){
for (int j = 0-(filter.cols/2);j<input.cols-(filter.cols/2);j++){ //adding k and l to i and j will make up the difference and allow us to process the whole image
float filtertotal = 0;
for (int k = 0; k < filter.rows;k++){
for (int l = 0; l < filter.rows;l++){
if(i+k >= 0 && i+k < input.rows && j+l >= 0 && j+l < input.cols){ //don't try to process pixels off the endge of the map
float a = input.at<uchar>(i+k,j+l);
float b = filter.at<float>(k,l);
float product = a * b;
filtertotal += product;
}
}
}
//filter all proccessed for this pixel, write it to dst
st.at<uchar>(i+(filter.rows/2),j+(filter.cols/2)) = filtertotal;
}
}
return dst;
}
有人看到我的執行有什麼問題嗎? (除了作爲慢)
這裏是我的執行:
cvtColor(src,src_grey,CV_BGR2GRAY);
Mat dst = myfilter2d(src_grey,filter);
imshow("myfilter2d",dst);
filter2D(src_grey,dst2,-1,filter);
imshow("filter2d",dst2);
這裏是我的內核:
float megapixelarray[basesize][basesize] = {
{1,1,-1,1,1},
{1,1,-1,1,1},
{1,1,1,1,1},
{1,1,-1,1,1},
{1,1,-1,1,1}
};
而且here are the (substantially different) results:
思想,任何人嗎?
編輯:感謝Brians答案我添加了這個代碼:
//normalize the kernel so its sum = 1
Scalar mysum = sum(dst);
dst = dst/mysum[0]; //make sure its not 0
dst = dst * -1; //show negetive
和filter2d更好地工作。某些過濾器提供了完全匹配,以及其他過濾器,如Sobel,fail miserably.
我正在接近實際的算法,但還沒有。任何人有任何想法?
+1好問的問題!你展示了你的實現,上下文以及結果。 –
我最近一直在玩相同的概念,比較opencv實現與自定義的實現。看來,至少在內核總和爲0的情況下,filter2d可能仍然會對其進行縮放,以使max(abs(kernel))爲1. – Bovaz