在OpenCV中绘制梯度向量场

5
我想计算灰度图像(代码中的smoothed_plane)的梯度,并在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);

编辑:

这是我输入/输出结果的一对框架,符合要求。

在此输入图片描述

我尝试打印一些值,在某些点上我得到了非常高或非常低的值。 再次感谢。

2个回答

5
我解决了我的问题。主要错误在于对偏导矩阵 grad_x 和 grad_y 访问方法的使用。如此处所述,方法 at.<>() 返回一个 Scalar 对象,因此要访问像素强度值,应该使用 .val[] 字段。以下是代码需要更改的方式:
Scharr(smoothed_plane,grad_x,ddepth,1,0,scale);
Scharr(smoothed_plane,grad_y,ddepth,0,1,scale);

for (int i = 0 ; i < image_height ; i ++){
    for (int j = 0 ; j < image_width ; j ++){
        Scalar xval = grad_x.at<float>(i,j);
        Scalar yval = grad_y.at<float>(i,j);
        gradient_field.at<Point2f>(i,j) = Point2f(xval.val[0],yval.val[0]);
    }
}

这是预期的结果:在此输入图片描述


你是如何缩小箭头的?我正在尝试你的初始代码,但我的箭头比上面图片中展示的要大得多。 - ttsesm
是的,我缩小了箭头的大小,只是为了可视化。原始箭头对我来说也显得更大。我之前没有说是因为这对我的问题来说是不必要的。基本上,我将每个箭头的结束点设置为cv::Point2f p2(p + gf_ptr[j]*0.1),其中p是起始点。 - Marco Ferro
好的,我明白了,感谢您的反馈。gf_ptrPoint2f* gf_ptr = gradient_field.ptr<Point2f>(i); 对吗? - ttsesm

0

在尝试直接使用您的代码后,以下方法对我有效

Scharr(smoothed_plane,grad_x,ddepth,1,0,scale);
Scharr(smoothed_plane,grad_y,ddepth,0,1,scale);

for (int i = 0 ; i < image_height ; i ++){
    for (int j = 0 ; j < image_width ; j ++){
        Scalar xval = grad_x.at<uchar>(i,j); // Notice <uchar> not float
        Scalar yval = grad_y.at<uchar>(i,j);
        gradient_field.at<Point2f>(i,j) = Point2f(xval.val[0],yval.val[0]);
    }
}

<float>建议给了我高且无法使用的值。


是的,我认为这取决于您希望为Scharr算子中的目标矩阵(ddepth)设置多少深度。在我的情况下,我将其更改为CV_32F,因此使用<float>适用于我。我想对于CV_8U(CV_16S也是如此?)应该可以使用<uchar>。不管怎样,谢谢 :) - Marco Ferro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接