Skip to content

Instantly share code, notes, and snippets.

@vumbumy
Created March 4, 2016 04:23
Show Gist options
  • Save vumbumy/b80ddda258ba4e252aa9 to your computer and use it in GitHub Desktop.
Save vumbumy/b80ddda258ba4e252aa9 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using Cognex.VisionPro.OCRMax;
using Cognex.VisionPro.Display;
using Cognex.VisionPro;
namespace withOpenCV
{
public partial class Form1 : Form
{
List<List<double>>[] cfdcX = null;
List<List<double>>[] cfdcY = null;
const double q = 0.01;
const double r = 0.01;
const int iterationCnt = 100;
const double T1 = 0.08;
const double T2 = 0.9;
public Form1()
{
InitializeComponent();
iteration_label.Text += iterationCnt.ToString();
T1_label.Text += T1;
T2_label.Text += T2;
r_label.Text += r;
String rootPath = "D:\\Samples\\Images";
String filePath = rootPath + "\\Demo\\10.jpg";
// OpenCV Sobel Edge Detection Image
Mat matImage = new Mat(filePath);
Mat sobel_1 = new Mat(); Mat sobel_2 = new Mat();
Cv2.CvtColor(matImage, matImage, ColorConversionCodes.BGR2GRAY); ;
Cv2.Sobel(matImage, sobel_1, MatType.CV_8U, 1, 0);
Cv2.Sobel(matImage, sobel_2, MatType.CV_8U, 0, 1);
Cv2.Add(sobel_1, sobel_2, matImage);
pictureBox5.Image = matImage.ToBitmap();
// 이미지 불러오기
Bitmap image = new Bitmap(filePath);
// 첫번째 Box에 원본 이미지 반영
pictureBox1.Image = image;
// 원본 이미지 (N * M) 픽셀 사이에 엣지의 크기는 (N-1) * (M-1)
int height = image.Height - 1;
int width = image.Width - 1;
// k번째 엣지 이미지 변수 선언
Bitmap bmpEdge = new Bitmap(width, height);
// k번째 X축 Y축 confidence 변수 선언
cfdcX = new List<List<double>>[2];
cfdcY = new List<List<double>>[2];
cfdcX[0] = new List<List<double>>();
cfdcY[0] = new List<List<double>>();
// Maximum gradient magnitude
int maxMagtd = 255;
/*
* ■ □ □
* ■ □ □
* ■ □ □
*/
for (int i = 0; i < height; i++)
{
int now = rgbToY(image.GetPixel(0, i));
int nextX = rgbToY(image.GetPixel(1, i));
int nextY = rgbToY(image.GetPixel(0, i+1));
int nextXY = rgbToY(image.GetPixel(1, i+1));
int gapX = nextX - now + nextXY - nextY;
int gapY = nextY - now + nextY - nextX;
int magntd = (int)Math.Max(Math.Min(gapX + gapY, 255), 0);
bmpEdge.SetPixel(0, i, Color.FromArgb(magntd, magntd, magntd));
}
/*
* ■ ■ ■
* □ □ □
* □ □ □
*/
for (int j = 1; j < width; j++)
{
int now = rgbToY(image.GetPixel(j, 0));
int nextX = rgbToY(image.GetPixel(j + 1, 0));
int nextY = rgbToY(image.GetPixel(j, +1));
int nextXY = rgbToY(image.GetPixel(j+1, 1));
int gapX = nextX - now + nextXY - nextY;
int gapY = nextY - now + nextY - nextX;
int magntd = (int)Math.Max(Math.Min(gapX + gapY, 255), 0);
bmpEdge.SetPixel(j, 0, Color.FromArgb(magntd, magntd, magntd));
}
/*
* □ □ □
* □ ■ ■
* □ ■ ■
*/
for (int i = 1; i < height; i++)
{
for (int j = 1; j < width; j++)
{
int nextX1 = rgbToY(image.GetPixel(j+1, i-1));
int nextX2 = rgbToY(image.GetPixel(j+1, i));
int nextX3 = rgbToY(image.GetPixel(j+1, i+1));
int bfX1 = rgbToY(image.GetPixel(j - 1, i - 1));
int bfX2 = rgbToY(image.GetPixel(j - 1, i));
int bfX3 = rgbToY(image.GetPixel(j - 1, i + 1));
int nextY1 = rgbToY(image.GetPixel(j - 1, i + 1));
int nextY2 = rgbToY(image.GetPixel(j, i + 1));
int nextY3 = rgbToY(image.GetPixel(j + 1, i + 1));
int bfY1 = rgbToY(image.GetPixel(j - 1, i - 1));
int bfY2 = rgbToY(image.GetPixel(j, i - 1));
int bfY3 = rgbToY(image.GetPixel(j + 1, i - 1));
int gapX = nextX1 + nextX2 + nextX3 - (bfX1 + bfX2 + bfX3);
int gapY = nextY1 + nextY2 + nextY3 - (bfY1 + bfY2 + bfY3);
int magntd = (int)Math.Max(Math.Min(gapX + gapY, 255), 0);
bmpEdge.SetPixel(j, i, Color.FromArgb(magntd, magntd, magntd));
/*
int bfX = rgbToY(bmpEdge.GetPixel(j-1,i));
int bfY = rgbToY(bmpEdge.GetPixel(j,i-1));
// Maximum 산출
maxMagtd = Math.Max(maxMagtd, Math.Abs(bfX-magntd));
maxMagtd = Math.Max(maxMagtd, Math.Abs(bfY-magntd));
*/
}
}
//pictureBox2.Image = (Bitmap)bmpEdge.Clone();
string txt = "";
height--; width--;
for (int i = 0; i < height; i++)
{
cfdcX[0].Add(new List<double>());
cfdcY[0].Add(new List<double>());
for (int j = 0; j < width; j++)
{
int nextX = rgbToY(bmpEdge.GetPixel(j+1,i));
int nextY = rgbToY(bmpEdge.GetPixel(j, i + 1));
int now = rgbToY(bmpEdge.GetPixel(j, i));
cfdcX[0][i].Add(Math.Abs(now - nextX)*1.0 / maxMagtd);
cfdcY[0][i].Add(Math.Abs(now - nextY)*1.0 / maxMagtd);
/*
if (cfdcX[0][i][j] > 0.9 || cfdcY[0][i][j] > 0.9)
MessageBox.Show(i + " , " + j);
*/
//txt += cfdcX[0][i][j].ToString() + "\t" + cfdcY[0][i][j].ToString() + "\r\n";
if (cfdcX[0][i][j] <= T1)
cfdcX[0][i][j] = 0;
else if (cfdcX[0][i][j] >= T2)
cfdcX[0][i][j] = 1;
if (cfdcY[0][i][j] <= T1)
cfdcY[0][i][j] = 0;
else if (cfdcY[0][i][j] >= T2)
cfdcY[0][i][j] = 1;
int pixelVal = (int)(Math.Max(cfdcY[0][i][j], cfdcX[0][i][j]) * 255);
bmpEdge.SetPixel(j, i, Color.FromArgb(pixelVal, pixelVal, pixelVal));
}
}
System.IO.File.WriteAllText(rootPath + "\\Demo\\data.txt", txt);
pictureBox2.Image = (Bitmap)bmpEdge.Clone();
int h = height - 1;
int w = width - 1;
for (int k = 0; k < iterationCnt; k++)
{
cfdcX[1] = new List<List<double>>(cfdcX[0]);
cfdcY[1] = new List<List<double>>(cfdcY[0]);
for (int i = 1; i < h; i++)
{
for (int j = 1; j < w; j++)
{
// X축에 대한 Vertex Types
int leftType = vertexType(
cfdcY[0][i - 1][j],
cfdcX[0][i - 1][j],
cfdcY[0][i - 1][j + 1]
);
int rightType = vertexType(
cfdcY[0][i][j],
cfdcX[0][i + 1][j],
cfdcY[0][i][j + 1]
);
cfdcX[1][i][j] = edgeType(leftType, rightType, cfdcX[0][i][j]);
if (cfdcX[1][i][j] <= T1)
cfdcX[1][i][j] = 0;
else if (cfdcX[1][i][j] >= T2)
cfdcX[1][i][j] = 1;
// Y축에 대한 Vertex Types
leftType = vertexType(
cfdcX[0][i][j - 1],
cfdcY[0][i][j - 1],
cfdcX[0][i + 1][j - 1]
);
rightType = vertexType(
cfdcX[0][i][j],
cfdcY[0][i + 1][j],
cfdcX[0][i][j + 1]
);
cfdcY[1][i][j] = edgeType(leftType, rightType, cfdcY[0][i][j]);
if (cfdcY[1][i][j] <= T1)
cfdcY[1][i][j] = 0;
else if (cfdcY[1][i][j] >= T2)
cfdcY[1][i][j] = 1;
}
}
cfdcX[0] = cfdcX[1];
cfdcY[0] = cfdcY[1];
}
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
{
int pixelVal = (int)(Math.Max(cfdcY[1][i][j], cfdcX[1][i][j]) * 255);
bmpEdge.SetPixel(j, i, Color.FromArgb(pixelVal, pixelVal, pixelVal));
}
pictureBox3.Image = bmpEdge;
}
int rgbToY(Color c)
{
return (int)(c.R * 0.299 + c.G * 0.587 + c.B * 0.114);
}
double edgeType(int t1, int t2, double val)
{
if (t1 == 0 && t2 == 0)
return Math.Max(0, val - r);
if (t1 == 0 && t2 == 1)
return val;
if (t1 == 0 && t2 == 2)
return Math.Max(0, val - r);
if (t1 == 0 && t2 == 3)
return Math.Max(0, val - r);
if (t1 == 1 && t2 == 0)
return val;
if (t1 == 1 && t2 == 1)
return Math.Min(1, val + r);
if (t1 == 1 && t2 == 2)
return Math.Min(1, val + r);
if (t1 == 1 && t2 == 3)
return Math.Min(1, val + r);
if (t1 == 2 && t2 == 0)
return Math.Max(0, val - r);
if (t1 == 2 && t2 == 1)
return Math.Min(1, val + r);
if (t1 == 2 && t2 == 2)
return val;
if (t1 == 2 && t2 == 3)
return val;
if (t1 == 3 && t2 == 0)
return Math.Max(0, val - r);
if (t1 == 3 && t2 == 1)
return Math.Min(1, val + r);
if (t1 == 3 && t2 == 2)
return val;
return val;
}
int vertexType(double a, double b, double c)
{
double m = Math.Max(a, Math.Max(b, Math.Max(c, q)));
// Vertex Type
KeyValuePair<int, double> type = new KeyValuePair<int, double>(-1, -1);
double temp = (m - a) * (m - b) * (m - c);
if (type.Value < temp)
type = new KeyValuePair<int, double>(0, temp);
temp = a * (m - b) * (m - c);
if (type.Value < temp)
type = new KeyValuePair<int, double>(1, temp);
temp = b * (m - a) * (m - c);
if (type.Value < temp)
type = new KeyValuePair<int, double>(1, temp);
temp = c * (m - a) * (m - b);
if (type.Value < temp)
type = new KeyValuePair<int, double>(1, temp);
temp = a * b * (m - c);
if (type.Value < temp)
type = new KeyValuePair<int, double>(2, temp);
temp = a * c * (m - b);
if (type.Value < temp)
type = new KeyValuePair<int, double>(2, temp);
temp = c * b * (m - a);
if (type.Value < temp)
type = new KeyValuePair<int, double>(2, temp);
temp = a * b * c;
if (type.Value < temp)
type = new KeyValuePair<int, double>(3, temp);
return type.Key;
}
private void label5_Click(object sender, EventArgs e)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment