Filter2D のソース コードを探しましたが、見つかりませんでした。Visual c++ もできませんでした。ここにfilter2Dアルゴリズムの専門家はいますか? どのように機能するかはわかっていますが、実際にどのように機能するかはわかりません。独自の filter2d() 関数を作成してテストしましたが、結果は opencvs 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}
};
考えている人はいますか?
編集: 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 などの他のフィルターは惨めに失敗します。
実際のアルゴリズムに近づいていますが、まだそこにはありません。他にアイデアのある人はいますか?