opencv矩陣掩模
阿新 • • 發佈:2018-12-09
掩模公式: I(i,j)=5*I(i,j)-[I(i-1,j)+I(i+1,j)+I(i,j-1)+I(i,j+1)]
Mat src, dst; src = imread("3.jpg"); imshow("Normal",src); dst = Mat::zeros(src.size(), src.type()); int cols = (src.cols - 1)*src.channels(); int rows = src.rows; int offsetx = src.channels(); //src.ptr<uchar>(row)獲取第row行的指標,下標從0開始; //saturate_cast<ucahr>(n) 限制n為0-255 for (int row = 1; row < rows-1; row++) { uchar* previous = src.ptr<uchar>(row-1); uchar* current = src.ptr<uchar>(row); uchar* next = src.ptr<uchar>(row + 1); uchar* output = dst.ptr<uchar>(row); for (int col=offsetx; col<cols; col++) { output[col] = saturate_cast<uchar>(5 * current[col] - (previous[col] + next[col] + current[col - 1] + current[col + 1])); } } imshow("End", dst);
opencv本身提供掩模函式:
Mat src, dst;
src = imread("3.jpg");
imshow("Normal",src);
Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
int x = src.depth();
filter2D(src, dst, src.depth(), kernel);
imshow("End", dst);