(TIỂU LUẬN) NHẬP môn THỊ GIÁC máy TÍNH bài tập số 1 giả sử sử dụng camera sau

18 3 0
(TIỂU LUẬN) NHẬP môn THỊ GIÁC máy TÍNH bài tập số 1 giả sử sử dụng camera sau

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

∞ ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH TRƯỜNG ĐẠI HỌC BÁCH KHOA KHOA CƠ KHÍ NHẬP MƠN THỊ GIÁC MÁY TÍNH BÀI TẬP SỐ STT NHĨM : – LỚP: L01 HỌC KÌ 1/2021-2022 Giảng viên : TS.Lê Đức Hạnh Sinh viên thực hiện: N P L H ĐỀ BÀI Giả sử sử dụng camera sau: a Nếu Working Distance Maximum 1129 cần chọn Focus Length ? (2đ) b Camera cần nhìn vùng làm việc 1500x1200mm , Feature cần quan sát có độ lớn 0.5mm Nếu Focus Length 18mm Cần setup working distance ? (2đ) Lập trình UI cho giao diện sử dụng Opencv CLR Gồm: - nút nhấn để mở webcam Hình webcam phải nằm picture box UI (2đ) - nút nhấn để save hình Khi webcam mở lần lược save hình với định dạng: picture_mssv_xx.jpg với xx số thứ tự (2đ) Lập trình mở webcam lưu lại video (15 giây) webcam sử dụng Cmake để compile (2đ) BÀI LÀM Câu a Ta tra thông số datasheet máy ảnh acA2500-14gm-Basler ace có: (Link tra: https://docs.baslerweb.com/c125-0818-5m-p? fbclid=IwAR1llO16StqDsHyNTeP3lbTSs5UMYm8Q8cea-2mO4-D2a_5GGwsDxIpQIQ) Bên cạnh đó, ta có quan hệ hình học thơng số thể qua hình dưới: sin sin → height = 0.744 width Ta lại có: (1) vertical (2) Từ (1) (2), có: width = 868.21mm height = 645.95mm Ta có: distance focal length FOV = sensor size working → → focal length = 7.45mm b Ta có: sensor resolution = sensor size = pixel size sensor resolution = 10.56 (mm) working distance = ( 2.2 2.2 ) 10 −3 ( 6000 4800 ) = 13.20 Câu Đầu tiên ta cần thực quy trình tiền xử lý: (Tham khảo Slide giảng thầy Hạnh) Tải sử dụng Visual Studio 2017 OpenCV 3.2.0 Tạo dự án CLR trống ( CLR empty project) Tiến hành thêm vào (add) liên kết (link) tệp tin, thư viện OpenCV 3.2.0 Thêm vào Project hệ giao diện UI cách add new item vào project sau chọn UI ->Windows Form 5 Kiểm tra dự án sẵn sàng chưa việc thực thi hàm lệnh Ví dụ #include , hàm main(),… ; Nếu có lỗi debug Nếu trơn tru ta sẵn sàng sử dụng Đây quản lý dự án tác giả: Trong tập ta tạo giao diện có cơng cụ là: Một nút để mở webcam Nút nhấn tên button1 ( thay đổi tên nút nhấn tùy ý) Một nút nhấn để lưu hình Nút nhấn tên button2 ( thay đổi tên nút nhấn tùy ý) Một khung ảnh để hiển thị hình ảnh giao diện Khung ảnh tên picturebox1 ( thay đổi tên khung ảnh tùy ý) Sử dụng công cụ toolbox Visual Studio để tiến hành thực hiện, ta trình bày sau: Ta tiến hành thiết lập code cho button1, ta xây dựng định nghĩa thêm hàm để tiến hành vẽ khung hình lên picturebox1 (Tham khảo code thầy Hạnh sách Thị Giác Máy cho Robotic thầy) Ta thu kết quả: Ảnh tác giả chụp khu tự học lầu H3 sở trường Bách Khoa Tiếp theo ta tiến hành thiết lập mã cho nút nhấn 2, ta sử dụng vòng lặp for để thực lần chụp ảnh Đồng thời ta xử lý chuỗi string theo yêu cầu đề để đưa vào hàm imwrite OpenCV Nhắc lại “picture_mssv_xx.jpg” Ta ấn nút nhấn Kiểm tra đường dẫn mà ta thiết lập để lưu: Như ta hoàn thành Câu Vào Visual studio tạo file Cmake: Sau tạo, xuất file gồm file cpp, 1file h file txt: Ta dùng file cpp txt để code Kết quả: Lưu video: Build Cmake dùng Cmake GUI Hàng địa nơi file file cpp txt Hàng thứ nơi ta muốn build Chọn Configue chọn x64, sau ấn Finish Sau chọn Generate Sau vào thư mục build, chọn file sln Chọn BAI3 làm Starup Project Sau chạy chương trình PHỤ LỤC CODE Có tệp thao tác code MyForm.cpp MyForm.h Code tệp MyForm.cpp là: #include "stdafx.h" #include "MyForm.h" using namespace System; using namespace System::Windows::Forms; [STAThreadAttribute] void main() { Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); Project1::MyForm form; Application::Run(% form); } Code tệp MyForm.h là: #pragma once #include #include #include using namespace std; using namespace cv; Mat edges; namespace Project1 { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; / / Summary for MyForm / public ref class MyForm : public System::Windows::Forms::Form { public: MyForm(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: / / Clean up any resources being used / ~MyForm() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::PictureBox^ pictureBox1; private: System::Windows::Forms::Button^ button2; private: System::Windows::Forms::SaveFileDialog^ saveFileDialog1; protected: private: Required designer variable System::ComponentModel::Container ^components; / / / #pragma region Windows Form Designer generated code / / Required method for Designer support - not modify / the contents of this method with the code editor / void InitializeComponent(void) { this->button1 = (gcnew System::Windows::Forms::Button()); this>pictureBox1 = (gcnew System::Windows::Forms::PictureBox()); this->button2 = (gcnew System::Windows::Forms::Button()); this>saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog()); (cli::safe_cast( this>pictureBox1))->BeginInit(); this->SuspendLayout(); // / button1 / this->button1->Location = System::Drawing::Point(10, 19); this->button1>Name = L"button1"; this->button1->Size = System::Drawing::Size(120, 29); this->button1->TabIndex = 0; this->button1>Text = L"button1"; this->button1>UseVisualStyleBackColor = true; this->button1>Click += gcnew System::EventHandler(this, &MyForm::button1_Click); // / pictureBox1 / this->pictureBox1->Location = System::Drawing::Point(12, 67); this->pictureBox1->Name = L"pictureBox1"; this->pictureBox1->Size = System::Drawing::Size(498, 410); this->pictureBox1>TabIndex = 1; this->pictureBox1>TabStop = false; this->pictureBox1>Click += gcnew System::EventHandler(this, &MyForm::pictureBox1_Click); // / button2 / this->button2->Location = System::Drawing::Point(392, 19); this->button2>Name = L"button2"; this->button2->Size = System::Drawing::Size(118, 29); this->button2->TabIndex = 2; this->button2>Text = L"button2"; this->button2>UseVisualStyleBackColor = true; this->button2>Click += gcnew System::EventHandler(this, &MyForm::button2_Click); // / MyForm / this->AutoScaleDimensions = System::Drawing::SizeF(8, 16); this>AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(522, 489); this->Controls->Add(this->button2); this>Controls->Add(this->pictureBox1); this->Controls->Add(this->button1); this->Name = L"MyForm"; this->Text = L"MyForm"; (cli::safe_cast( this>pictureBox1))->EndInit(); this->ResumeLayout(false); } #pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { VideoCapture cap(0); // open the default camera namedWindow("edges", 1); for (;;) { Mat frame; cap >> frame; // get a new frame from camera cvtColor(frame, edges, CV_BGR2BGRA); imshow("edges", edges); DrawCVImage(pictureBox1, edges); waitKey(30); } } 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; b = gcnew System::Drawing::Bitmap(colorImage.cols, colorImage.rows, colorImage.step, System::Drawing::Imaging::PixelFormat::Format32bppRgb, ptr); System::Drawing::RectangleF rect(0, 0, (float)control->Width, (float)control -> Height); graphics->DrawImage(b, rect); } private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { std::string link = "C:/Users/doror/Desktop/Visual Studio SAVE/NMTGMT/Save/"; std::string name = "picture_1910214_"; std::string STT; std::string defined = ".jpg"; std::string path; for (int i = 1; i < 5; i++) { STT = std::to_string(i); path = link + name + STT + defined; imwrite(path, edges); } } private: System::Void pictureBox1_Click(System::Object^ System::EventArgs^ e) { } }; } sender, Có tệp thao tác code BAI3.cpp, Cmakelist.txt, Cmakelist.txt (tên cuối giống nhau, xem hình ba để thấy chi tiết) Code tệp BAI3.cpp #include "BAI3.h" #include #include using namespace std; using namespace cv; int main() { Mat myImage; time_t start, end; time(&start); namedWindow("Video Player"); VideoCapture cap(0); int frame_width = static_cast(cap.get(3)); int frame_height = static_cast(cap.get(4)); Size frame_size(frame_width, frame_height); int fps = 20; VideoWriter output("video.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, frame_size); if (!cap.isOpened()) { cout myImage; output.write(myImage); if (myImage.empty()) { break; } imshow("Video Player", myImage); time(&end); int key = waitKey(20); if (key == 'q') { cout

Ngày đăng: 12/12/2022, 06:54

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan