- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 OMR 项目中工作,我们使用的是 C#。当我们来扫描答卷时,图像是歪斜的。我们怎样才能消除它们?
最佳答案
可用的 VB.Net 代码 here ,但是由于您要求使用 C#,因此这里是他们的 Deskew 类的 C# 翻译(注意:二值化(严格来说不是必需的,但效果更好)和旋转是留给用户的练习)。
public class Deskew
{
// Representation of a line in the image.
private class HougLine
{
// Count of points in the line.
public int Count;
// Index in Matrix.
public int Index;
// The line is represented as all x,y that solve y*cos(alpha)-x*sin(alpha)=d
public double Alpha;
}
// The Bitmap
Bitmap _internalBmp;
// The range of angles to search for lines
const double ALPHA_START = -20;
const double ALPHA_STEP = 0.2;
const int STEPS = 40 * 5;
const double STEP = 1;
// Precalculation of sin and cos.
double[] _sinA;
double[] _cosA;
// Range of d
double _min;
int _count;
// Count of points that fit in a line.
int[] _hMatrix;
public Bitmap DeskewImage(Bitmap image, int type, int binarizeThreshold)
{
Size oldSize = image.Size;
_internalBmp = BitmapFunctions.Resize(image, new Size(1000, 1000), true, image.PixelFormat);
Binarize(_internalBmp, binarizeThreshold);
return Rotate(image, GetSkewAngle());
}
// Calculate the skew angle of the image cBmp.
private double GetSkewAngle()
{
// Hough Transformation
Calc();
// Top 20 of the detected lines in the image.
HougLine[] hl = GetTop(20);
// Average angle of the lines
double sum = 0;
int count = 0;
for (int i = 0; i <= 19; i++)
{
sum += hl[i].Alpha;
count += 1;
}
return sum / count;
}
// Calculate the Count lines in the image with most points.
private HougLine[] GetTop(int count)
{
HougLine[] hl = new HougLine[count];
for (int i = 0; i <= count - 1; i++)
{
hl[i] = new HougLine();
}
for (int i = 0; i <= _hMatrix.Length - 1; i++)
{
if (_hMatrix[i] > hl[count - 1].Count)
{
hl[count - 1].Count = _hMatrix[i];
hl[count - 1].Index = i;
int j = count - 1;
while (j > 0 && hl[j].Count > hl[j - 1].Count)
{
HougLine tmp = hl[j];
hl[j] = hl[j - 1];
hl[j - 1] = tmp;
j -= 1;
}
}
}
for (int i = 0; i <= count - 1; i++)
{
int dIndex = hl[i].Index / STEPS;
int alphaIndex = hl[i].Index - dIndex * STEPS;
hl[i].Alpha = GetAlpha(alphaIndex);
//hl[i].D = dIndex + _min;
}
return hl;
}
// Hough Transforamtion:
private void Calc()
{
int hMin = _internalBmp.Height / 4;
int hMax = _internalBmp.Height * 3 / 4;
Init();
for (int y = hMin; y <= hMax; y++)
{
for (int x = 1; x <= _internalBmp.Width - 2; x++)
{
// Only lower edges are considered.
if (IsBlack(x, y))
{
if (!IsBlack(x, y + 1))
{
Calc(x, y);
}
}
}
}
}
// Calculate all lines through the point (x,y).
private void Calc(int x, int y)
{
int alpha;
for (alpha = 0; alpha <= STEPS - 1; alpha++)
{
double d = y * _cosA[alpha] - x * _sinA[alpha];
int calculatedIndex = (int)CalcDIndex(d);
int index = calculatedIndex * STEPS + alpha;
try
{
_hMatrix[index] += 1;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
}
private double CalcDIndex(double d)
{
return Convert.ToInt32(d - _min);
}
private bool IsBlack(int x, int y)
{
Color c = _internalBmp.GetPixel(x, y);
double luminance = (c.R * 0.299) + (c.G * 0.587) + (c.B * 0.114);
return luminance < 140;
}
private void Init()
{
// Precalculation of sin and cos.
_cosA = new double[STEPS];
_sinA = new double[STEPS];
for (int i = 0; i < STEPS; i++)
{
double angle = GetAlpha(i) * Math.PI / 180.0;
_sinA[i] = Math.Sin(angle);
_cosA[i] = Math.Cos(angle);
}
// Range of d:
_min = -_internalBmp.Width;
_count = (int)(2 * (_internalBmp.Width + _internalBmp.Height) / STEP);
_hMatrix = new int[_count * STEPS];
}
private static double GetAlpha(int index)
{
return ALPHA_START + index * ALPHA_STEP;
}
}
关于c# - 校正扫描图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2394405/
我试图通过叠加更新的(更详细的)卫星图像(我从 {leaflet} 包中获取)来改善 Rayshader 的外观,但叠加层不匹配与 3D 渲染。 理想情况下,我正在寻找可以获取全局卫星图像的开源解决方
我想构建一个由单个数字组成的常量数组(1..200)来制作一个“查找表”,以将值为 1 - 200 的滚动条的位置转换为用于对图像应用 Gamma 校正的值。 第一个数组值的值在 7.0 - 9.9
我尝试对图像进行一些简单的 Gamma 校正。起初,我尝试使用 Matlab,然后将其应用于 opencv。但我得到不同的结果。下面是部分代码。代码哪里出错了? 在matlab中: for i=1:r
我使用 DirectXTex 库捕获 DX11 游戏的屏幕截图并将其保存到文件中。问题是当我将它保存为 jpeg 时效果很好,但如果我将它保存为 png 图像会变得非常明亮并被洗掉。我使用 Tweak
我正在尝试使用以下代码检测图像中ID卡的边界。关键是我使用的 Gamma 值。我使用2或3的值(假设我希望卡在背景下突出显示)。使用背景较浅或与卡片颜色本身一样浅的照片时遇到问题。请看下面的图片..第
我正在进行立体视觉设置,机翼上方安装了 2 个摄像头。左摄像头向内倾斜几度,而右摄像头与机翼平行。所有可用图像 here 然后使用(剪切和粘贴,但不按原样编译) // performing stere
我正在尝试解决有关 NFA 的问题。指令如下:字母{a, b, c}。 • L1 是最后一个字符与倒数第五个字符相同的所有字符串。例如,应该接受字符串 aaacbacbca,因为倒数第五个字符和最后一
我尝试使用 qt 更改图像的 Gamma,但没有得到理想的结果。这是我的代码: QImage Filters::aply_filtre_gamma(QImage image){ // (std:
我需要对 Y'CbCr 空间中的图像进行 Gamma 校正,以便在图像中的饱和和饥饿区域中显示细节,我想知道是否需要调整色度子 channel ? 我知道如果我让亮度 channel 像素挨饿,如果我
我不了解opencv中hog.cpp中的 Gamma 校正代码,我经历了一些链接here与opencv hog.cpp中的代码不匹配 Mat_ _lut(1, 256); const float* l
根据本页http://www.w3schools.com/cssref/css3_pr_filter.asp有对比度、亮度、色调、饱和度等。但没有明确访问 Gamma 。有没有办法用现有的 CSS3
我进行了超几何分析(使用 Python 脚本)来研究 GO-terms 在基因子集中的富集。我的输出示例如下: GO00001 1500 300 200 150 5.39198144708e-7
我使用 Opencv 编写了一个 Android 应用程序,我的图像处理算法需要对检测到的矩形进行正确的旋转,因此作为该过程的开始,我 将最大的矩形检测为 RotatedRect。 获取矩形的旋转角度
我正在使用 OpenCV 校准和校正立体声系统。我有一个眼睛会聚的立体相机,实际上我按以下顺序运行这些功能: for(int j=0; j < ChessBoard.numSquares; j++)
我会对图像使用 Gamma 校正。因此,我必须使用 G = 0.6 为源图像的每个像素强度赋值。我有问题,因为目标图像完全错误。当我从源图像中获取像素时,可能会遇到转换问题。这是我的代码: #incl
我正在构建一个 Android 应用程序,为用户提供一些图像处理功能。但在应用任何图像转换功能之前,我想进行 Gamma 校正以改善图像。我知道如何执行 Gamma 校正,但我不知道要使用什么 Gam
我在 Windows 10 上使用 SDL2 创建 OpenGL 上下文,但是当我尝试在 Intel UHD 630 上获取帧缓冲区附件颜色编码时,我收到了无效操作错误。在我的 Nvidia Gefo
我有RGB数据和Gamma校正比例 我可以用下面的来计算吗 R = pow(R, 1/Gamma) G = pow(G, 1/Gamma) B = pow(B, 1/Gamma) 或 Gamma 校正
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
自 Snow Leopard 以来,QTKit 现在从 QTMovies frameImageAtTime:withAttributes:error: 等函数返回颜色校正后的图像数据。给定未压缩的 A
我是一名优秀的程序员,十分优秀!