侵蚀无法正常工作
Erosion not working correct
我试图在c 中实现侵蚀(形态操作(,我使用openCV来读取和显示侵蚀后的第一个图像和图像。
我想逐步解释我的表现:
- 我使用一个随机值为255或0的矩阵创建了我的"二进制图像"(我只使用了黑白(。
- 我将初始图像复制到最终图像
- 然后,我用3x3掩码浏览了矩阵(最终图像(,如果我的9个值为255。
我的面具喜欢:
[255 255 255
255 255 255
255 255 255]
- 如果我的计数器是= 9,我为黑色面具中心不同的像素涂上了颜色。
这是我的代码:
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
#define WIDTH 16
#define HEIGHT 16
int main( int argc, char** argv )
{
Mat image(HEIGHT, WIDTH, CV_8UC1);
Mat imageFinal(HEIGHT, WIDTH, CV_8UC1);
int values[WIDTH][HEIGHT] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 0, 0,
0, 0, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 0, 0,
0, 0, 0, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 255, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0,
0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0,
0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for(int row = 0; row < WIDTH; row++){
for(int col = 0; col < HEIGHT; col++){
image.data[col + row * image.cols] = values[row][col];
}
}
image.copyTo(imageFinal);
int count = 0;
for(int row = 1; row < image.rows-1; row++){
for(int col = 1; col < image.cols-1; col++){
count = 0;
for(int a = -1; a <= 1; a++){
for(int b = -1; b <= 1; b++){
if(image.at<uchar>(row + a, col + b) == 255){
count++;
}
}
}cout << count << endl;
if(count == 9){
for(int a = -1; a <= 1; a++){
for(int b = -1; b <= 1; b++){
if(a != 0 && b != 0){
imageFinal.at<uchar>(row + a, col + b) = 0;
}
}
}
}
}
}
imshow("Image", image);
imshow("final", imageFinal);
waitKey(0);
return 0;
}
但结果不正确。
初始图像和正确的结果:[错误的结果] [1]
我不是100%确定的,但是在阅读了侵蚀的Wiki条目后,我认为正确实现将是这样的:
for each pixel(x,y) in original_image
count neigbours where original_image == 255
if count == 9
new_image(x,y) = 255
else
new_image(x,y) = 0
end
end
当你有
时for each pixel(x,y) in original_image
count neigbours where original_image == 255
if count == 9
set all neighbours (but not the pixel itself) in new_image to 0
end
基本上侵蚀应对图像的3x3部分进行...
1 1 1 ? ? ? 0 1 1 ? ? ?
1 1 1 -> ? 1 ? and 1 1 1 -> ? 0 ?
1 1 1 ? ? ? 1 1 1 ? ? ?
但是你做
1 1 1 0 0 0
1 1 1 -> 0 1 0
1 1 1 0 0 0
应用过滤器时,您应该仅更改您要查看的像素的值,但是您可以更改所有邻居,而不是像素本身。
我在Wiki文章中发现公式不太启发,也许这有助于理解:
new_image(x,y) = (3x3Sub(old_image,x,y) == mask) * 255
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- sdl软件渲染器不工作,工作在硬件加速的一个
- C++程序已停止工作-求解常微分方程