是否应将morphologyEx使用矩阵元素之外的元素作为边界像素?

时间:2019-03-02 19:47:00

标签: c++ opencv

这是使用OpenCV 4.0.1运行的。不确定上一个版本。

下面的代码显示OpenCV的膨胀,我认为morphologyEx通常在子矩阵与相同矩阵的克隆上计算时不会返回相同的结果。似乎是使用子矩阵外的值来计算结果的边缘值。

简而言之,v2in和v3in是等效的,但v2in由v1的子矩阵组成,而v3in是其子集。但是,两者的扩张会产生不同的结果,其中扩张v2in似乎错误地使用了数据,而不是从与v2in数据相邻的v1in元素中获取的v2in中。

// dilation of a 1x7 "vector" works as expected.
Mat_<uchar> vin1 = (Mat_<uchar>(1, 7) << 0,0,0,1,0,0,0);
std::cout << "vin1:" << vin1 << std::endl;
Mat_<uchar> vout1;
morphologyEx(vin1, vout1, MORPH_DILATE, Mat::ones(1, 5, CV_8UC1));
std::cout << "vout1:" << vout1 << std::endl;

// Changing the edge values of v1in1.
Mat_<uchar> vin2;
vin1(0) = 255; vin1(6) = 128;
// creating v2 as a subset of v1 that doesn't include it edge elements (v1 elts 0 and 6)
vin2 = vin1.colRange(1, 6);
// printing the state of things.  Comfirming that v2 is in fact a sub-matrix of 1 that doesn't include the 
// edge (v1 elts 0 and 6) values of vin1
std::cout << "in1(0) = 255; vin1(6) = 128 modified vin1= " << vin1 << std::endl;
std::cout << "vin1.datastart= "  << std::hex << static_cast<const void*>(vin1.datastart) << std::endl;
std::cout << "vin2=vin1.colRange(1, 6), vin2=" << vin2 << std::endl;
std::cout << "vin2.datastart= " << std::hex << static_cast<const void*>(vin2.datastart) << std::endl;
std::cout << "vin2.data= " << std::hex << static_cast<void*>(vin2.data) << std::endl;
// dilation of v2, in vout2, uses the data not in v2, taken from v1 elts 0 and 6 yielding bad results.
Mat_<uchar> vout2;
morphologyEx(vin2, vout2, MORPH_DILATE, Mat::ones(1, 5, CV_8UC1));
std::cout << "Dilate of vin2=vin1.colRange(1, 6), vout2= " << vout2 << std::endl;
Mat_<uchar> vin3 = vin1.colRange(1, 6).clone();
// But doing the same dilation using a clone vs a submatrix gives a different, and correct, answer.
std::cout << "vin3=vin1.colRange(1, 6).clone(), vin3=" << vin3 << std::endl;
Mat_<uchar> vout3;
morphologyEx(vin3, vout3, MORPH_DILATE, Mat::ones(1, 5, CV_8UC1));
std::cout << "Dilate of vin3=vin1.colRange(1, 6).clone(), vout3" << vout3 << std::endl;

输出:

  

vin1:[0,0,0,1,0,0,0]

     

vout1:[0,1,1,1,1,1,0]

     

in1(0)= 255; vin1(6)= 128修改后的vin1 = [255,0,0,1,0,0,128]

     

vin1.datastart = 000001DC6A628FC0

     

vin2 = vin1.colRange(1,6),vin2 = [0,0,1,0,0]

     

vin2.datastart = 000001DC6A628FC0

     

vin2.data = 000001DC6A628FC1

     

vin2 = vin1.colRange(1,6),vout2 = [255,255,1,128,128]的扩张

     

vin3 = vin1.colRange(1,6).clone(),vin3 = [0,0,1,0,0]

     

vin3 = vin1.colRange(1,6).clone(),vout3 [1,1,1,1,1]的扩张

0 个答案:

没有答案