24小时热门版块排行榜    

查看: 291  |  回复: 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 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[硕博家园] 售SCI一区文章,我:8.O.55.1.O.54,科目齐全,可伽急 +3 HEQlVqMTIA7d 2026-08-07 4/200 2026-08-08 04:07 by 6vVgjDL4CnGu
[考博] 售SCI一区文章,我:8.O.55.1.O.54,科目齐全,可伽急 +3 HEQlVqMTIA7d 2026-08-07 3/150 2026-08-08 03:47 by 6vVgjDL4CnGu
[基金申请] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +3 KXLV3nuBVBY7 2026-08-07 3/150 2026-08-08 03:27 by 6vVgjDL4CnGu
[基金申请] 基金中了 +14 laoda193707 2026-08-06 14/700 2026-08-08 00:23 by 实验小白ha
[基金申请] 关于filecode +4 布布和一二 2026-08-07 7/350 2026-08-07 22:55 by zhanghaozhu
[基金申请] 娱乐 +6 Tide man 2026-08-03 6/300 2026-08-07 22:40 by 铁帽子农民
[基金申请] 化学口download_prp&fileCode的固定段好像这几天一直没变,有变的大神么? +3 Tide man 2026-08-07 4/200 2026-08-07 22:39 by Tide man
[基金申请] 关于代码变化问题,想知道的进来 +9 且听虎啸 2026-08-07 12/600 2026-08-07 21:53 by 理想三旬2025
[基金申请] 固定端突然变了,今天 +6 archvillain 2026-08-06 10/500 2026-08-07 16:03 by 医学老男孩
[基金申请] filecode与中标关系的预测 +3 布布和一二 2026-08-07 3/150 2026-08-07 15:09 by gltch
[论文投稿] 十年后又回来了,论文投稿求助 +4 哈哈114477 2026-08-01 4/200 2026-08-07 14:39 by jgy194592
[基金申请] 关于豆爷回答的JTJC与%2F数量 +4 yang182083 2026-08-06 6/300 2026-08-07 12:43 by 虫友是什么虫
[基金申请] filecode +14 等待解的谜 2026-08-06 19/950 2026-08-07 12:20 by wlwhappy
[基金申请] filecode +8 布布和一二 2026-08-06 11/550 2026-08-06 20:41 by tangpu318
[基金申请] 求各位大神看下 100+6 hpkpkpkp 2026-08-05 33/1650 2026-08-06 14:49 by zhiyanjiang
[基金申请] 影响面上的因素 +8 布布和一二 2026-08-05 11/550 2026-08-06 10:41 by 宝贝虫子
[基金申请] 8月时间戳变的,举个手。玩一下,释放压力 +9 archvillain 2026-08-04 11/550 2026-08-05 20:06 by wlwhappy
[基金申请] 有没有H口的?有收到消息的吗? +3 超级海虾 2026-08-04 3/150 2026-08-04 17:26 by 学教育滴
[基金申请] 纯娱乐,不喜欢勿喷 +7 Tide man 2026-08-04 10/500 2026-08-04 15:10 by loufangrui
[基金申请] 面上提前没消息,有中的吗 +14 archvillain 2026-08-02 18/900 2026-08-04 14:42 by archvillain
信息提示
请填处理意见