日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

游戏密保卡图片识别

發(fā)布時(shí)間:2023/11/29 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 游戏密保卡图片识别 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

識(shí)別主要步驟

1.圖像預(yù)處理。包括確認(rèn)圖片有效區(qū)域,灰度化,二值化。

2.字符分割。即將識(shí)別信息最小化。由于密保卡圖片文字寬度固定且無粘連,只需要使用固定寬度切割。

3.對(duì)分割后的信息提取特征,建立特征庫

4.提取特征和特征庫樣本進(jìn)行匹配,輸出識(shí)別結(jié)果

?

首先看下密保卡圖片

1、減少識(shí)別區(qū)域。由于密保卡有效區(qū)域固定,故將有效區(qū)域直接截取出來。

2、圖片灰度化

?圖片灰度算法有平均值法,分量法,最大值法,加權(quán)平均法,本例用的加權(quán)平均法

public static Bitmap CorlorGray(Bitmap bmp){//位圖矩形System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);//以可讀寫方式鎖定全部位圖像素System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);//得到首地址IntPtr ptr = bmpData.Scan0;//定義被鎖定的數(shù)組大小,由位圖數(shù)據(jù)與未用空間組成int bytes = bmpData.Stride * bmpData.Height;byte[] rgbValues = new byte[bytes];//復(fù)制被鎖定的位圖像素值到數(shù)組中System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);//灰度化double colorTemp = 0;for (int i = 0; i < bmpData.Height; i++){//只處理每行圖像像素?cái)?shù)據(jù),舍棄未用空間for (int j = 0; j < bmpData.Width * 3; j += 3){colorTemp = rgbValues[i * bmpData.Stride + j + 2] * 0.299 + rgbValues[i * bmpData.Stride + j + 1] * 0.587 + rgbValues[i * bmpData.Stride + j] * 0.114;rgbValues[i * bmpData.Stride + j] = rgbValues[i * bmpData.Stride + j + 1] = rgbValues[i * bmpData.Stride + j + 2] = (byte)colorTemp;}}//把數(shù)組復(fù)位回位圖System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);//解鎖位圖 bmp.UnlockBits(bmpData);return bmp;}

?

3、圖片二值化

?

最常見的二值處理方法是計(jì)算像素的平均值K,掃描圖像的每個(gè)像素值如像素值大于K

像素值設(shè)為255(白色),值小于等于K像素值設(shè)為0(黑色)

#region 閾值法二值化 public static Bitmap Threshoding(Bitmap b, byte threshold){int width = b.Width;int height = b.Height;BitmapData data = b.LockBits(new System.Drawing.Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);unsafe{byte* p = (byte*)data.Scan0;int offset = data.Stride - width * 4;byte R, G, B, gray;for (int y = 0; y < height; y++){for (int x = 0; x < width; x++){R = p[2];G = p[1];B = p[0];gray = (byte)((R * 19595 + G * 38469 + B * 7472) >> 16);if (gray >= threshold){p[0] = p[1] = p[2] = 255;}else{p[0] = p[1] = p[2] = 0;}p += 4;}p += offset;}b.UnlockBits(data);return b;}}#endregion

?4、字符分割

由于密保卡每個(gè)小方塊大小固定,如果識(shí)別A1,那么只需截取一個(gè)37* 20的圖片塊來進(jìn)行處理。

循環(huán)遍歷圖片Y軸每個(gè)像素點(diǎn),找到字符之間像素全為白色的X坐標(biāo)集合,從而將圖片切割。

public static List<Bitmap> Cut(Bitmap bitmap){List<ContentRectangle> lst = new List<ContentRectangle>();int width = bitmap.Width;int height = bitmap.Height;int[] xarray = new int[width];for (int x = 0; x < width; x++){for (int y = 0; y < height; y++){int r = bitmap.GetPixel(x, y).R;if (r == 0)xarray[x] += 1;}}int temp = 0;int[] yarray = new int[height];for (int i = 0; i < width; i++){if (xarray[i] > 0 && i != 0 && xarray[i - 1] <= 0){temp = i;}if (xarray[i] > 0 && i < width - 1 && xarray[i + 1] <= 0){for (int j = 0; j < height; j++){for (int x = temp + 1; x < i + 1; x++){int r = bitmap.GetPixel(x, j).R;if (r == 0)yarray[j] += 1;}}}int ttmp = 0;for (int y = 0; y < height; y++){if (yarray[y] != 0){ttmp = y;break;}}for (int x = height - 1; x > -1; x--){if (yarray[x] != 0){ContentRectangle rectangle = new ContentRectangle();rectangle.X = temp;rectangle.Width = i + 1 - temp;rectangle.Y = ttmp;rectangle.Height = x + 1 - ttmp;lst.Add(rectangle);yarray = new int[height];break;}}}List<Bitmap> lstbmp = new List<Bitmap>();foreach (ContentRectangle rect in lst){var tempbmp = bitmap.Clone(new System.Drawing.Rectangle(rect.X, rect.Y, rect.Width, rect.Height), bitmap.PixelFormat);lstbmp.Add(tempbmp);}return lstbmp;}

?

5、建立特征庫

字符切割后得到了類似以下圖片。人眼可以直觀的辨識(shí)出來,但機(jī)器卻是不認(rèn)識(shí)的。所以需要建立特征庫,以便機(jī)器比對(duì)識(shí)別。

/// <summary>/// 獲取數(shù)字對(duì)應(yīng)的二值化代碼/// </summary>/// <param name="bitmap"></param>/// <returns></returns>public static string GetCodebybitmap(Bitmap bitmap){StringBuilder code = new StringBuilder();for (int i = 0; i < bitmap.Width; i++){for (int j = 0; j < bitmap.Height; j++){int r = bitmap.GetPixel(i, j).R;code.Append(r > 127 ? "1" : "0");}}return code.ToString();}

將圖片7像素點(diǎn)逐個(gè)掃描轉(zhuǎn)換為0,、1表示的二值化字符串與數(shù)字7進(jìn)行關(guān)聯(lián)存儲(chǔ),建立特征庫。

6、識(shí)別

按上述步驟進(jìn)行圖片處理后取得圖片的0、1表示的二值化代碼并與特征庫中的代碼進(jìn)行比對(duì),匹配對(duì)應(yīng)代碼完成識(shí)別。

匹配算法可以直接使用代碼相等,但這樣使得特征庫必須完善,否則容易匹配失敗。所以一般都會(huì)采用字符串相似度匹配,設(shè)置閾值,相似度大于閾值的即為同一個(gè)字符。相關(guān)算法自行百度。

?

轉(zhuǎn)載于:https://www.cnblogs.com/guozhongxiang/p/7495338.html

總結(jié)

以上是生活随笔為你收集整理的游戏密保卡图片识别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。