15 лаба 2 сем ит. Элементы линейной фильтрации изображений
Скачать 216.5 Kb.
|
|
Студент гр. 1105 | | Косенко М. Д. |
Преподаватель | | Чиркунова А.А. |
Санкт-Петербург
2022
ЛАБОРАТОРНАЯ РАБОТА № 15
ЭЛЕМЕНТЫ ЛИНЕЙНОЙ ФИЛЬТРАЦИИ ИЗОБРАЖЕНИЙ.
Цель работы: выполнение линейной пространственной фильтрации изображений средствами C++ в среде Visual Studio.
Упражнение 1.
#pragma once
namespace WinFormsTest {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
}
protected:
MyForm()
{
if (components)
{
delete components;
}
}
private: Bitmap^ img1;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::OpenFileDialog^ openFileDialog1;
private: System::Windows::Forms::PictureBox^ pictureBox1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::TextBox^ textBox1;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
this->button2 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
(cli::safe_cast
this->SuspendLayout();
// button1
this->button1->Location = System::Drawing::Point(151, 12);
this->button1->Margin = System::Windows::Forms::Padding(4, 3, 4, 3);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(228, 37);
this->button1->TabIndex = 0;
this->button1->Text = L"Выберите картинку";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
// openFileDialog1
this->openFileDialog1->FileName = L"openFileDialog1";
// pictureBox1
this->pictureBox1->Location = System::Drawing::Point(63, 98);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(128, 128);
this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
this->pictureBox1->TabIndex = 2;
this->pictureBox1->TabStop = false;
// button2
this->button2->Location = System::Drawing::Point(220, 142);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(82, 39);
this->button2->TabIndex = 3;
this->button2->Text = L"--->";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &MyForm::button2_Click);
// textBox1
this->textBox1->Location = System::Drawing::Point(345, 66);
this->textBox1->Multiline = true;
this->textBox1->Name = L"textBox1";
this->textBox1->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
this->textBox1->Size = System::Drawing::Size(171, 405);
this->textBox1->TabIndex = 4;
// MyForm
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(558, 488);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button2);
this->Controls->Add(this->pictureBox1);
this->Controls->Add(this->button1);
this->Margin = System::Windows::Forms::Padding(4, 3, 4, 3);
this->Name = L"MyForm";
this->Text =L"MyForm";
(cli::safe_cast
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^e) {
if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
pictureBox1->Image = Image::FromFile(openFileDialog1->FileName);
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
Bitmap^img1 = gcnew Bitmap(openFileDialog1->FileName);
textBox1->Text = "";
int sum = 0;
int mass[32][32][3];
for (int i = 0; i < img1->Width; i++) {
for (int j = 0; j < img1->Height; j++) {
Color img12 = img1->GetPixel(i, j);
Color img = Color::FromArgb(img12.R, img12.G,img12.B);
mass[i][j][0] = {img.R};
mass[i][j][1] = {img.G};
mass[i][j][2] = {img.B};
textBox1->Text += "Сумма: " + (img.R + img.B + img.G).ToString() + Environment::NewLine;
} }
}
};
}
Упражнение 2 - 3.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^e) {
if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
pictureBox1->Image = Image::FromFile(openFileDialog1->FileName);
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
Bitmap^img1 = gcnew Bitmap(openFileDialog1->FileName);
Bitmap^ img2 = gcnew Bitmap(img1->Width, img1->Height);
int sumR = 0;
int sumG = 0;
int sumB = 0;
int m = 9;
int mass[32][32][3];
for (int i = 0; i < img1->Width; i++) {
int t = 0;
int c = 0;
int s = 2;
int r = 2;
for (int j = 0; j < img1->Height; j++) {
Color img12 = img1->GetPixel(i, j);
Color img = Color::FromArgb(img12.R, img12.G, img12.B);
if (i > 0) { t = -1; }
else { t = 0; }
if (i == img1->Width -1) { s = 0;}
else { s = 2; }
if (j > 0) { c = -1; }
else { c = 0; }
m = 0;
sumR = 0;
sumG = 0;
sumB = 0;
if (j == img1->Height -1) { r = 0; }
else { r = 2; }
for (int k = t; k < s; k++) {
for (int n = c; n < r; n++) {
Color cl1 = img1->GetPixel(i + k, j + n);
sumR += cl1.R;
sumB += cl1.B;
sumG += cl1.G;
if (i == 0 && j == 0) { m = 4; }
else if ((i == 0) || (j == 0)) { m = 6; }
else if ((i == img1->Width - k) || (j == img1->Height - n)) { m = 6; }
else { m = 9; }
}
}
Color cl = Color::FromArgb(sumR / m, sumG / m, sumB / m);
img2->SetPixel(i, j, cl);
}
}
pictureBox2->Image = img2;
int a[3][3];
int b_matrix[3][3] = { {1, 2, 1}, {2, 4, 2}, {1, 2, 1} };
Bitmap^ img3 = gcnew Bitmap(img1->Width, img1->Height);
for (int i = 1; i < img1->Width - 1; i++) {
for (int j = 1; j < img1->Height - 1; j++) {
int sum = 0;
for (int k1 = -1; k1 <= 1; k1++) {
for (int k2 = -1; k2 <= 1; k2++) {
Color cl1 = img1->GetPixel(i + k1, j + k2);
a[k1 + 1][k2 + 1] = b_matrix[k1 + 1][k2 + 1] * cl1.R ;
sum += a[k1 + 1][k2 + 1];
}
}
Color cl2 = Color::FromArgb(int(sum / 16), int(sum / 16), int(sum / 16));
img3->SetPixel(i, j, cl2);
}
}
pictureBox3->Image = img3;
}
};
}
Вывод: При выполнении данной лабораторной работы, я выяснил, что при использовании фильтра скользящего среднего изображение размывается, а при использовании Гауссовой фильтрации изображение тоже размывается, но уменьшается шум и усиливается гладкость.