วันเสาร์ที่ 26 พฤศจิกายน พ.ศ. 2559

C++ UI Canny


private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
{
Mat srcimg;
Mat grayimg;
int thresh = 100;
int max_thresh = 255;

RNG rng(12345);

srcimg = imread("D:\\1.png", IMREAD_COLOR);
if (srcimg.empty())
{
MessageBox::Show("No Image");
Application::Exit();
}
//convert RGB to GRAY
cvtColor(srcimg, grayimg, COLOR_BGR2GRAY);
blur(grayimg, grayimg, cv::Size(3, 3));
//draw source image
DrawCVImage(pictureBox1, srcimg);

Mat canny;
vector<vector<cv::Point>> contours;
vector<Vec4i> heirachy;

Canny(grayimg, canny, trackBar1->Value, max_thresh, 3);
findContours(canny, contours, heirachy, RETR_TREE, CHAIN_APPROX_SIMPLE, cv::Point(0, 0));

Mat drawing = Mat::zeros(canny.size(), CV_8UC3);
for (size_t i = 0; i < contours.size(); i++)
{
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(drawing, contours, (int)i, color, 2, 8, heirachy, 0, cv::Point());
}
//draw Canny Image
DrawCVImage(pictureBox2, drawing);
}
void DrawCVImage(Control^ control, Mat& image)
{
Graphics^ graphic = control->CreateGraphics();
IntPtr ptr(image.ptr());
Bitmap^ b = gcnew Bitmap(image.cols, image.rows, image.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
RectangleF rec(0, 0, control->Width, control->Height);
graphic->DrawImage(b, rec);
}
private: System::Void trackBar1_Scroll(System::Object^  sender, System::EventArgs^  e)
{
button1->PerformClick();
label1->Text = trackBar1->Value.ToString();
}


ไม่มีความคิดเห็น:

แสดงความคิดเห็น