//输入:原图和圆心信息(圆心位置和半径),输出:直线所在区域(包含较少噪声的圆环) public myPicture doFind(myPicture input, circle cir) { int width = input.width, height = input.height; Bitmap res = new Bitmap(width, height); int x = (int)cir.x, y = (int)cir.y; //截取圆环为0.15*半径——0.5*半径 int maxr = (int)(0.5 * cir.r), minr = (int)(0.15 * cir.r); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { res.SetPixel(i, j, Color.FromArgb(255, 255, 255)); } } //将0.15*半径——0.5*半径区域赋值成原图像内容 for (int i = -maxr; i <= maxr; i++) { int pos1 = -1; for (int j = 0; j < height; j++) { if (i * i + j * j <= maxr * maxr) { pos1 = j; break; } } int pos2 = -1; for (int j = height - 1; j >= 0; j--) { if (i * i + j * j <= maxr * maxr) { pos2 = j; break; } } if (pos1 == -1) { continue; } for (int j = pos1; j <= pos2; j++) { if (i * i + j * j < minr * minr) { continue; } res.SetPixel(x + i, y + j, input.picture.GetPixel(x + i, y + j)); res.SetPixel(x + i, y - j, input.picture.GetPixel(x + i, y - j)); } } return(new myPicture(res)); }
//函数功能:确定仪表盘指针的方向向量 //输入:二值图像input,直线方程ret,仪表盘中心o //输出:仪表盘指针上的某一个点 public myPair doFind(myPicture input, line ret, circle o) { if (Math.Abs(ret.B) < 0.000001) { return(new myPair((int)o.x, 0)); } int MAGIC = (int)(input.width * 0.18); int cnt1 = 0, cnt2 = 0; double K = -ret.A / ret.B; double B = -ret.C / ret.B; for (int dx = 1; dx <= MAGIC; ++dx) { double x = o.x + dx; double y = K * x + B; int xx = (int)x; int yy = (int)y; if (xx + 2 >= input.width || xx - 2 < 0) { break; } if (yy + 2 >= input.height || yy - 2 < 0) { break; } for (int i = -2; i <= 2; ++i) { for (int j = -2; j <= 2; ++j) { if (input.grayDegree[(int)(x + i), (int)(y + j)] == 255) { cnt1++; } } } } for (int dx = 1; dx <= MAGIC; ++dx) { double x = o.x - dx; double y = K * x + B; int xx = (int)x; int yy = (int)y; if (xx + 2 >= input.width || xx - 2 < 0) { break; } if (yy + 2 >= input.height || yy - 2 < 0) { break; } for (int i = -2; i <= 2; ++i) { for (int j = -2; j <= 2; ++j) { if (input.grayDegree[(int)(x + i), (int)(y + j)] == 255) { cnt2++; } } } } if (cnt1 > cnt2) { double x = o.x + MAGIC; double y = K * x + B; return(new myPair((int)x, (int)y)); } else { double x = o.x - MAGIC; double y = K * x + B; return(new myPair((int)x, (int)y)); } }
private double angle(myPair left, myPair right, circle o) { return(Math.Acos(((left.x * right.x - o.x * (left.x + right.x) + o.x * o.x) + (left.y * right.y - o.y * (left.y + right.y) + o.y * o.y)) / (Math.Sqrt((left.x - o.x) * (left.x - o.x) + (left.y - o.y) * (left.y - o.y)) * Math.Sqrt((right.x - o.x) * (right.x - o.x) + (right.y - o.y) * (right.y - o.y))))); }