我試圖在C++中實現侵蝕(形態學操作),我使用OpenCV進行讀取並在侵蝕後顯示第一幅圖像和圖像。侵蝕不能正常工作
我想解釋一步一步我是如何做到:
- 我創造了我的「二值圖像」(我用的只有黑和白),所用的255隨機值或0
- 矩陣我複製初始圖像到最終圖像
- 然後,我通過矩陣去(最終圖像)與3×3掩模和餘數,如果我有255
9值My掩模像:
[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;
}
但結果是不正確的。
Initial image and correct result: [錯誤結果] [1]
爲什麼我有-1?這不是個好問題嗎?要麼 ? – Laurentiu
「但結果不正確」......什麼是「不正確」?圖像太小,看不到任何區別 – user463035818
但什麼是正確的公式或過濾器侵蝕? – Laurentiu