磁盘结构元素opencv与Matlab
Disk Structuring Element opencv vs Matlab
我想在OpenCv上创建一个磁盘形状的结构元素。我需要我的SE与
相似sel = strel('disk',5);
我想用
cvstructuringElementEx(cols,rows,anchor_x,anchor_y,shape,*values);
我需要做些什么来实现这一点,anchor_x
和anchor_y
的哪些值与MATLAB给出相同的SE中心点?
根据文档,您可以尝试:
cv::Mat sel = cv::getStructuringElement(MORPH_ELLIPSE, cv::Size(9,9));
这给了我以下的结构元素:
0 0 0 0 1 0 0 0 0
0 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 0
0 0 0 0 1 0 0 0 0
而在MATLAB中我得到:
>> getnhood(strel('disk',5))
ans =
0 0 1 1 1 1 1 0 0
0 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1 0
0 0 1 1 1 1 1 0 0
所以不完全相同,但足够接近:)
我需要与matlab中相同的确切结构元素,因此我根据需要编写了这个(不是很优雅)函数。它适用于3到21之间的奇数行/列的形状(您可以手动添加其他值(在Matlab中检查#零)。
函数是这样调用的:
int Radius = 1;
// following call equivalent to Matlab's sel = getnhood(strel('disk',Radius))
cv::Mat sel = strelDisk(Radius);
实际函数是
cv::Mat strelDisk(int Radius){
// THIS RETURNS STREL('DISK',RADIUS) LIKE IN MATLAB FOR RADIUS = ODD NUMBER BETWEEN 3-->21
cv::Mat sel((2*Radius-1),(2*Radius-1),CV_8U,cv::Scalar(255));
int borderWidth;
switch (Radius){
case 1: borderWidth = 0; break;
case 3: borderWidth = 0; break;
case 5: borderWidth = 2; break;
case 7: borderWidth = 2; break;
case 9: borderWidth = 4; break;
case 11: borderWidth = 6; break;
case 13: borderWidth = 6; break;
case 15: borderWidth = 8; break;
case 17: borderWidth = 8; break;
case 19: borderWidth = 10; break;
case 21: borderWidth = 10; break;
}
for (int i=0; i<borderWidth; i++){
for (int j=0; j<borderWidth; j++){
if (i+j<8){
sel.at<uchar>(i,j)=0;
sel.at<uchar>(i,sel.cols-1-j)=0;
sel.at<uchar>(sel.rows-1-i,j)=0;
sel.at<uchar>(sel.rows-1-i,sel.cols-1-j)=0;
}
}
}
return sel;
相关文章:
- MATLAB的imfinfo in openCV
- opencv c++ kmeans 和 matlab kmeans 的不同结果
- 在 matlab 中调用 opencv 的图像时,无法读取 C++ 中的图像
- 使 OpenCV 调整大小与 Matlab/Octave 调整大小相同
- Opencv 平均滤波器提供的输出与 Matlab 平均滤波器不同
- 想要将此 Matlab 代码转换为 OpenCV 代码
- 如何使用 Matlab 函数 ordfilt2 与 OpenCV C++
- 如何将 OpenCV-contribute-modules 绑定到 MATLAB
- 如何通过MEX将OpenCV C 垫的向量返回到MATLAB
- MATLAB与DFT中的FFT2在OpenCV C 速度比较中
- 使用MATLAB通过MEX运行OPENCV代码在VisualStudio上可以使用
- 使用MATLAB和OpenCV求和图像的效率
- 为 OpenCV 编译 MATLAB 绑定
- 使用OpenCV Vector和MATLAB时遇到问题
- MATLAB sub2ind / ind2sub in OpenCV /C++
- 在OpenCV中使用Matlab的冒号运算符C++没有循环
- opencv 相当于 matlab 的 fliplr() 是什么?
- 如何在opencv中使用matlab c ++库绘制点
- C++ OpenCV 中的等效 Matlab 代码
- 我怎样才能用OpenCV模仿Matlab的medfilt2