转换的阈值时,图像1bpp? [英] Threshold value when converting image to 1bpp?

查看:215
本文介绍了转换的阈值时,图像1bpp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何标记这个问题,请编辑如果可能的话

I don't know how to tag this question, please edit if possible.

的工作:创建一个应用程序,它可以自动-crop图像中的黑色边框批量运行。图片来自100-300dpi质量,1bpp-24 bpp的变化,涌现了一批可以从10而异 - 10 000图像。

The job: Create an application which can auto-crop black borders in images in batch runs. Images vary in quality from 100-300dpi, 1bpp-24bpp and a batch can vary from 10 - 10 000 images.

计划:转换图像1bpp(黑白,黑/白,如果是没有的话)和清理后,白斑点/污垢/噪声找到其中黑色端部和白开始,这些都是对图像作物新COORDS,它们应用到原始图像的一个克隆。 。删除旧图像,保存新的

The plan: Convert image to 1bpp (bitonal, black/white, if it isn't already) and after "cleaning up" white spots/dirt/noise find where the black ends and the white begins, these are the new coords for the image crop, apply them to a clone of the original image. Delete old image, save new one.

进展情况:所有上述操作后,和作品,但...

The progress: All of the above is done, and works, but...

问题:当转换为1bpp我没有一个门槛值的控制权。我需要这个。 。很多暗的图像裁剪得到太多

The problem: When converting to 1bpp I have no control of a "threshold" value. I need this. A lot of dark images get cropped too much.

的尝试:我试过

位图imgBitonal = imgOriginal.Clone(新的Rectangle(0,0,b.Width,b.Height),PixelFormat.Format1bppIndexed)

和还这个。两者工作,但没有似乎其中给我手动设定的阈值的可能性。我需要为用户能够设置该值,除其他外,和运行批处理以便查看是否设置任何好的之前使用我的预览功能。

And also this. Both of which work, but none seem to give me the possibility to manually set a threshold value. I need for the user to be able to set this value, amongst others, and use my "preview" function before running the batch so as to see if the settings are any good.

的呐喊:我在这里的损失。我现在不该做什么或如何做到这一点。请帮一个老乡出来编码器。点我的方向,告诉我在哪里,在一个阈值,发现链接中发现的代码(我还没有找到一个,或不知道去哪里找),或者只给我一些代码工作。任何帮助表示赞赏。

The cry: I'm at a loss here. I don't now what to do or how to do it. Please help a fellow coder out. Point me in a direction, show me where in the code found in the link a threshold value is found (I haven't found one, or don't know where to look) or just give me some code that works. Any help is appreciated.

推荐答案

试试这个,从非常的快1bpp转换

从这里的 24 bpp的转换位图1bpp

        private static unsafe void Convert(Bitmap src, Bitmap conv)
        {
            // Lock source and destination in memory for unsafe access
            var bmbo = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadOnly,
                                     src.PixelFormat);
            var bmdn = conv.LockBits(new Rectangle(0, 0, conv.Width, conv.Height), ImageLockMode.ReadWrite,
                                     conv.PixelFormat);

            var srcScan0 = bmbo.Scan0;
            var convScan0 = bmdn.Scan0;

            var srcStride = bmbo.Stride;
            var convStride = bmdn.Stride;

            byte* sourcePixels = (byte*)(void*)srcScan0;
            byte* destPixels = (byte*)(void*)convScan0;

            var srcLineIdx = 0;
            var convLineIdx = 0;
            var hmax = src.Height-1;
            var wmax = src.Width-1;
            for (int y = 0; y < hmax; y++)
            {
                // find indexes for source/destination lines

                // use addition, not multiplication?
                srcLineIdx += srcStride;
                convLineIdx += convStride;

                var srcIdx = srcLineIdx;
                for (int x = 0; x < wmax; x++)
                {
                    // index for source pixel (32bbp, rgba format)
                    srcIdx += 4;
                    //var r = pixel[2];
                    //var g = pixel[1];
                    //var b = pixel[0];

                    // could just check directly?
                    //if (Color.FromArgb(r,g,b).GetBrightness() > 0.01f)
                    if (!(sourcePixels[srcIdx] == 0 && sourcePixels[srcIdx + 1] == 0 && sourcePixels[srcIdx + 2] == 0))
                    {
                        // destination byte for pixel (1bpp, ie 8pixels per byte)
                        var idx = convLineIdx + (x >> 3);
                        // mask out pixel bit in destination byte
                        destPixels[idx] |= (byte)(0x80 >> (x & 0x7));
                    }
                }
            }
            src.UnlockBits(bmbo);
            conv.UnlockBits(bmdn);
        }

这篇关于转换的阈值时,图像1bpp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆