วันพฤหัสบดีที่ 12 มกราคม พ.ศ. 2560
วันพฤหัสบดีที่ 5 มกราคม พ.ศ. 2560
C++ OPENCV
Get Pixel Image
========================================================================
#pragma once
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <msclr/marshal.h>
#include <iostream>
using namespace std;
using namespace cv;
namespace HistOpencv {
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;
Mat src; // keep iamge
/// <summary>
/// Summary for MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::PictureBox^ pictureBox1;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::PictureBox^ pictureBox2;
private: System::Windows::Forms::PictureBox^ pictureBox3;
private: System::Windows::Forms::PictureBox^ pictureBox4;
private: System::Windows::Forms::PictureBox^ pictureBox5;
private: System::Windows::Forms::PictureBox^ pictureBox6;
private: System::Windows::Forms::PictureBox^ pictureBox7;
private: System::Windows::Forms::PictureBox^ pictureBox8;
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->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->pictureBox2 = (gcnew System::Windows::Forms::PictureBox());
this->pictureBox3 = (gcnew System::Windows::Forms::PictureBox());
this->pictureBox4 = (gcnew System::Windows::Forms::PictureBox());
this->pictureBox5 = (gcnew System::Windows::Forms::PictureBox());
this->pictureBox6 = (gcnew System::Windows::Forms::PictureBox());
this->pictureBox7 = (gcnew System::Windows::Forms::PictureBox());
this->pictureBox8 = (gcnew System::Windows::Forms::PictureBox());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox2))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox3))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox4))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox5))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox6))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox7))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox8))->BeginInit();
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(12, 29);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"Browse";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(93, 29);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 1;
this->button2->Text = L"Process";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &MyForm::button2_Click);
//
// pictureBox1
//
this->pictureBox1->Location = System::Drawing::Point(12, 78);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(373, 300);
this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage;
this->pictureBox1->TabIndex = 2;
this->pictureBox1->TabStop = false;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(12, 62);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(0, 13);
this->label1->TabIndex = 3;
//
// pictureBox2
//
this->pictureBox2->Location = System::Drawing::Point(391, 78);
this->pictureBox2->Name = L"pictureBox2";
this->pictureBox2->Size = System::Drawing::Size(382, 300);
this->pictureBox2->TabIndex = 4;
this->pictureBox2->TabStop = false;
//
// pictureBox3
//
this->pictureBox3->Location = System::Drawing::Point(779, 78);
this->pictureBox3->Name = L"pictureBox3";
this->pictureBox3->Size = System::Drawing::Size(400, 300);
this->pictureBox3->TabIndex = 5;
this->pictureBox3->TabStop = false;
//
// pictureBox4
//
this->pictureBox4->Location = System::Drawing::Point(391, 384);
this->pictureBox4->Name = L"pictureBox4";
this->pictureBox4->Size = System::Drawing::Size(382, 285);
this->pictureBox4->TabIndex = 6;
this->pictureBox4->TabStop = false;
//
// pictureBox5
//
this->pictureBox5->Location = System::Drawing::Point(779, 384);
this->pictureBox5->Name = L"pictureBox5";
this->pictureBox5->Size = System::Drawing::Size(400, 285);
this->pictureBox5->TabIndex = 7;
this->pictureBox5->TabStop = false;
//
// pictureBox6
//
this->pictureBox6->Location = System::Drawing::Point(15, 401);
this->pictureBox6->Name = L"pictureBox6";
this->pictureBox6->Size = System::Drawing::Size(370, 80);
this->pictureBox6->TabIndex = 8;
this->pictureBox6->TabStop = false;
//
// pictureBox7
//
this->pictureBox7->Location = System::Drawing::Point(15, 487);
this->pictureBox7->Name = L"pictureBox7";
this->pictureBox7->Size = System::Drawing::Size(370, 78);
this->pictureBox7->TabIndex = 9;
this->pictureBox7->TabStop = false;
//
// pictureBox8
//
this->pictureBox8->Location = System::Drawing::Point(15, 571);
this->pictureBox8->Name = L"pictureBox8";
this->pictureBox8->Size = System::Drawing::Size(370, 84);
this->pictureBox8->TabIndex = 10;
this->pictureBox8->TabStop = false;
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(1194, 681);
this->Controls->Add(this->pictureBox8);
this->Controls->Add(this->pictureBox7);
this->Controls->Add(this->pictureBox6);
this->Controls->Add(this->pictureBox5);
this->Controls->Add(this->pictureBox4);
this->Controls->Add(this->pictureBox3);
this->Controls->Add(this->pictureBox2);
this->Controls->Add(this->label1);
this->Controls->Add(this->pictureBox1);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->KeyPreview = true;
this->Name = L"MyForm";
this->Text = L"MyForm";
this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &MyForm::MyForm_KeyDown);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox2))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox3))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox4))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox5))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox6))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox7))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox8))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void MyForm_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e)
{
if (e->KeyCode == Keys::Escape)
{
if (MessageBox::Show("Do you want to exit program", "HistOPENCV", MessageBoxButtons::OKCancel, MessageBoxIcon::Warning) == System::Windows::Forms::DialogResult::OK)
{
Application::Exit();
}
}
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
OpenFileDialog^ open = gcnew OpenFileDialog();
open->Filter = "Image(*.jpg;*.bmp;*.png)|*.jpg;*.bmp;*.png";
if (open->ShowDialog() == System::Windows::Forms::DialogResult::Cancel)
{
return;
}
Bitmap^ bmpSrc = gcnew Bitmap(open->FileName);
pictureBox1->Image = bmpSrc;
pictureBox1->Refresh();
//Load Image into source variable and show as opencv method
src = imread(ConvertString2Char(open->FileName));
label1->Text = open->FileName->ToString();
}
private: char* ConvertString2Char(System::String^ str)
{
char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
return str2;
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
Mat gray, red, blue, green, rorate, binary, adaptivebinary, resizing;
resizing = ToResize(src);
gray = ToGray(resizing);
DrawCVImage(pictureBox2, gray);
binary = ToBinary(gray);
DrawCVImage(pictureBox3, binary);
adaptivebinary = ToAdaptiveBinary(gray);
DrawCVImage(pictureBox4, adaptivebinary);
rorate = ToRorate(resizing, 45, 1);
DrawCVImage(pictureBox5, rorate);
red = ToHistogram(resizing)[0];
green = ToHistogram(resizing)[1];
blue = ToHistogram(resizing)[2];
DrawCVImage(pictureBox6, red);
DrawCVImage(pictureBox7, green);
DrawCVImage(pictureBox8, blue);
}
private: Mat ToResize(Mat src)
{
Mat dst;
resize(src, dst, cv::Size(320, 240), 0, 0, 1);
return dst;
}
private: Mat ToGray(Mat src)
{
Mat dst;
cvtColor(src, dst, CV_RGB2GRAY);
return dst;
}
private: Mat ToBinary(Mat src)
{
Mat dst;
threshold(src, dst, 100, 255, CV_THRESH_BINARY);
return dst;
}
private: Mat ToAdaptiveBinary(Mat src)
{
Mat dst;
adaptiveThreshold(src, dst, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,35, 5);
return dst;
}
private: Mat ToRorate(Mat src,double angle,double scale)
{
Mat dst;
Point2f center(src.cols / 2, src.rows / 2);
Mat mat_rot = getRotationMatrix2D(center, angle, scale);
warpAffine(src, dst, mat_rot, src.size());
return dst;
}
private: vector<Mat> ToHistogram(Mat src)
{
vector<Mat> result;
vector<Mat> img_rgb;
Mat img_r, img_b, img_g;
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);
calcHist(&img_rgb[0], 1, 0, Mat(), img_b, 1, &size_histogram, &hist_range, true, false);
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;
}
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:
break;
}
System::Drawing::RectangleF rect(0, 0, (float)control->Width, (float)control->Height);
graphics->DrawImage(b, rect);
}
};
}
Function ออกจากโปรแกรม
วันพุธที่ 4 มกราคม พ.ศ. 2560
เฉลย C ปิรามิด
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.
สมัครสมาชิก:
บทความ (Atom)


















































