Since you did not provide any information about the image class you use, I will just invent a very generic one. It basically just stores the red, green and blue components as integer values, has a routine to set these values and one to do a simple check whether the pixel is black (done as mentioned in my reply to your comment on solution 1) as well as a ToString() routine to return the color components as a formated string.
class Color
{
public int R, G, B;
public void SetColor(int R, int G, int B)
{
this.R = R;
this.G = G;
this.B = B;
}
public bool isBlack()
{
if (R == 0 && G == 0 && B == 0)
{
return true;
}
return false;
}
public override string ToString()
{
return "(" + R.ToString() + "," + G.ToString() + "," + B.ToString() + ")";
}
}
Now, we need a two-dimentional array that serves as image, I will just use a 4x4 matrix for this example.
int width = 4, height = 4;
Color[,] image = new Color[height, width];
The image has to be initialized. You said your image has a black edge, so let us make an image with a black border and some colored data in the center, something like this:
Now comes the initialization of the image which is pretty boring to read, I will just show a shortened version here. When you use a helper class to load your image you do have this data anyway. But for the sake of this example, here is the initialization:
image[0, 0] = new Color();
image[0, 0].SetColor(0, 0, 0);
image[1, 0] = new Color();
image[1, 0].SetColor(0, 0, 0);
image[1, 1] = new Color();
image[1, 1].SetColor(255, 0, 0);
image[1, 2] = new Color();
image[1, 2].SetColor(0, 0, 255);
image[2, 0] = new Color();
image[2, 0].SetColor(0, 0, 0);
image[2, 1] = new Color();
image[2, 1].SetColor(0, 255, 0);
image[2, 2] = new Color();
image[2, 2].SetColor(255, 255, 0);
image[3, 3] = new Color();
image[3, 3].SetColor(0, 0, 0);
I will provide you with a link to the full source code of the example at the end, so if any questions arise, just have a look at the source code.
To later be able to compare the input with the result, let's see the image data.
Console.WriteLine("Old Image:\n");
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Console.Write(image[y, x].ToString() + " ");
}
Console.WriteLine();
}
The next part is more interesting for you, I guess. As I said in my first solution, my suggestion is to find the min and max X and Y values of the colored area in your image and copy this area into a new image. First, let's find these min and max values.
int minX = 0,
maxX = 0,
minY = 0,
maxY = 0;
bool gotMin = false;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (!image[y, x].isBlack())
{
if (!gotMin)
{
minX = x;
minY = y;
gotMin = true;
}
if (x > maxX) maxX = x;
if (y > maxY) maxY = y;
}
}
}
If you need any help understanding what's going on there, please have a look at the image I provide at the end. It demonstrates very detailed how I build this algorithm and you can use the method to tweak the algorithm as you might need it.
Now, the area we want to copy is know to us, let's copy the data to a new image. As you can see, these are exactly the same formulas I provided in the first solution.
int newWidth = maxX - minX + 1;
int newHeight = maxY - minY + 1;
Color[,] newImage = new Color[newHeight, newWidth];
for (int y = minY; y <= maxY; y++)
{
for (int x = minX; x <= maxX; x++)
{
newImage[y - minY, x - minX] = new Color();
newImage[y - minY, x - minX].SetColor(image[y, x].R, image[y, x].G, image[y, x].B);
}
}
And finally, let's see the result and compare it with the input.
Console.WriteLine("New Image:\n");
for (int y = 0; y < newHeight; y++)
{
for (int x = 0; x < newWidth; x++)
{
Console.Write(newImage[y, x].ToString() + " ");
}
Console.WriteLine();
}
Of course, you would use other methods to display your image, the console stuff here is just to give you an example.
Well, basically that is what I was talking about in my first solution.
I hope this helps.
Cheerio!
Links
Source code
Iteration analysis for constructing the algorithm (Image)