Skip to content

Instantly share code, notes, and snippets.

@clovergaze
Last active April 10, 2021 12:37
Show Gist options
  • Save clovergaze/3d88dc1678528c481a70466c11139b02 to your computer and use it in GitHub Desktop.
Save clovergaze/3d88dc1678528c481a70466c11139b02 to your computer and use it in GitHub Desktop.
Crop image area by detecting colored pixels (code for a CodeProject answer)
using System;
namespace DetectPixels
{
class Color
{
// Color components
public int R, G, B;
// Set color components
public void SetColor(int R, int G, int B)
{
this.R = R;
this.G = G;
this.B = B;
}
// Basic check, if the pixel is black
public bool isBlack()
{
if (R == 0 && G == 0 && B == 0)
{
return true;
}
return false;
}
// Routine for output purposes
public override string ToString()
{
return "(" + R.ToString() + "," + G.ToString() + "," + B.ToString() + ")";
}
}
class Program
{
static void Main(string[] args)
{
int width = 4, height = 4;
Color[,] image = new Color[height, width];
// Example image:
//
// B B B B
// B C C B
// B C C B
// B B B B
//
// B - black
// C - color
// 1. Line
image[0, 0] = new Color();
image[0, 0].SetColor(0, 0, 0);
image[0, 1] = new Color();
image[0, 1].SetColor(0, 0, 0);
image[0, 2] = new Color();
image[0, 2].SetColor(0, 0, 0);
image[0, 3] = new Color();
image[0, 3].SetColor(0, 0, 0);
// 2. Line
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[1, 3] = new Color();
image[1, 3].SetColor(0, 0, 0);
// 3. Line
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[2, 3] = new Color();
image[2, 3].SetColor(0, 0, 0);
// 4. Line
image[3, 0] = new Color();
image[3, 0].SetColor(0, 0, 0);
image[3, 1] = new Color();
image[3, 1].SetColor(0, 0, 0);
image[3, 2] = new Color();
image[3, 2].SetColor(0, 0, 0);
image[3, 3] = new Color();
image[3, 3].SetColor(0, 0, 0);
// "Show" the image
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();
}
Console.WriteLine();
// Get min, 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;
}
}
}
// Copy the relevant stuff
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);
}
}
// "Show" new image
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();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment