gpt4 book ai didi

c# - 不使用Aforge或OpenCv进行圆检测

转载 作者:行者123 更新时间:2023-12-02 16:39:47 25 4
gpt4 key购买 nike

我想在没有库的位图中检测圆。首先,我使用大津脱粒机进行二值化。二值化后,我使用了拉普拉斯边缘检测。现在我的位图中有一个圆圈。我如何检测到这个圈子。

注意:我试图绘制一个圆并 float x和y坐标。但是这种方法太慢了,而且存在半径问题。因为,如果我的tempCircle的半径为10,而真实圆的半径为9,则我的算法找不到该圆。

    BitmapProcess bmpPro = new BitmapProcess((Bitmap)(pictureBox1.Image));
bmpPro.LockBits();
int black = 0, temp = 0,fixX=0,fixY=0;

Bitmap bmp = (Bitmap)pictureBox1.Image;
Graphics g = this.pictureBox1.CreateGraphics();
Pen pen = new Pen(Color.Red, 10);
int x = 0, y = 0;
for (int a = 0; a < pictureBox1.Image.Width - 1; a += 1)
{
for (int b = 0; b < pictureBox1.Image.Height - 1; b++)
{
double radius = 10;

temp = 0;
for (double i = 0.0; i < 360.0; i += 1)
{
double angle = i * System.Math.PI / 180;
x = (int)(a + radius * System.Math.Cos(angle));
y = (int)(b + radius * System.Math.Sin(angle));
Color aa = bmpPro.GetPixel(Math.Abs(x), Math.Abs(y));

if (bmpPro.GetPixel(Math.Abs(x), Math.Abs(y))!=Color.Black) temp++;

}
if (temp > black)
{
black = temp;
fixX = a;
fixY = b;

}

}

}
g.DrawEllipse(pen,fixX,fixY,50,50);



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LockBitBitmap
{
class BitmapProcess
{
public int b = 0;
int TotalPixelLocked;
Bitmap source = null; //kaynak bmp
IntPtr Iptr = IntPtr.Zero; //baslangıc adresi
BitmapData bitmapData = null;

public byte[] Pixels { get; set; }
public int Depth { get; set; }
public int Width { get; set; }
public int Height { get; set; }

public BitmapProcess(Bitmap source)
{
this.source = source; //bitmapı dısardan al
}

public void LockBits()
{
//resmin en ve boyunu al
Width = source.Width;
Height = source.Height;

//kilit için rectangle olustur
Rectangle rect = new Rectangle(0,0,Width,Height);

//kaynak bitmap ın pixel formatını al
Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);

//pixel basına bit sayısını bul(Bpp)
if (Depth != 8 && Depth != 24 && Depth != 32)
{
return; //bit türü desteklenmiyor...
}

//bitmapı kilitle ve veriyi döndür...
bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite, source.PixelFormat);

//kilitlenecek pixel sayısını al
TotalPixelLocked = Math.Abs(bitmapData.Stride) * source.Height;

//pixel verilerini tutmak için byte dizisi olustur
int step = Depth / 8;
Pixels = new byte[TotalPixelLocked * step];
Iptr = bitmapData.Scan0;

//verileri pointerden diziye aktar
Marshal.Copy(Iptr, Pixels, 0, TotalPixelLocked);

}

public Color GetPixel(int x, int y)
{
Color clr = Color.Empty; //boş renk

//renkli nesne sayısını al
int cCount = Depth / 8;

//istenen pixelin baslangıc adresini bul
int i = y * bitmapData.Stride + x * cCount;
if (i > (Pixels.Length - cCount))
{
throw new IndexOutOfRangeException("index out of range ( dizi adresi geçersiz)");
}
if (Depth == 32) //r g b a
{
byte b = Pixels[i];
byte g = Pixels[i + 1];
byte r = Pixels[i + 2];
byte a = Pixels[i + 3];
clr = Color.FromArgb(a,r,g,b);
}

if (Depth == 24) //r g b
{
byte b = Pixels[i];
byte g = Pixels[i + 1];
byte r = Pixels[i + 2];
clr = Color.FromArgb(r, g, b);
}

if (Depth == 8) // r g b hepsi aynı
{
byte c = Pixels[i];
clr = Color.FromArgb(c,c,c);
}


return clr;

}

public void SetPixel(int x, int y, Color color)
{
// renkli nesne sayısı
int cCount = Depth / 8;

// baslangıc indexini bul
//int i = ((y * Width) + x) * cCount;
int i = y * bitmapData.Stride + x * cCount;
if (i > (Pixels.Length - cCount))
{
throw new IndexOutOfRangeException("index out of range ( dizi adresi geçersiz)");
}
if (Depth == 32) // r,g,b, (alpha)
{
Pixels[i] = color.B;
Pixels[i + 1] = color.G;
Pixels[i + 2] = color.R;
Pixels[i + 3] = color.A;
}
if (Depth == 24) // r,g,b
{
Pixels[i] = color.B;
Pixels[i + 1] = color.G;
Pixels[i + 2] = color.R;
b++;
}
if (Depth == 8)//r g b hepsi aynı
{
Pixels[i] = color.B;
}
}

public Bitmap giveBitmap()
{
System.Runtime.InteropServices.Marshal.Copy(Pixels, 0, Iptr, TotalPixelLocked);
source.UnlockBits(bitmapData);
return source;
}





}
}

最佳答案

如果图像中只有几个圆圈,建议您使用RANSAC

简要说明:从您的边缘像素中随机选择3个点,并选择适合这些点的find the equation of the circle。在所有剩余边缘点上使用此方程式,可以查看圆上有多少边缘点,即有多少满足该方程式。重复此过程N次(可以根据各种因素来确定N,包括边缘点的数量,圆的数量等),并记录具有最高内线数(位于圆上的点)的迭代。与此最大迭代相对应的方程是您的最佳拟合圆。

如果图像中有多个圆圈,请在删除或掩盖所有先前找到的圆圈后再重复上述步骤。

关于c# - 不使用Aforge或OpenCv进行圆检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27930913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com