Sei sulla pagina 1di 24

#pragma once

#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>

#include <opencv2/imgcodecs/imgcodecs.hpp>

#include <opencv2/imgproc/imgproc.hpp> // Untuk menambahkan satu lib file

namespace mgfmf {

using namespace System;

using namespace System::ComponentModel;

using namespace System::Collections;

using namespace System::Windows::Forms;

using namespace System::Data;

using namespace System::Drawing;

using namespace System::Runtime::InteropServices;

using namespace cv;

using namespace std;

Mat src; // Need to creat a Mat type for the src image.

/// <summary>

/// Summary for MainForm

/// </summary>

public ref class MainForm : public System::Windows::Forms::Form

public:

MainForm(void)
{

InitializeComponent();

//

//TODO: Add the constructor code here

//

protected:

/// <summary>

/// Clean up any resources being used.

/// </summary>

~MainForm()

if (components)

delete components;

private: System::Windows::Forms::Button^ btnBrowser;

protected:

private: System::Windows::Forms::Button^ btnProcess;

private: System::Windows::Forms::PictureBox^ ptbSource;

private: System::Windows::Forms::Label^ label1;

private: System::Windows::Forms::Label^ label2;


private: System::Windows::Forms::Label^ label3;

private: System::Windows::Forms::PictureBox^ ptbGray;

private: System::Windows::Forms::Label^ label4;

private: System::Windows::Forms::PictureBox^ ptbGaussian;

private: System::Windows::Forms::Label^ label7;

private: System::Windows::Forms::PictureBox^ ptbBinary;

private: System::Windows::Forms::PictureBox^ ptbAdaptive;

private: System::Windows::Forms::Label^ label8;

private: System::Windows::Forms::Label^ label9;

private: System::Windows::Forms::PictureBox^ ptbR;

private: System::Windows::Forms::Label^ label5;

private: System::Windows::Forms::PictureBox^ ptbB;

private: System::Windows::Forms::Label^ label6;

private: System::Windows::Forms::PictureBox^ ptbG;

private: System::Windows::Forms::Label^ label10;

private:

/// <summary>

/// Required designer variable.

/// </summary>

System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

void InitializeComponent(void)

this->btnBrowser = (gcnew System::Windows::Forms::Button());

this->btnProcess = (gcnew System::Windows::Forms::Button());

this->ptbSource = (gcnew System::Windows::Forms::PictureBox());

this->label1 = (gcnew System::Windows::Forms::Label());

this->label2 = (gcnew System::Windows::Forms::Label());

this->label3 = (gcnew System::Windows::Forms::Label());

this->ptbGray = (gcnew System::Windows::Forms::PictureBox());

this->label4 = (gcnew System::Windows::Forms::Label());

this->ptbGaussian = (gcnew System::Windows::Forms::PictureBox());

this->label7 = (gcnew System::Windows::Forms::Label());

this->ptbBinary = (gcnew System::Windows::Forms::PictureBox());

this->ptbAdaptive = (gcnew System::Windows::Forms::PictureBox());

this->label8 = (gcnew System::Windows::Forms::Label());

this->label9 = (gcnew System::Windows::Forms::Label());

this->ptbR = (gcnew System::Windows::Forms::PictureBox());

this->label5 = (gcnew System::Windows::Forms::Label());

this->ptbB = (gcnew System::Windows::Forms::PictureBox());

this->label6 = (gcnew System::Windows::Forms::Label());


this->ptbG = (gcnew System::Windows::Forms::PictureBox());

this->label10 = (gcnew System::Windows::Forms::Label());

(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>ptbSource))->BeginInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>ptbGray))->BeginInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>ptbGaussian))->BeginInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>ptbBinary))->BeginInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>ptbAdaptive))->BeginInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>ptbR))->BeginInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>ptbB))->BeginInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>ptbG))->BeginInit();

this->SuspendLayout();

//

// btnBrowser

//

this->btnBrowser->Location = System::Drawing::Point(12, 12);

this->btnBrowser->Name = L"btnBrowser";

this->btnBrowser->Size = System::Drawing::Size(75, 23);

this->btnBrowser->TabIndex = 0;

this->btnBrowser->Text = L"Browse";

this->btnBrowser->UseVisualStyleBackColor = true;

this->btnBrowser->Click += gcnew System::EventHandler(this,


&MainForm::btnBrowser_Click);

//

// btnProcess

//

this->btnProcess->Location = System::Drawing::Point(104, 12);

this->btnProcess->Name = L"btnProcess";

this->btnProcess->Size = System::Drawing::Size(75, 23);

this->btnProcess->TabIndex = 1;

this->btnProcess->Text = L"Process";

this->btnProcess->UseVisualStyleBackColor = true;

this->btnProcess->Click += gcnew System::EventHandler(this,


&MainForm::btnProcess_Click);

//

// ptbSource

//

this->ptbSource->BorderStyle =
System::Windows::Forms::BorderStyle::FixedSingle;

this->ptbSource->Location = System::Drawing::Point(17, 80);

this->ptbSource->Name = L"ptbSource";

this->ptbSource->Size = System::Drawing::Size(270, 174);

this->ptbSource->SizeMode =
System::Windows::Forms::PictureBoxSizeMode::Zoom;

this->ptbSource->TabIndex = 2;

this->ptbSource->TabStop = false;

//

// label1

//
this->label1->AutoSize = true;

this->label1->Location = System::Drawing::Point(14, 48);

this->label1->Name = L"label1";

this->label1->Size = System::Drawing::Size(73, 13);

this->label1->TabIndex = 3;

this->label1->Text = L"Source Image";

//

// label2

//

this->label2->AutoSize = true;

this->label2->Location = System::Drawing::Point(14, 64);

this->label2->Name = L"label2";

this->label2->Size = System::Drawing::Size(27, 13);

this->label2->TabIndex = 3;

this->label2->Text = L"Size";

//

// label3

//

this->label3->AutoSize = true;

this->label3->Location = System::Drawing::Point(101, 64);

this->label3->Name = L"label3";

this->label3->Size = System::Drawing::Size(27, 13);

this->label3->TabIndex = 3;

this->label3->Text = L"Size";

//
// ptbGray

//

this->ptbGray->BorderStyle =
System::Windows::Forms::BorderStyle::FixedSingle;

this->ptbGray->Location = System::Drawing::Point(308, 80);

this->ptbGray->Name = L"ptbGray";

this->ptbGray->Size = System::Drawing::Size(270, 174);

this->ptbGray->SizeMode =
System::Windows::Forms::PictureBoxSizeMode::Zoom;

this->ptbGray->TabIndex = 2;

this->ptbGray->TabStop = false;

//

// label4

//

this->label4->AutoSize = true;

this->label4->Location = System::Drawing::Point(306, 64);

this->label4->Name = L"label4";

this->label4->Size = System::Drawing::Size(61, 13);

this->label4->TabIndex = 3;

this->label4->Text = L"Gray Image";

//

// ptbGaussian

//

this->ptbGaussian->BorderStyle =
System::Windows::Forms::BorderStyle::FixedSingle;

this->ptbGaussian->Location = System::Drawing::Point(584, 80);

this->ptbGaussian->Name = L"ptbGaussian";
this->ptbGaussian->Size = System::Drawing::Size(270, 174);

this->ptbGaussian->SizeMode =
System::Windows::Forms::PictureBoxSizeMode::Zoom;

this->ptbGaussian->TabIndex = 2;

this->ptbGaussian->TabStop = false;

//

// label7

//

this->label7->AutoSize = true;

this->label7->Location = System::Drawing::Point(582, 64);

this->label7->Name = L"label7";

this->label7->Size = System::Drawing::Size(124, 13);

this->label7->TabIndex = 3;

this->label7->Text = L"90 Degree Rotate Image";

//

// ptbBinary

//

this->ptbBinary->BorderStyle =
System::Windows::Forms::BorderStyle::FixedSingle;

this->ptbBinary->Location = System::Drawing::Point(309, 282);

this->ptbBinary->Name = L"ptbBinary";

this->ptbBinary->Size = System::Drawing::Size(270, 174);

this->ptbBinary->SizeMode =
System::Windows::Forms::PictureBoxSizeMode::Zoom;

this->ptbBinary->TabIndex = 2;

this->ptbBinary->TabStop = false;

//
// ptbAdaptive

//

this->ptbAdaptive->BorderStyle =
System::Windows::Forms::BorderStyle::FixedSingle;

this->ptbAdaptive->Location = System::Drawing::Point(585, 282);

this->ptbAdaptive->Name = L"ptbAdaptive";

this->ptbAdaptive->Size = System::Drawing::Size(270, 174);

this->ptbAdaptive->SizeMode =
System::Windows::Forms::PictureBoxSizeMode::Zoom;

this->ptbAdaptive->TabIndex = 2;

this->ptbAdaptive->TabStop = false;

//

// label8

//

this->label8->AutoSize = true;

this->label8->Location = System::Drawing::Point(306, 266);

this->label8->Name = L"label8";

this->label8->Size = System::Drawing::Size(68, 13);

this->label8->TabIndex = 3;

this->label8->Text = L"Binary Image";

//

// label9

//

this->label9->AutoSize = true;

this->label9->Location = System::Drawing::Point(585, 266);

this->label9->Name = L"label9";
this->label9->Size = System::Drawing::Size(113, 13);

this->label9->TabIndex = 3;

this->label9->Text = L"Adaptive Binary Image";

//

// ptbR

//

this->ptbR->BorderStyle =
System::Windows::Forms::BorderStyle::FixedSingle;

this->ptbR->Location = System::Drawing::Point(17, 282);

this->ptbR->Name = L"ptbR";

this->ptbR->Size = System::Drawing::Size(270, 48);

this->ptbR->SizeMode =
System::Windows::Forms::PictureBoxSizeMode::Zoom;

this->ptbR->TabIndex = 2;

this->ptbR->TabStop = false;

//

// label5

//

this->label5->AutoSize = true;

this->label5->Location = System::Drawing::Point(14, 266);

this->label5->Name = L"label5";

this->label5->Size = System::Drawing::Size(80, 13);

this->label5->TabIndex = 3;

this->label5->Text = L"Histogram RED";

//

// ptbB
//

this->ptbB->BorderStyle =
System::Windows::Forms::BorderStyle::FixedSingle;

this->ptbB->Location = System::Drawing::Point(17, 345);

this->ptbB->Name = L"ptbB";

this->ptbB->Size = System::Drawing::Size(270, 48);

this->ptbB->SizeMode =
System::Windows::Forms::PictureBoxSizeMode::Zoom;

this->ptbB->TabIndex = 2;

this->ptbB->TabStop = false;

//

// label6

//

this->label6->AutoSize = true;

this->label6->Location = System::Drawing::Point(14, 330);

this->label6->Name = L"label6";

this->label6->Size = System::Drawing::Size(85, 13);

this->label6->TabIndex = 3;

this->label6->Text = L"Histogram BLUE";

//

// ptbG

//

this->ptbG->BorderStyle =
System::Windows::Forms::BorderStyle::FixedSingle;

this->ptbG->Location = System::Drawing::Point(17, 409);

this->ptbG->Name = L"ptbG";

this->ptbG->Size = System::Drawing::Size(270, 48);


this->ptbG->SizeMode =
System::Windows::Forms::PictureBoxSizeMode::Zoom;

this->ptbG->TabIndex = 2;

this->ptbG->TabStop = false;

//

// label10

//

this->label10->AutoSize = true;

this->label10->Location = System::Drawing::Point(14, 393);

this->label10->Name = L"label10";

this->label10->Size = System::Drawing::Size(95, 13);

this->label10->TabIndex = 3;

this->label10->Text = L"Histogram GREEN";

//

// MainForm

//

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

this->AutoScaleMode =
System::Windows::Forms::AutoScaleMode::Font;

this->BackColor = System::Drawing::SystemColors::Control;

this->ClientSize = System::Drawing::Size(1210, 469);

this->Controls->Add(this->label3);

this->Controls->Add(this->label2);

this->Controls->Add(this->label9);

this->Controls->Add(this->label7);

this->Controls->Add(this->label10);
this->Controls->Add(this->label6);

this->Controls->Add(this->label5);

this->Controls->Add(this->label8);

this->Controls->Add(this->label4);

this->Controls->Add(this->label1);

this->Controls->Add(this->ptbAdaptive);

this->Controls->Add(this->ptbG);

this->Controls->Add(this->ptbB);

this->Controls->Add(this->ptbR);

this->Controls->Add(this->ptbBinary);

this->Controls->Add(this->ptbGaussian);

this->Controls->Add(this->ptbGray);

this->Controls->Add(this->ptbSource);

this->Controls->Add(this->btnProcess);

this->Controls->Add(this->btnBrowser);

this->Name = L"MainForm";

this->Text = L"MainForm";

(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>ptbSource))->EndInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>ptbGray))->EndInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>ptbGaussian))->EndInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>ptbBinary))->EndInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>ptbAdaptive))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>ptbR))->EndInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>ptbB))->EndInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this-
>ptbG))->EndInit();

this->ResumeLayout(false);

this->PerformLayout();

#pragma endregion

private: System::Void btnBrowser_Click(System::Object^ sender, System::EventArgs^ e)


{

// Load image from PC & show into picture box

OpenFileDialog^ dgOpen = gcnew OpenFileDialog();

dgOpen->Filter = "Image(*.bmp; *.jpg)|*.bmp;*.jpg|All files (*.*)|*.*||";

if (dgOpen->ShowDialog() == System::Windows::Forms::DialogResult::Cancel)

return;

Bitmap^ bmpSrc = gcnew Bitmap(dgOpen->FileName);

ptbSource->Image = bmpSrc;

ptbSource->Refresh();

// load image into src variable and show as OpenCV method.

src = imread(ConvertString2Char(dgOpen->FileName)); // We need to convert


file name to char by Marshal method

//imshow("Source image showing via OpenCV", src);

private: char* ConvertString2Char(System::String^ str) { // Marshal method

char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);

return str2;

private: System::Void btnProcess_Click(System::Object^ sender, System::EventArgs^ e) {

// All image process will deploy after click this button.

Mat gray;

Mat red;

Mat blue;

Mat green;

Mat rorate;

Mat binary;

Mat adaptivebinary;

Mat resizeimg;

Mat gaussianimg, medianimg, Img1 , Img2, Img3;

gaussianimg = GaussianFiltering(src);

medianimg - MedianFiltering(src);

// digunakan untuk mendefenisikan filter dengan matrix

double m [3][3] = { -1, -1, 0,


-1,0,1,

0, 1, 1 };

double m2[3][3] = { 1, 1, 1,

1,-7,1,

1, 1, 1 };

double m3[3][3] = { -1, -1, -1,

-1,8,-1,

-1, -1, -1 };

//

Img1 = Filter(src,m);

Img2 = Filter(src, m2);

Img3 = Filter(src, m3);

resizeimg = ToResize(src);

imshow("resize image", resizeimg); // We need to resize picture before process


to fix show img problem

gaussianimg = GaussianFiltering(resizeimg);

DrawCVImage(ptbGaussian, gaussianimg); // show on picture box

gray = ToGray(resizeimg); // convert to gray

DrawCVImage(ptbGray, gray); // show on picture box

binary = ToBinary(gray);

DrawCVImage(ptbBinary, binary);

adaptivebinary = ToAdaptiveBinary(gray);

DrawCVImage(ptbAdaptive, adaptivebinary);
//Now goto show rorate image

rorate = ToRotate(resizeimg, 45, 1); // Angle = 45 degree and Zoom scale = 1

DrawCVImage(ptbRotate, rorate);

// Finally goto show histogram

red = ToHistogram(resizeimg)[0];

green = ToHistogram(resizeimg)[1];

blue = ToHistogram(resizeimg)[2];

DrawCVImage(ptbR, red);

DrawCVImage(ptbG, green);

DrawCVImage(ptbB, blue);

// To save time I created all "Image processing function" as bellow

private: Mat ToGray(Mat src) { // Convert color image to gray

Mat dst;
cvtColor(src, dst, CV_RGB2GRAY); // You can check all detail method by move
over mouse to the cvtColor method. Same with other methods.

return dst; // after convert return destination image.

private: Mat ToBinary(Mat src) { // convert image to binary image

Mat dst;

threshold(src, dst, 100, 255, CV_THRESH_BINARY); // use Threshold method

return dst;

private: Mat ToAdaptiveBinary(Mat src) {

Mat dst;

adaptiveThreshold(src, dst, 255, CV_ADAPTIVE_THRESH_MEAN_C,


CV_THRESH_BINARY, 35, 5); // use AdaptiveThreshold -> covert quality perfomance much beter.

return dst;

private: Mat ToResize(Mat src) { // Rezise image method

Mat dst;

resize(src, dst, cv::Size(320, 240), 0, 0, 1); // we need to define new size onn
cv:Sizze(width, height)

return dst;

private: Mat ToRotate(Mat src, double angle, double scale) { // Rotate image with center
poit define: Point2f center(src.cols / 2, src.rows / 2);

Mat dst;

Point2f center(src.cols / 2, src.rows / 2);

Mat mat_rot = getRotationMatrix2D(center, angle, scale); // create a marix with


center point, angle & scale as defination

warpAffine(src, dst, mat_rot, src.size());

return dst;

private: cv::Mat GaussianFiltering(Mat inputMat) {

Mat dst, dst1, dst2;

cvtColor(inputMat, dst, CV_64F);

GaussianBlur(dst, dst, cv::Size(9, 9), 2);

convertScaleAbs(dst, dst2);

return dst2;

private: cv::Mat MedianFiltering(Mat inputMat) {

private: cv::Mat Filter(Mat inputMat, double m[3][3]) {

Mat dst, dst2;

Mat M = Mat(3, 3, CV_64F, m);

cv::filter2D(inputMat, dst, inputMat.depth(), M);

convertScaleAbs(dst, dst2);

return dst2;

}
private: vector<Mat> ToHistogram(Mat src) { // This method will draw histogram of 3
colors R,G,B

vector<Mat> result;

vector<Mat> img_rgb;

Mat img_r, img_g, img_b;

int chart_w = 400;

int chart_h = 400;

int size_histogram = 255;

float range[] = { 0, 255 };

const float* hist_range = { range };

split(src, img_rgb); // Devide color image to separate rgb image.

calcHist(&img_rgb[0], 1, 0, Mat(), img_b, 1, &size_histogram, &hist_range, true,


false); // Get the histogram data per color into img_b matrix.

calcHist(&img_rgb[1], 1, 0, Mat(), img_g, 1, &size_histogram, &hist_range, true,


false);

calcHist(&img_rgb[2], 1, 0, Mat(), img_r, 1, &size_histogram, &hist_range, true,


false);

int bin = cvRound((double)chart_w / size_histogram);

// Then we draw histogram here

Mat disp_r(chart_w, chart_h, CV_8UC3, Scalar(255, 255, 255));

Mat disp_g = disp_r.clone();

Mat disp_b = disp_r.clone();


normalize(img_b, img_r, 0, disp_b.rows, NORM_MINMAX, -1, Mat());

normalize(img_g, img_g, 0, disp_b.rows, NORM_MINMAX, -1, Mat());

normalize(img_r, img_b, 0, disp_b.rows, NORM_MINMAX, -1, Mat());

for (int i = 1; i < 255; i++)

line(disp_r, cv::Point(bin*(i), chart_h), cv::Point(bin*(i), chart_h -


cvRound(img_r.at<float>(i))),

Scalar(0, 0, 255), 2, 8, 0);

line(disp_g, cv::Point(bin*(i), chart_h), cv::Point(bin*(i), chart_h -


cvRound(img_g.at<float>(i))),

Scalar(0, 255, 0), 2, 8, 0);

line(disp_b, cv::Point(bin*(i), chart_h), cv::Point(bin*(i), chart_h -


cvRound(img_b.at<float>(i))),

Scalar(255, 0, 0), 2, 8, 0);

// push all 3 colors to the result variable.

result.push_back(disp_r);

result.push_back(disp_g);

result.push_back(disp_b);

return result;

// For the Windows Form picture box, to show Mat image we need
bellow method (This method you can see on OpenCV website)

private: void DrawCVImage(System::Windows::Forms::Control^ control, cv::Mat&


colorImage)

System::Drawing::Graphics^ graphics = control->CreateGraphics();

System::IntPtr ptr(colorImage.ptr());

System::Drawing::Bitmap^ b;

switch (colorImage.type())

case CV_8UC3: // non-grayscale images are correctly displayed here

b = gcnew System::Drawing::Bitmap(colorImage.cols, colorImage.rows,


colorImage.step,

System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);

break;

case CV_8UC1: // grayscale images are incorrectly displayed here

b = gcnew System::Drawing::Bitmap(colorImage.cols, colorImage.rows,


colorImage.step,

System::Drawing::Imaging::PixelFormat::Format8bppIndexed,
ptr);

break;

default:

// error message

break;

System::Drawing::RectangleF rect(0, 0, (float)control->Width, (float)control-


>Height);

graphics->DrawImage(b, rect);

};
}

Potrebbero piacerti anche