开发者

C# array position

I know its a dumb question, but to find value of x and y in an array using index position, can we do it ?

int [,] Values = new int [Width,Height];
for (int Xcount = 0; Xcount < Width; Xcount++)
{
     for (int Ycount = 0; Ycount < Height; Ycount++)
     {
         Color col = img.GetPixel(Xcount, Ycount);
         if (col.R > 140 && col.G > 140 && col.B > 140)
         {              
              Values[Xcount , Ycount]++;
              file.WriteLine("X :" + Xcount + "  Y :" + Ycount);
              count++;
         }
    }
}

How can i get the value of X and Y at index position 0 and last index pos开发者_StackOverflowition i.e count; I need the fist and last values to be used in further processing Thanks in advance !


Are you asking how to obtain the x and y indexes from a single position value?

If so, I just answered a question like that here.


To find the first and last pixels encountered by your loops that was above the (140,140,140) threshold:

As "count" is only incremented for pixels that are above the threshold, it does not contain enough information at the end of the loop to locate the (x,y) position that first/last caused it to be incremented. You need to save/remember the X,Y position of the pixels as you discover them.

You will therefore need to keep a couple of extra X,Y variables. When you encounter the first pixel above your threshold, save X,Y into firstX,firstY. Every time you encounter a pixel above the threshold, store X,Y into lastX,lastY. On exit from the loop you will then have the values you're asking for.

I'm still not sure what you'd need these values for, though. I'm wondering if you are actually trying to find a rectangle that encloses all pixels above the threshold, in which case you need to do something similar to the above, but treat X and Y independently. start with (e.g.) minX=9999, maxY=9999, maxX=0, maxY=0. When a pixel is above your threshold, update the min/max values to accumulate the bounds of the rectangle, i.e:

if (X < minX) minX = X;
if (Y < minY) minY = Y;
if (X > maxX) maxX = X;
if (Y > maxY) maxY = Y;

Then at the end of the scan, you will have the two corners (minX, minY) and (maxX, maxY) that describe the rectangle which encloses all pixels above the threshold value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜