我想计算灰度图像的梯度(平滑平面在代码中)并将其绘制为 OpenCV 中的矢量场,叠加到现有图像上。
我尝试应用一对 Sobel 运算符(我也尝试过 Scharr)来计算沿 x 和 y 的两个导数,如 OpenCV 文档中所述,但当我尝试绘图时,矢量场似乎完全错误。我想了解我的错误是什么。
我在这里放了一些代码以便更清楚。在此先感谢您的帮助。
//img is a gray-scale image
Mat abs_grad_x, abs_grad_y, grad;
Mat g_img;
int ddepth = CV_16S;
int scale = 1;
int delta = 0;
cvtColor(img,g_img,CV_GRAY2BGR);
smoothed_plane = Mat::zeros(image_height,image_width,CV_8UC1);
gradient_field = Mat::zeros(image_height,image_width,CV_32FC2);
// Smooth the dominant plane by convolution with a Gaussian
GaussianBlur(dominant_plane,smoothed_plane,Size(51,51),image_height*image_width*0.5);
/// Morphological opening (remove small objects from the foreground)
erode(smoothed_plane, smoothed_plane, getStructuringElement(MORPH_ELLIPSE, Size(40+1,40+1)));
dilate(smoothed_plane, smoothed_plane, getStructuringElement(MORPH_ELLIPSE, Size(40, 40)));
/// Morphological closing (fill small holes in the foreground)
dilate(smoothed_plane, smoothed_plane, getStructuringElement(MORPH_ELLIPSE, Size(40, 40)));
erode(smoothed_plane, smoothed_plane, getStructuringElement(MORPH_ELLIPSE, Size(40, 40)));
imshow("Eroded plane",smoothed_plane);
/// Gradient X
Scharr( smoothed_plane, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_x, abs_grad_x );
/// Gradient Y
Scharr( smoothed_plane, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_y, abs_grad_y );
for (int i = 0 ; i < image_height ; i ++){
for (int j = 0 ; j < image_width ; j ++){
gradient_field.at<Point2f>(Point2f(j,i)) = Point2f(abs_grad_x.at<float>(Point2f(j,i)),abs_grad_y.at<float>(Point2f(j,i)));
}
}
for (int i = 0 ; i < image_height ; i += flowResolution){
for (int j = 0 ; j < image_width ; j+= flowResolution){
Point2f p(j,i);
Point2f p2(gradient_field.at<Point2f>(p)+p);
arrowedLine(g_img,p,p2,Scalar(0,0,255),1.5,8,0,0.1);
}
}//*/
imshow("Gradient Vector Field", g_img);
EDIT:
这是我的输入/输出结果的一对帧,根据需要
我尝试打印出一些值,在某些点上我得到了非常高或非常低的值。
再次感谢