C 程序改编成matlab
:@) #define EXTRA_NAME "@sobeledge."#include "loadbmp.h"
#define Point(x,y) lpPoints[(x)+(y)*nWidth]
#define Point1(x,y) lpPoints1[(x)+(y)*nWidth]
void Sobel()
{
int x,y,x1,y1,i;
BYTE *lpPoints=new BYTE;
BYTE *lpPoints1=new BYTE;
memset(lpPoints1,0,nWidth*nHeight);
GetPoints(lpPoints);
int d,max;
static s={
{-1,-2,-1,0,0,0,1,2,1},
{0,-1,-2,1,0,-1,2,1,0},
{1,0,-1,2,0,-2,1,0,-1},
{2,1,0,1,0,-1,0,-1,-2},
{1,2,1,0,0,0,-1,-2,-1},
{0,1,2,-1,0,1,-2,-1,0},
{-1,0,1,-2,0,2,-1,0,1},
{-2,-1,0,-1,0,1,0,1,2}
};
for(y=1;y<nHeight-1;y++)
{
for(x=1;x<nWidth-1;x++)
{
max=0;
for(i=0;i<8;i++)
{
d=0;
for(y1=0;y1<3;y1++)
for(x1=0;x1<3;x1++)
{
d+=s*Point(x+x1-1,y+y1-1);
}
if (d>max) max=d;
}
if (max>255) max=255;
Point1(x,y)=(BYTE)max;
}
}
PutPoints(lpPoints1);
delete lpPoints;
delete lpPoints1;
}
[ 本帖最后由 eight 于 2007-5-31 20:20 编辑 ]
回复 #1 cyz1101327 的帖子
一时没看明白,你不如把你的问题写清楚更好 我要做一个8方向的sobel边缘检测,上面的是一个VC++的程序,我要把他变成MATLAB语言 主要的要把这段变成matlab语言int d,max;
static s={
{-1,-2,-1,0,0,0,1,2,1},
{0,-1,-2,1,0,-1,2,1,0},
{1,0,-1,2,0,-2,1,0,-1},
{2,1,0,1,0,-1,0,-1,-2},
{1,2,1,0,0,0,-1,-2,-1},
{0,1,2,-1,0,1,-2,-1,0},
{-1,0,1,-2,0,2,-1,0,1},
{-2,-1,0,-1,0,1,0,1,2}
};
for(y=1;y<nHeight-1;y++)
{
for(x=1;x<nWidth-1;x++)
{
max=0;
for(i=0;i<8;i++)
{
d=0;
for(y1=0;y1<3;y1++)
for(x1=0;x1<3;x1++)
{
d+=s*Point(x+x1-1,y+y1-1);
}
if (d>max) max=d;
}
if (max>255) max=255;
Point1(x,y)=(BYTE)max;
}
请高手帮帮忙咯 哪个的大哥帮忙看看咯
下面是我自己编的程序提示错误:
??? Index exceeds matrix dimensions.
Error in ==> D:\matlab\work\sboel.m
On line 47==> d=d+k1(a,b)*img(x+x1-1,y+y1-1);
M=1;
for i=1:M
str=strcat('33.jpg'); %concatenates two strings that form the name of the image
eval('img=imread(str);');
subplot(1,2,1)
imshow(img);
=size(img); % get the number of rows (N1) and columns (N2)
img=double(img);
end
k1=[-1 -2 -1 0 0 0 1 2 1
0 -1 -2 1 0 -1 2 1 0
1 0 -1 2 0 -2 1 0 -1
2 1 0 1 0 -1 0 -1 -2
1 2 1 0 0 0 -1 -2 -1
0 1 2 -1 0 1 -2 -1 0
-1 0 1 -2 0 2 -1 0 1
-2 -1 0 -1 0 1 0 1 2]
for y=2:icol-1;
for x=2:irow-1;
max=0;
for a=1:8;
d=0;
for b=1:9;
for x1=0:3;
for y1=0:3;
d=d+k1(a,b)*img(x+x1-1,y+y1-1);
y1=y1+1;
end
x1=x1+1;
end
b=b+1;
end
if (d>max)
max=d;
end
a=a+1;
end
if (max>255)
max=255;
img(x,y)=max;
end
i=i+1;
end
j=j+1;
end
subplot(1,2,2)
imshow(img);
end 原帖由 cyz1101327 于 2007-5-31 16:31 发表 http://www.chinavib.com/forum/images/common/back.gif
哪个的大哥帮忙看看咯
下面是我自己编的程序提示错误:
??? Index exceeds matrix dimensions.
Error in ==> D:\matlab\work\sboel.m
On line 47==> d=d+k1(a,b)*img(x+x1-1,y+y1-1);
...
建议先学些基础,然后自己做。你的问题是因为数组或矩阵访问越界,这些错误最好自己调试、修改一下
谢谢
页:
[1]