1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
| using OpenCvSharp; using OpenCvSharp.Extensions; using System; using System.Drawing; using System.Windows.Forms;
namespace OpenCvSharp_图像校正 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
string img = "test.png";
private void Form1_Load(object sender, EventArgs e) { pictureBox1.Image = new Bitmap(img); }
private void button1_Click(object sender, EventArgs e) { Mat src = new Mat(img);
InputArray kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(3, 3)); Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel, new OpenCvSharp.Point(-1, -1), 3);
Cv2.GaussianBlur(src, src, new OpenCvSharp.Size(11, 11), 2, 2);
Mat canny_Image = new Mat(); Cv2.Canny(src, canny_Image, 10, 30, 3, false);
OpenCvSharp.Point[][] contours; HierarchyIndex[] hierarchly;
Cv2.FindContours(canny_Image, out contours, out hierarchly, RetrievalModes.External, ContourApproximationModes.ApproxSimple, new OpenCvSharp.Point(0, 0));
if (contours.Length == 0) { MessageBox.Show("边缘检测失败"); return; }
Random rnd = new Random(); Scalar color; color = new Scalar(0, 255, 0); for (int i = 0; i < contours.Length; i++) { color = new Scalar(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)); Cv2.DrawContours(src, contours, i, color, 2, LineTypes.Link4); }
double max_area = 0.0; double currentArea = 0.0; OpenCvSharp.Point[] max_contour = null; for (int i = 0; i < contours.Length; i++) { currentArea = Cv2.ContourArea(contours[i]); if (currentArea > max_area) { max_area = currentArea; max_contour = contours[i]; } }
OpenCvSharp.Point[] hull = Cv2.ConvexHull(max_contour); double epsilon = 0.02 * Cv2.ArcLength(max_contour, true); OpenCvSharp.Point[] approx = Cv2.ApproxPolyDP(hull, epsilon, true);
if (approx.Length != 4) { MessageBox.Show("拟合凸包的四个顶点失败"); return; }
Scalar scalar2 = new Scalar(0, 255, 255);
Cv2.Line(src, approx[0], approx[1], scalar2, 1, LineTypes.Link4); Cv2.Line(src, approx[1], approx[2], scalar2, 1, LineTypes.Link4); Cv2.Line(src, approx[2], approx[3], scalar2, 1, LineTypes.Link4); Cv2.Line(src, approx[3], approx[0], scalar2, 1, LineTypes.Link4);
Array.Sort(approx, (cs1, cs2) => { if (cs1 != null && cs1 != null) { if (cs1.Y > cs2.Y) return 1; else if (cs1.Y == cs2.Y) { if (cs1.X < cs2.X) return 1; else return -1; } else return -1; } return 0;
});
OpenCvSharp.Point2f[] srcPt = new OpenCvSharp.Point2f[4]; srcPt[0] = approx[0]; srcPt[1] = approx[1]; srcPt[2] = approx[3]; srcPt[3] = approx[2];
RotatedRect rect = Cv2.MinAreaRect(srcPt); Rect box = rect.BoundingRect(); OpenCvSharp.Point2f[] dstPt = new OpenCvSharp.Point2f[4];
dstPt[0].X = box.X; dstPt[0].Y = box.Y;
dstPt[1].X = box.X + box.Width; dstPt[1].Y = box.Y;
dstPt[2].X = box.X + box.Width; dstPt[2].Y = box.Y + box.Height;
dstPt[3].X = box.X; dstPt[3].Y = box.Y + box.Height;
Mat src2 = new Mat(img); Mat final = new Mat(); Mat warpmatrix = Cv2.GetPerspectiveTransform(srcPt, dstPt); Cv2.WarpPerspective(src2, final, warpmatrix, src.Size());
Bitmap temp = BitmapConverter.ToBitmap(final);
pictureBox2.Image = temp;
DrawLine(srcPt, dstPt);
}
void DrawLine(OpenCvSharp.Point2f[] srcPt, OpenCvSharp.Point2f[] dstPt) { Bitmap bmp = new Bitmap(img); Graphics g = Graphics.FromImage(bmp);
Pen pen = new Pen(Color.Red, 3); Pen pen2 = new Pen(Color.Blue, 3);
g.DrawLine(pen, srcPt[0].X, srcPt[0].Y, srcPt[1].X, srcPt[1].Y); g.DrawLine(pen, srcPt[1].X, srcPt[1].Y, srcPt[2].X, srcPt[2].Y); g.DrawLine(pen, srcPt[2].X, srcPt[2].Y, srcPt[3].X, srcPt[3].Y); g.DrawLine(pen, srcPt[3].X, srcPt[3].Y, srcPt[0].X, srcPt[0].Y);
g.DrawLine(pen2, dstPt[0].X, dstPt[0].Y, dstPt[1].X, dstPt[1].Y); g.DrawLine(pen2, dstPt[1].X, dstPt[1].Y, dstPt[2].X, dstPt[2].Y); g.DrawLine(pen2, dstPt[2].X, dstPt[2].Y, dstPt[3].X, dstPt[3].Y); g.DrawLine(pen2, dstPt[3].X, dstPt[3].Y, dstPt[0].X, dstPt[0].Y);
pictureBox1.Image = bmp;
}
public Image CutImage(Image src, int left, int top, int right, int bottom) { Bitmap srcBitmap = new Bitmap(src); int width = right - left; int height = bottom - top; Bitmap destBitmap = new Bitmap(width, height); using (Graphics g = Graphics.FromImage(destBitmap)) { g.Clear(Color.Transparent); g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(srcBitmap, new Rectangle(0, 0, width, height), left, top, width, height, GraphicsUnit.Pixel); } return destBitmap; } } }
|