本文介绍了检测扫描区域(颜色检测)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在扫描图像,但是我想仅自动裁剪扫描区域并从扫描图像中删除未使用的黑色区域.
换句话说,我想从扫描的图像中删除不需要的颜色(黑色)或不需要的边缘.

任何人都有什么主意吗?
问候..

I am scanning an image but I want to automatic crop only the scanned area and delete the unused black area from the scanned image.
In other words I want to remove the unwanted color (black) or unwanted edges from the scanned images.

Any body have any idea ???
regards..

推荐答案

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() + ")";
    }
}



现在,我们需要一个二维数组作为图像,在此示例中,我将仅使用4x4矩阵.



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:

// Example image:
//
// B B B B
// B C C B
// B C C B
// B B B B
//
// B - black
// C - color



现在是图像的初始化,这很无聊,我在这里只显示一个缩短的版本.当您使用帮助器类加载图像时,您仍然拥有此数据.但是为了这个示例,这里是初始化:



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:

// 1. Line
image[0, 0] = new Color();
image[0, 0].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);

// [..]

// 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[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.

// "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();
}



我想,下一部分对您来说更有趣.正如我在第一个解决方案中所说的那样,我的建议是找到图像中彩色区域的最小X和Y值,并将该区域复制到新图像中.首先,让我们找到这些最小值和最大值.



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.

// "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();
}



当然,您可以使用其他方法来显示图像,这里的控制台内容只是为了举例.

好吧,基本上这就是我在第一个解决方案中所说的.

希望对您有所帮助.

啦啦啦!


链接

源代码
迭代分析



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



这篇关于检测扫描区域(颜色检测)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:04