我正在尝试为图像卷积实现4个nieghbour laplacian: 我正处于运动的基础阶段,并试图建立我的概念 如何处理图像。我的问题非常基本,但我很困惑。
4邻居拉普拉斯:
0 -1 0 -1 4 -1 0 -1 0 任何人都可以找到错误,因为当我运行这个问题它什么也没做,最后
给我以下的价值观。和图像保持不变。
(507,503)[r = 55 | g = 55 | b = 55]
(507,504)[r = 59 | g = 59 | b = 59]
(507,505)[r = 4e | g = 4e | b = 4e]
(507,506)[r = 45 | g = 45 | b = 45]
(507,507)[r = 4 | g = 4 | b = 4]
(507,508)[r = 0 | g = 0 | b = 0]
(507,509)[r = 0 | g = 0 | b = 0]
(507,510)[r = 0 | g = 0 | b = 0]
(507,511)[r = 0 | g = 0 | b = 0]
(508,0)[r = 0 | g = 0 | b = 0]
(508,1)[r = 0 | g = 0 | b = 0]
(508,2)[r = 0 | g = 0 | b = 0]
(508,3)[r = 0 | g = 0 | b = 0]
(508,4)[r = 0 | g = 0 | b = 0]
void MyFrame::OnTestingImage(wxCommandEvent & event)
{
printf("Testing...");
free(loadedImage);
loadedImage = new wxImage(bitmap.ConvertToImage());
float kernel[3][3]= {{0, -1, 0},{-1,4, -1},{0, -1, 0}};
int r,g,b;
float intensity;
float sum = 0;
for( int i=0;i<imgWidth;i++)
for(int j=0;j<imgWidth;j++){
for(int r = -1;r<2;r++)
for(int t = -1;t<2;t++)
{
if(r+i<0 || r+i>imgWidth || j+t < 0||j+t>imgHeight )
intensity = 0;
else
intensity = loadedImage->(GetRed(i + kernel[r][t], j + kernel[r][t]));
sum += intensity *((float)kernel[r][t]);
}
sum = (int)intensity;
r = g = b = sum;
printf("(%d,%d) [r = %x | g = %x | b = %x] \n",i,j,r,g,b);
loadedImage->SetRGB(i,j,r,g,b);
}
printf(" Finished Testing.\n");
Refresh();
}
答案 0 :(得分:1)
我认为应该是
intensity = loadedImage->(GetRed(i + r, j + t));
答案 1 :(得分:1)
如果我理解您的代码正确,那么您正在更新您正在阅读的相同图片loadedImage
。因此,坐标i/j
的总和将包含i+1/j
和i/j+1
的未处理值,但i-1/j
和i/j-1
的更新值。那是对的吗?这不是卷积的工作原理:总和中的所有像素都应该是原始值。最简单的方法是将结果写入单独的图像。
ADD:@ lennon310是对的,当然应该是loadedImage->(GetRed(i + r, j + t));
答案 2 :(得分:0)
如果你单独处理边框元素会更快,因为你可以避免使用if语句。只需从1迭代到大小 - 1然后零边框元素。
您无法访问正确的元素。代替
intensity = loadedImage->(GetRed(i + kernel[r][t], j + kernel[r][t]));
写
intensity = loadedImage->(GetRed(i + r, j + t));
由于您只访问了五个元素,因此可以完全避免内部for循环,只需编写
即可sum = 0
sum += loadedImage->(GetRed(i + 1, j + 1));
sum += loadedImage->(GetRed(i + 1, j - 1));
sum += loadedImage->(GetRed(i - 1, j + 1));
sum += loadedImage->(GetRed(i - 1, j - 1));
sum += 4 * loadedImage->(GetRed(i, j));`