【发布时间】:2016-09-05 01:15:19
【问题描述】:
我有一个程序可以将图像拆分为 50 x 50 像素块进行编辑,我已经尝试了 3 天来编写嵌套 for 循环来完成我需要的操作,但在此之前我尝试获取宽度段和高度分段,然后尝试获取每个分段的区域,但这根本不起作用,所以我尝试了几种使用嵌套 for 循环的方法,但有些东西我遗漏或没有正确执行,任何帮助将不胜感激因为我没有想法!这是我当前的等式,其中首先列出并解释了变量:
j = (picture1.bbox_right + 1) - picture1.bbox_left; //gets image width in pixels
k = (picture1.bbox_bottom + 1) - picture1.bbox_top; //gets image height in pixels
h = j*k; //total number of pixels in image
s = j mod 50; //remaining pixels in image x plane
t = k mod 50; //remaining pixels in y plane
c = ceil(j/50); //number of segments in x plane, segments can be a maximum of 50 pixels wide
d = ceil(k/50); //number of segments in y plane segments can be a maximum of 50 pixels tall
brk = c*d; //total number of segments in image
for (i=0; i<h+1; i+=1) z[i] = 0;
for (i=0; i<=brk+1; i+=1) {v[i] = 0; w[i] = 0; z[i] = 0;} //v[] is the segment width, w[] is the segment height, z[] is the area of the segment
//drwatx[] and m[] are the starting x position of each segment
//drwaty[] and n[] are the starting y position of each segment
if(h > 2500)
{
if(d > c)
{
for(i=0; i<brk; i+=1)
{
for(a=0; a<d-1; a+=1)
{
if(a < d-1)
{
for(b=0; b<c-1; b+=1)
{
if(b < c-1)
{
m[b + i] = picture1.bbox_left + b*50;
drwatx[b + i] = picture1.bbox_left + b*50;
v[b + i] = 50;
}
if (b == c-1 && s == 0) v[b + i] = 50;
if (b == c-1 && s > 0) v[b + i] = s;
}
if(a < d-1)
{
n[a + i] = picture1.bbox_top - 1 + a*50;
drwaty[a + i] = picture1.bbox_top - 1 + a*50;
w[a + i] = 50;
}
if(a == d-1 && s == 0)
{
w[a + i] = 50;
}
if(a == d-1 && s > 0)
{
v[a + i] = s;
w[a + i] = t;
}
}
}
z[i] = v[i]*w[i];
}
}
}
这是一张包含更多信息的图,以及我的嵌套 forloop 的结果与我试图获得的结果-
我正在寻找的输出是每个段的起始 x,y 位置和每个段的区域。
【问题讨论】:
-
我建议使用更好的变量名。您的代码几乎不可读。