I am trying to make a Sobel operation on an image but I keep getting flipped results. This is my result I keep having:
My code is the following:
void MyMainWindow::clickButtonEdge() {
int kx[3][3] = {-1, 0 , 1, -2, 0, 2, -1, 0, 1};
int ky[3][3] = {1, 2, 1, 0, 0, 0, -1, -2, -1};
QImage img(m_image_path.c_str());
for(int y=1; y < img.height()-1; y++){
for(int x = 1; x<img.width()-1; x++){
int a = (QColor(img.pixel(x-1,y-1)).red() + QColor(img.pixel(x-1,y-1)).blue()
+ QColor(img.pixel(x-1,y-1)).green())/3;
int b = (QColor(img.pixel(x,y-1)).red() + QColor(img.pixel(x,y-1)).blue()
+ QColor(img.pixel(x,y-1)).green())/3;
int c = (QColor(img.pixel(x+1,y-1)).red() + QColor(img.pixel(x+1,y-1)).green()
+ QColor(img.pixel(x+1,y-1)).blue())/3;
int d = (QColor(img.pixel(x-1,y)).blue() + QColor(img.pixel(x-1,y)).green()
+ QColor(img.pixel(x-1,y)).red())/3;
int e = (QColor(img.pixel(x,y)).green() + QColor(img.pixel(x,y)).red() + QColor(img.pixel(x,y)).blue())/3;
int f = (QColor(img.pixel(x+1,y)).blue() + QColor(img.pixel(x+1,y)).red()
+ QColor(img.pixel(x+1,y)).green())/3;
int g = (QColor(img.pixel(x-1,y+1)).green() + QColor(img.pixel(x-1,y+1)).red()
+ QColor(img.pixel(x-1,y+1)).blue())/3;
int h = (QColor(img.pixel(x,y+1)).blue() + QColor(img.pixel(x,y+1)).green()
+ QColor(img.pixel(x,y+1)).red())/3;
int i = (QColor(img.pixel(x+1,y+1)).red() + QColor(img.pixel(x+1,y+1)).green()
+ QColor(img.pixel(x+1,y+1)).blue())/3;
int matrix[3][3] = {a,b,c,d,e,f,g,h,i};
int sumx = 0;
int sumy = 0;
for(int s=0; s<3; s++){
for(int t=0; t<3; t++){
sumx = sumx + (matrix[s][t] * kx[s][t]);
sumy = sumy + (matrix[s][t] * kx[s][t]);
}
}
int newValue = sqrt(pow(sumx, 2) + pow(sumy, 2));
if(newValue < 0){
newValue = 0;
}
if(newValue > 255){
newValue = 255;
}
QColor test = QColor(img.pixel(x,y));
test.setRed(newValue);
test.setBlue(newValue);
test.setGreen(newValue);
img.setPixel(x, y, test.rgb());
}
}
m_label_picture->setPixmap(QPixmap::fromImage(img));
}
I use Qt and C++
First I make the two kernels named kx and ky.
Then I load the image and build a for loop construction that makes a new matrix from the grey-values of the pixels around the one that I need (for kernel multiplication) , then I make the sum for as well the kx and ky. I really don't know my mistake,... Your help is appreciated!
You are overwriting the input image and then using the overwritten values to later calculation, and this will lead to wrong results.
To avoid this, you can copy the image and use it as the destination. (using the copy as source is better because doing so can reduce copying, but using as destination can be done with less code change)
Also you are using kx
for both x and y calculations.
QImage img(m_image_path.c_str());
QImage out = img.copy(); /* add this */
for(int y=1; y < img.height()-1; y++){
for(int x = 1; x<img.width()-1; x++){
/* omitted, same as the original code */
for(int s=0; s<3; s++){
for(int t=0; t<3; t++){
sumx = sumx + (matrix[s][t] * kx[s][t]);
sumy = sumy + (matrix[s][t] * ky[s][t]); /* use ky, not kx */
}
}
/* omitted, same as the original code */
QColor test = QColor(img.pixel(x,y));
test.setRed(newValue);
test.setBlue(newValue);
test.setGreen(newValue);
out.setPixel(x, y, test.rgb()); /* modify out, not img */
}
}
img = out; /* filter calculation is done, so update img */