24小时热门版块排行榜    

查看: 301  |  回复: 0

130098300

银虫 (正式写手)

[求助] 分水岭算法中计算输入图像的梯度这段代码是什么意思

先上代码,代码是我从网上下的,用MFC写的
void CMyImageDBDoc::GetGradient(BYTE* image, INT width, INT height
, FLOAT* deltar, FLOAT* deltasita)
//得到输入图像的梯度;
{
//下面计算各像素在水平和垂直方向上的梯度,边缘点梯度计为0;
INT* deltaxarr;
INT* deltayarr;
INT grawidth = width;
INT graheight = height;
INT deltacount = grawidth * graheight;
deltaxarr = new INT[deltacount];
deltayarr = new INT[deltacount];

  //暂不计算边缘点;
for (INT y=1; y {
for (INT x=1; x {
INT inarrpos = ((y)*width + (x))*3 + 1;//在输入块中的位置;
INT deltaarrpos = y*grawidth + x;//在梯度数组中的位置;
//卷积计算;
deltaxarr[deltaarrpos] = (INT) ( (
image[((y-1)*width + (x+1))*3 + 1] //右上
+ image[((y)*width + (x+1))*3 + 1] //右
+ image[((y+1)*width + (x+1))*3 + 1] //右下
- image[((y-1)*width + (x-1))*3 + 1] //左上
- image[((y)*width + (x-1))*3 + 1] //左
- image[((y+1)*width + (x-1))*3 + 1] ) / 3 );//左下
deltayarr[deltaarrpos] = (INT) ( (
image[((y-1)*width + (x+1))*3 + 1] //右上
+ image[((y-1)*width + (x))*3 + 1] //上
+ image[((y-1)*width + (x-1))*3 + 1] //左上
- image[((y+1)*width + (x-1))*3 + 1] //左下
- image[((y+1)*width + (x))*3 + 1] //下
- image[((y+1)*width + (x+1))*3 + 1]) / 3 );//右下
}
}

//边缘赋为其内侧点的值;
for (y=0; y {
INT x1 = 0;
INT pos1 = y*grawidth + x1;
deltaxarr[pos1] = deltaxarr[pos1+1];
deltayarr[pos1] = deltayarr[pos1+1];
INT x2 = grawidth-1;
INT pos2 = y*grawidth + x2;
deltaxarr[pos2] = deltaxarr[pos2-1];
deltayarr[pos2] = deltayarr[pos2-1];
}
for (INT x=0; x {
INT y1 = 0;
INT pos1 = x;
INT inner = x + grawidth;//下一行;
deltaxarr[pos1] = deltaxarr[inner];
deltayarr[pos1] = deltayarr[inner];
INT y2 = graheight-1;
INT pos2 = y2*grawidth + x;
inner = pos2 - grawidth;//上一行;
deltaxarr[pos2] = deltaxarr[inner];
deltayarr[pos2] = deltayarr[inner];
}


for (y=0; y {
for (x=0; x {
INT temppos = y*grawidth + x;
if ( (deltaxarr[temppos])==0 )
{
if (deltayarr[temppos]!=0)
{
deltasita[temppos] = 0;//水平方向;
deltar[temppos] = (FLOAT) abs(deltayarr[temppos]);
}
else
{
deltasita[temppos] = -1;//无确定方向;
deltar[temppos] = (FLOAT) abs(deltayarr[temppos]);
}
continue;
}
deltasita[temppos] = (FLOAT) ( atan(
(FLOAT)deltayarr[temppos]
/ (FLOAT)deltaxarr[temppos] ) + PI/2. );
deltar[temppos] = (FLOAT) sqrt((DOUBLE)
( deltayarr[temppos]*deltayarr[temppos]
+ deltaxarr[temppos]*deltaxarr[temppos] ) );
}
}

delete [] deltaxarr; deltaxarr = NULL; //删除水平和垂直梯度数组;
delete [] deltayarr; deltayarr = NULL;
}
我想问的是,
INT inarrpos = ((y)*width + (x))*3 + 1;//在输入块中的位置;
这段代码是什么意思呢,为什么要乘以3再加1,然后还有下面这整个循环啥意思,我也不明白,请懂的人详细给我说说这段代码
for (y=0; y {
for (x=0; x {
INT temppos = y*grawidth + x;
if ( (deltaxarr[temppos])==0 )
{
if (deltayarr[temppos]!=0)
{
deltasita[temppos] = 0;//水平方向;
deltar[temppos] = (FLOAT) abs(deltayarr[temppos]);
}
else
{
deltasita[temppos] = -1;//无确定方向;
deltar[temppos] = (FLOAT) abs(deltayarr[temppos]);
}
continue;
}
deltasita[temppos] = (FLOAT) ( atan(
(FLOAT)deltayarr[temppos]
/ (FLOAT)deltaxarr[temppos] ) + PI/2. );
deltar[temppos] = (FLOAT) sqrt((DOUBLE)
( deltayarr[temppos]*deltayarr[temppos]
+ deltaxarr[temppos]*deltaxarr[temppos] ) );
}
}

[ 来自科研家族 哲学人生 ]
回复此楼
Working for the Lord with all my heart
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

智能机器人

Robot (super robot)

我们都爱小木虫

相关版块跳转 我要订阅楼主 130098300 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[基金申请] 科研人应该花精力去思考如何解决问题,而不是去凝练问题 +5 瞬息宇宙 2026-09-01 11/550 2026-09-02 14:15 by simwe390
[基金申请] 要骂人了,新模版改版就是要淡化问题凝练这种虚的东西,结果有个评委还在说凝练得不够 +9 瞬息宇宙 2026-08-31 17/850 2026-09-02 14:04 by anjeeshine
[基金申请] 基金系统什么内容也没有 30+4 winsaint 2026-08-27 10/500 2026-09-02 11:35 by 大不刘6
[基金申请] 学科评审组评审是指会评吗? +5 瞬息宇宙 2026-08-31 5/250 2026-09-02 10:13 by 雨冰共舞
[文学芳草园] 梦想 +5 myrtle 2026-08-26 8/400 2026-09-02 02:42 by Leogzhya
[基金申请] 面上函评意见出来了,像什么等级? 20+4 Tsingking1 2026-08-27 17/850 2026-09-01 19:51 by 超级无敌华子
[基金申请] 为什么资助数各大高校都创新高,自己申请怎么就这么难 +13 Kittylucky 2026-08-27 14/700 2026-09-01 11:06 by feng6531
[论文投稿] 小白求助 投论文要求的highlights应该如何写 5+3 l1963982152 2026-08-29 4/200 2026-09-01 09:04 by 北京莱茵编辑
[基金申请] 麻烦专家们看看评委们的意见(F口面上) +7 gdd2018 2026-08-28 12/600 2026-09-01 08:32 by 尼古拉斯小虫
[基金申请] 基金未中,这种答复是模板吗? +6 zhaosm1982 2026-08-27 7/350 2026-08-31 21:18 by qdxxmc
[基金申请] 面上意见出来了 +12 黄鸟于飞Chao 2026-08-29 23/1150 2026-08-31 18:57 by 黄鸟于飞Chao
[基金申请] 能否申诉? +7 echo8914667 2026-08-30 8/400 2026-08-31 17:00 by yihongxu
[基金申请] 中青基了要发朋友圈吗? +7 349506619 2026-08-28 7/350 2026-08-31 13:39 by 冼亮淀粉酶
[基金申请] 基金不中,共勉 +12 eulota 2026-08-26 12/600 2026-08-31 08:42 by ZJTJZ
[基金申请] 为什么到现在没收到通知? +5 tannykie 2026-08-29 5/250 2026-08-30 21:05 by purplejack
[考博] 找导师 +6 yuanjiabao 2026-08-29 7/350 2026-08-30 14:40 by 生科新手
[基金申请] 我就是申请一个面上项目而已,这评审意见是按照杰青的条件评的吧? +6 gouxfjh 2026-08-28 11/550 2026-08-30 07:57 by gouxfjh
[基金申请] 国自然评审意见 +13 wangmingqi 2026-08-28 19/950 2026-08-29 10:22 by Poppy1104
[基金申请] 2026年叶企孙基金 +4 bud_bud 2026-08-27 7/350 2026-08-29 07:23 by foolishmani
[基金申请] 看板上这么多中的,有点像50人群里49个人都是骗子的那种感觉…… +5 a089 2026-08-26 6/300 2026-08-27 14:05 by jonewore
信息提示
请填处理意见