声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1812|回复: 6

[编程技巧] C 程序改编成matlab

[复制链接]
发表于 2007-5-30 13:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
:@) #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[nWidth*nHeight];
BYTE *lpPoints1=new BYTE[nWidth*nHeight];
memset(lpPoints1,0,nWidth*nHeight);
GetPoints(lpPoints);
int d,max;
static s[8][9]={
  {-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[x1+y1*3]*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 编辑 ]
回复
分享到:

使用道具 举报

发表于 2007-5-30 13:36 | 显示全部楼层

回复 #1 cyz1101327 的帖子

一时没看明白,你不如把你的问题写清楚更好
 楼主| 发表于 2007-5-31 16:23 | 显示全部楼层
我要做一个8方向的sobel边缘检测,上面的是一个VC++的程序,我要把他变成MATLAB语言
 楼主| 发表于 2007-5-31 16:27 | 显示全部楼层
主要的要把这段变成matlab语言
int d,max;
static s[8][9]={
  {-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[x1+y1*3]*Point(x+x1-1,y+y1-1);
    }
    if (d>max) max=d;
   }
   if (max>255) max=255;
   Point1(x,y)=(BYTE)max;
  }
请高手帮帮忙咯
 楼主| 发表于 2007-5-31 16:31 | 显示全部楼层
哪个的大哥帮忙看看咯
下面是我自己编的程序提示错误:

??? 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);   
   
    [irow icol]=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
发表于 2007-5-31 20:21 | 显示全部楼层
原帖由 cyz1101327 于 2007-5-31 16:31 发表
哪个的大哥帮忙看看咯
下面是我自己编的程序提示错误:

??? 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);
...


建议先学些基础,然后自己做。你的问题是因为数组或矩阵访问越界,这些错误最好自己调试、修改一下
发表于 2007-6-1 13:04 | 显示全部楼层

谢谢

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-24 07:22 , Processed in 0.054902 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表