c# 如何在大图里面找小图(bmp)
发布网友
发布时间:2022-07-12 10:25
我来回答
共5个回答
热心网友
时间:2023-10-17 12:36
class ImagePoint
{
#region 私有属性
private Bitmap S_Bmp = null;
private Bitmap P_Bmp = null;
private int S_Width;
private int S_Height;
private int P_Width;
private int P_Height;
//容差值
private int Differ;
private int Point_Index;
private byte[][][] S_ImgBype = null;
private byte[][][] P_ImgBype = null;
private PointXY[] Point_XY = null;
private bool isFind;
#endregion
#region 结构体
private struct PointXY : IComparer
{
public int X;
public int Y;
public int DifferXY;
public PointXY(int X, int Y, int Differ)
{
this.X = X;
this.Y = Y;
this.DifferXY = Differ;
}
int IComparer.Compare(object x, object y)
{
return ((PointXY)x).DifferXY - ((PointXY)y).DifferXY;
}
}
#endregion
#region 构造方法
public ImagePoint(Bitmap source,Bitmap part)
{
this.S_Bmp = source;
this.P_Bmp = part;
this.S_Width = source.Width;
this.S_Height = source.Height;
this.S_ImgBype =GetImgByte(S_Bmp);
this.P_Width = part.Width;
this.P_Height = part.Height;
this.P_ImgBype = GetImgByte(P_Bmp);
}
#endregion
#region 公共属性
public int ImagePointCont
{
get
{
if(Point_XY!=null)
{
return Point_XY.Length;
}
else
{
return 0;
}
}
}
public int ImagePointIndex
{
get
{
return Point_Index;
}
set
{
if (value < 0)
{
Point_Index = 0;
}
else if (value > ImagePointCont)
{
Point_Index = ImagePointCont;
}
else
{
Point_Index = value;
}
}
}
#endregion
#region 公共方法
public Point GetImgPoint(int difference)
{
if (isFind)
{
if (Differ == difference)
{
if (Point_XY != null)
{
if (Point_XY.Length > Point_Index)
{
return new Point(Point_XY[Point_Index].X,Point_XY[Point_Index].Y);
}
else
{
return new Point(-1, -1);
}
}
else
{
return new Point(-1,-1);
}
}
else
{
this.Differ = difference > 100 ? 100 : difference < 0 ? 0 : difference;
return GetImgPoint();
}
}
else
{
this.Differ = difference > 100 ? 100 : difference < 0 ? 0 : difference;
return GetImgPoint();
}
}
public Point GetImgPoint(int difference, int index)
{
Point_Index = index;
if (isFind)
{
if (Differ == difference)
{
if (Point_XY != null)
{
if (Point_XY.Length > Point_Index)
{
return new Point(Point_XY[Point_Index].X, Point_XY[Point_Index].Y);
}
else
{
return new Point(-1, -1);
}
}
else
{
return new Point(-1, -1);
}
}
else
{
this.Differ = difference > 100 ? 100 : difference < 0 ? 0 : difference;
return GetImgPoint();
}
}
else
{
this.Differ = difference > 100 ? 100 : difference < 0 ? 0 : difference;
return GetImgPoint();
}
}
public Point GetNextPoint()
{
return GetImgPoint(Differ,++Point_Index);
}
public Point GetBackPoint()
{
return GetImgPoint(Differ, --Point_Index);
}
#endregion
#region 私有方法
private PointXY[] GetPointXY()
{
int _Differ = 0;
int _TotalDiffer = 0;
int _MaxDiffer = this.P_Width * this.Differ / 100;
int _BreakW = S_Width - P_Width + 1;
int _BreakH = S_Height - P_Height + 1;
ArrayList _List = new ArrayList();
for (int w = 0; w < _BreakW; w++)
{
for (int h = 0; h < _BreakH; h++)
{
for (int x = 0; x < P_Width; x++)
{
for (int y = 0; y < P_Height; y++)
{
if (_Differ <= _MaxDiffer)
{
if(!ImgBypeEquals(S_ImgBype[w+x][h+y],P_ImgBype[x][y]))
{
_Differ++;
}
}
else{break;}
}
if(_Differ>_MaxDiffer)
{break;}
else
{
_TotalDiffer+=_Differ;
_Differ=0;
}
}
if(_Differ<=_MaxDiffer)
{
_List.Add(new PointXY(w,h,_TotalDiffer));
}
else
{
_Differ=0;
_TotalDiffer=0;
}
}
}
int _Cont=_List.Count;
PointXY[] _TempPoint=new PointXY[_Cont];
if(_Cont>0)
{
_List.Sort(_TempPoint[0]);
for(int i=0;i<_Cont;i++)
{
_TempPoint[i]=(PointXY)_List[i];
}
return _TempPoint;
}
else
{return null;}
}
private Point GetImgPoint()
{
isFind = true;
Point_XY = GetPointXY();
if (Point_XY != null)
{
if(Point_XY.Length>Point_Index)
{
return new Point(Point_XY[Point_Index].X,Point_XY[Point_Index].Y);
}
else
{
return new Point(-1, -1);
}
}
else
{
return new Point(-1, -1);
}
}
/*/获得BMP三维数组
private byte[, ,] GetImgByte(Bitmap bmp)
{
Bitmap _Bmp = new Bitmap(bmp);
int _BmpWidth = _Bmp.Width;
int _BmpHeight = _Bmp.Height;
BitmapData _Data = _Bmp.LockBits(new Rectangle(0, 0, _BmpWidth, _BmpHeight),
ImageLockMode.ReadWrite, _Bmp.PixelFormat);
byte[] _Byte = new byte[_Data.Stride * _Data.Height];
int _ByteLength = _Byte.Length;
Marshal.Copy(_Data.Scan0, _Byte, 0, _ByteLength);
_Bmp.UnlockBits(_Data);
_Bmp.Dispose();
//计算每个坐标占用长度
int _PointLength = _ByteLength / _BmpWidth / _BmpHeight;
//保存结果的数组
byte[, ,] _Result = new byte[_BmpWidth,_BmpHeight,_PointLength];
for (int h = 0,index=0; h < _BmpHeight; h++)
{
for (int w = 0; w < _BmpWidth; w++)
{
for (int l = 0; l < _PointLength; l++,index++)
{
_Result[w,h,l]=_Byte[index];
}
}
}
return _Result;
}*/
//获得BMP三维交差数组
private byte[][][] GetImgByte(Bitmap bmp)
{
Bitmap _Bmp = new Bitmap(bmp);
int _BmpWidth = _Bmp.Width;
int _BmpHeight = _Bmp.Height;
BitmapData _Data = _Bmp.LockBits(new Rectangle(0, 0, _BmpWidth, _BmpHeight),
ImageLockMode.ReadWrite, _Bmp.PixelFormat);
byte[] _Byte = new byte[_Data.Stride * _Data.Height];
int _ByteLength = _Byte.Length;
Marshal.Copy(_Data.Scan0, _Byte, 0, _ByteLength);
_Bmp.UnlockBits(_Data);
_Bmp.Dispose();
//计算每个坐标占用长度
int _PointLength = _ByteLength/ _BmpWidth / _BmpHeight;
//保存结果的交错数组
byte[][][] _Result = new byte[_BmpWidth][][];
//初始化所有单元
for (int w = 0; w < _BmpWidth; w++)
{
_Result[w] = new byte[_BmpHeight][];
for (int h = 0; h < _BmpHeight; h++)
{
_Result[w][h] = new byte[_PointLength];
}
}
for (int h = 0, index = 0; h < _BmpHeight; h++)
{
for (int w = 0; w < _BmpWidth; w++)
{
for (int l = 0; l < _PointLength; l++, index++)
{
_Result[w][h][l] = _Byte[index];
}
}
}
return _Result;
}
/// <summary>
/// 比较两个BYPE数组是否相同
/// </summary>
/// <param name="src">源</param>
/// <param name="obj">目标</param>
/// <returns>两个对象是否相同</returns>
private bool ImgBypeEquals(byte[] src, byte[] obj)
{
if (src.Length == obj.Length)
{
for (int i = 0; i < src.Length; i++)
{
if (src[i] == obj[i] ? false : true)
{
return false;
}
}
return true;
}
else
{
return false;
}
}
#endregion
}
网上找的没试过
参考资料:http://www.81913221.cn/content.asp?id=24
热心网友
时间:2023-10-17 12:37
我也只是个小虾
1:bmp图,你只给出了大小不等,这一个条件,那么就只能判断大小,
2:我也不知道
3:必定是要循环的,因为你要判断的是个未知数,不知道有几个1,
所以要逐一判断,
(A)找出B中1的位置
(B)判断该位置在A中是否存在
(C)截掉第一个位置字符串,将余下字符赋给新值。继续判断。
热心网友
时间:2023-10-17 12:37
idl 或者 matlab 来做 用C#调用吧
idl 专门用来做图像
matlab 专门来做数*算
idl我用过 速度很快 C# 开发图像算法还是放弃吧~
用idl 都能解决 上述问题
你要啥?
热心网友
时间:2023-10-17 12:38
1.
你用了控件么,改判断控件吧,Location 判断。
2.
byte[] imagebytes=null;
FileStream fs=new FileStream(Image_path,FileMode.Open);
BinaryReader br=new BinaryReader(fs);
imagebytes=br.ReadBytes(br.Length);
3.
用Indexof试试
热心网友
时间:2023-10-17 12:38
3.字符串转int,然后先与后异或。
不能知道多少个,只能知道是否b里面1的位置对应a里面全为1.