Hello, I have this code in c ++.
Its only entry is a totally white image with a black dot of a pixel in the center ~ (300x300), the image is 600x600.png located in a folder img /
The problem is that I try to make the CDA 3x3 link based on the first pseudocodigo, but the result is more attached to the chessboard, despite having and modifying with variables d1 and d2, with values 3 and 4 respectively.
In chessboard it is of d1 = 1 and d2 = 1, although a white box is shown in the center, in a 100x100 image it can be better appreciated a minimum change between both sets of modified values (d1, d2). Note if I am totally attached to the pseudocode, I have memory access errors that are not accessible or without reference, that is why it does not start from 1 to i less than Y, etc. in the 'for'.
I have the theory that it is the type of data that does not allow me to make certain comparisons, I have tried with Vec3i, int, char, Vec3b I have made so many attempts but the results vary up to show surreal images, this code is the one that It has given me "better" results.
Beforehand, Thank you!
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <math.h>
using namespace std;
using namespace cv;
int main(int argc, char *argv[]){
Mat I = imread("img/6x6.png", CV_LOAD_IMAGE_COLOR);
if(I.empty()){return -1;}int X = I.rows;int Y = I.cols;
///Chamfer 3x3//////////////////////////////////////////////////////////////////////////////////////////////////////
uchar d1=3; uchar d2=4;
Mat d(X, Y, CV_8UC1);//Mat m(rows, cols)
for(int i = 1; i < Y-1; i++){
for(int j = 1; j < X-1; j++){
d.at<uchar>(i, j) = 255;
}
}
for(int i = 1; i < Y-1; i++){
for(int j = 1; j < X-1; j++){
if(I.at<uchar>(i-1,j) != I.at<uchar>(i,j) || I.at<uchar>(i+1,j) != I.at<uchar>(i,j)||
I.at<uchar>(i,j-1) != I.at<uchar>(i,j) || I.at<uchar>(i,j+1) != I.at<uchar>(i,j)){
d.at<uchar>(i, j) = 0;
}
}
}
for(int i = 1; i < Y-1; i++){
for(int j = 1; j < X-1; j++){
if(d.at<uchar>(i-1,j-1)+d2<d.at<uchar>(i, j)){d.at<uchar>(i, j) = d.at<uchar>(i-1, j-1)+d2;}
if(d.at<uchar>(i,j-1)+d1<d.at<uchar>(i, j)){d.at<uchar>(i, j) = d.at<uchar>(i, j-1)+d1;}
if(d.at<uchar>(i+1, j-1)+d2<d.at<uchar>(i, j)){d.at<uchar>(i, j) = d.at<uchar>(i+1, j-1)+d2;}
if(d.at<uchar>(i-1, j)+d1<d.at<uchar>(i, j)){d.at<uchar>(i, j) = d.at<uchar>(i-1, j)+d1;}
}
}
for(int i = Y-1; i > 1; i--){
for(int j = X-1; j > 1; j--){
if(d.at<uchar>(i+1, j)+d1<d.at<uchar>(i, j)){d.at<uchar>(i, j) = d.at<uchar>(i+1, j)+d1;}
if(d.at<uchar>(i-1, j+1)+d2<d.at<uchar>(i, j)){d.at<uchar>(i, j) = d.at<uchar>(i-1, j+1)+d2;}
if(d.at<uchar>(i, j+1)+d1<d.at<uchar>(i, j)){d.at<uchar>(i, j) = d.at<uchar>(i, j+1)+d1;}
if(d.at<uchar>(i+1, j+1)+d2<d.at<uchar>(i, j)){d.at<uchar>(i, j) = d.at<uchar>(i+1, j+1)+d2;}
}
}
for(int i = Y-1; i > 1; i--){
for(int j = X-1; j > 1; j--){
if(I.at<uchar>(i,j)==0){d.at<uchar>(i, j)=-d.at<uchar>(i, j);}
}
}
///Chamfer 3x3//////////////////////////////////////////////////////////////////////////////////////////////////////
imshow("Input", I);
imshow("3x3", d);
waitKey(0);
return 0;
}