Opencv 2.4.10
在下面代码的末尾,将使用矩阵Img2上的9个宽磁盘结构元素调用一个扩展。最初,Img2是由一个简单的头副本(Img2=Img1)从Img1创建的。注意,Img1是在没有通过Range从Img0复制数据的情况下生成的,因此Img1没有第一行和最后3行的Img0。扩张的结果不正确。
但是,如果我通过克隆( Img2=Img1.clone() )为Img2=Img1.clone使用了完整的副本,则扩展是正确的。
注意,不管我使用的是哪种复制方法,在Img2上使用imwrite (没有在下面的代码中显示)是相同的。那么,形态学算子的作用也不应该是一样的吗?
Mat Tmp;
Mat Img1=Img0(Range(3-1, Img0.rows - 3+1),Range::all());
Img1(Range(0,1), Range::all()) = 0;
Img1(Range(Img1.rows-1,Img1.rows), Range::all()) = 0;
// bad
//Mat Img2 = Img1; // header only copy: the dilation results are wrong on the top and bottom
// good
Mat Img2 = Img1.clone(); // full copy, dilation works right.
Mat Disk4;
// exact replacement for mmatlab strel('disk',4,0), somewhat difference than opencv's ellipse structuring element.
MakeFilledEllipse( 4, 4, Disk4);
// If I use Img2 from clone, this is the same as matlab's.
// If I just do a header copy some areas the top and bottom are different
dilate(Img2, Tmp,Disk4, Point(-1,-1),1,BORDER_CONSTANT, Scalar(0));编辑--我随后简化了代码,使Img2替换了img1,并且没有img1,这样我就可以使用1级的Mat头间接重复这个问题,并且仍然以同样的方式失败(不正确)。
Mat Tmp;
Mat Img2=Img0(Range(3-1, Img0.rows - 3+1),Range::all());
Img2(Range(0,1), Range::all()) = 0;
Img2(Range(Img2.rows-1,Img2.rows), Range::all()) = 0;
Mat Disk4;
// exact replacement for mmatlab strel('disk',4,0), somewhat difference than opencv's ellipse structuring element.
MakeFilledEllipse( 4, 4, Disk4);
// bad result
dilate(Img2, Tmp,Disk4, Point(-1,-1),1,BORDER_CONSTANT, Scalar(0));发布于 2015-02-19 13:06:42
Mat在其内部选择ROI时变得不连续.
我不确定在您的例子中,Mat Mat::operator()( Range _rowRange, Range _colRange ) const是否会将CONTINUOUS_FLAG设置为false,但是SUBMATRIX_FLAG肯定会被设置,这会导致不同的操作。
在这里,我认为cv::dilate()的某些部分(例如边界像素外推法,或者决定像素邻域形状的结构元素)会影响您的输出,如果您的ROI周围的原始图像中有像素。
我建议在调用cv::dilate()之前使用以下方法重新排序内存
if (!mat.isContinuous() || mat.isSubmatrix())
{
mat = mat.clone();
}https://stackoverflow.com/questions/28597070
复制相似问题