一、实验目的
掌握类和对象的创建
掌握构造函数、构造函数的重载,拷贝构造函数、析构函数的设计和使用
掌握成员函数的设计和使用
二、实验内容
下面的代码已经创建了图像类的框架,请完善该图像类。在该类中,实现图像的读入、保存、显示,并实现图像的翻转、缩放、裁剪等操作。在主程序中,读入某个图像文件(比如“fruits.bmp”),对其进行缩小,上下翻转,左右翻转,指定区域裁剪等操作。
[使用多文件结构设计该类,即类的声明代码在.h文件中,类的实现代码在.cpp文件中,main函数的代码在另一个.cpp文件中。]
请编程实现:
图像类:
- 创建图像类Image,实现各个重载的构造函数,拷贝构造函数(深拷贝),析构函数。
- 实现对图像的读入,保存,显示。即实现函数ReadBMP,WriteBMP,ReadText,WriteText。类似实验4,从24位BMP图像文件读入时,把3通道转换为1通道,使其成为二维数组。文本文件可以仿照实验3的设计,第一行是图像的行数和列数,后面的每一行存储图像的一行像素值,每个像素的像素值为一个整数。
- 获取图像中某像素点的值。即实现函数At()。
- 将图像中给定像素点的像素值设置为某值,即实现函数Set。将图像所有像素的像素值设定为某值,即实现函数SetAll。
- 同一个函数实现图像的上下翻转、左右翻转。即实现函数Flip。
- 根据指定区域裁剪图像。
- 求图像的均值,方差。
- 图像的旋转,缩放。
- 定义友元函数交换两个Image对象的数据。
在main函数中测试Image类: - 创建Image类对象img。
- 读入文件中的图像“fruits.bmp”。
- 利用Image类的成员函数,对图像进行翻转、旋转。
- 利用Image类的成员函数,将图像长宽缩小到1/2大小;将图像长宽放大2倍。
- 利用拷贝构造函数,创建新的对象new_img。
- 给定的两个点(Point):左上点(top_left)和右下点(bottom_right),将此指定区域内的new_img对象图像进行裁剪操作。
- 求图像的所有像素点的均值和方差,并输出。
- 交换两个Image对象的数据。
以上操作,都需要把结果保存成新的BMP文件以及文本文件,以查看结果是否正确。
三、实验代码
1.提示
Image.h
#ifndef IMAGE_H
#define IMAGE_H
class Image
{
public:
Image(); //无参数的构造函数,创建行列都为零的Image对象
Image(int h, int w); //构造函数重载,创建h行,w列的Image对象
Image(int h, int w, unsigned char val); //构造函数重载,创建的图像像素值都为val;
Image(char* ImageName); //构造函数重载,利用文件名从硬盘加载图像文件成为Image对象;
Image(unsigned char *m, int rows, int cols); //构造函数重载,从一维静态数组创建Image对象,图像的行数和列数由后面两个参数给出;
Image(unsigned char m[][100], int rows); //构造函数重载,从静态二维数组创建Image对象,图像的行数(二维数组的第一个维度)由第二个参数rows给出;
Image(unsigned char **m, int h, int w); //构造函数重载,从动态数组(二级指针)创建Image对象,图像的行数和列数由后面两个参数给出;
Image(const Image &im); //拷贝构造函数;
~Image(); //析构函数;
void ReadBMP(char* filename); //从BMP文件中读入图像数据;
void WriteBMP(char* filename); //将图像数据保存为BMP图像文件;
void ReadText(char* filename); //从文本文件中读入图像数据;
void WriteText(char* filename); //将图像数据保存为文本文件;
unsigned char& At(int row, int col); //获取第row行第col列的像素点的值;
void Set(int row, int col, unsigned char value); //设置像素(row,col)为某值;
void Set(unsigned char value); //设置图像所有像素为同一值;
void Flip(int code); //图像的翻转; 根据code的值:0:左右翻转,1:上下翻转;
void Resize(int code); //图像的缩放;根据code的值:0:缩小一倍,1:放大一倍;
void Crop(int x1, int y1, int x2, int y2);//裁剪点(x1,y1)到点(x2,y2)的图像
void Rotate(int degree);//图像旋转的函数(简单起见,旋转角度为90度的整数倍)
void Mean_Variance(float &m, float &var);//求图像的均值和方差,利用参数输出
friend void Swap(Image &a, Image &b);//使用友元函数交换两个Image对象的数据
private:
unsigned char **data;
int height
1. List item
;
int width;
};
#endif
Image.cpp
#include "Image.h"
//构造函数
Image::Image()
{
//write your code here
}
//构造函数重载
Image::Image(int h, int w)
{
//write your code here
}
// 其他重载构造函数的实现
// ......
//拷贝构造函数
Image::Image(const Image &im)
{
//write your code here
}
//析构函数
Image::~Image()
{
//write your code here
}
//从硬盘读入图像文件;
void Image::ReadBMP(char* filename)
{
}
//保存图像;
void Image::WriteBMP(char *filename)
{
}
//从文本文件读取
void Image::ReadText(char* filename)
{
}
//保存成文本文件
void Image::WriteText(char *filename)
{
}
//获取图像中指定点的值
unsigned char& Image::At(int row, int col)
{
//write your code here
}
//设置图像为同一值
void Image::Set(unsigned char value)
{
//write your code here
}
//false 左右,true 上下;
void Image::Flip(int code)
{
//write your code here
}
//图像缩小,放大
void Image::Resize(int code)
{
//write your code here
}
//图像裁剪的函数
//图像旋转的函数
//write your code here
//实现友元函数,交换两个Image对象的数据
void Swap(Image &a, Image &b)
{
}
CppExp.cpp
#include “Image.h”
int main(int argc, char* argv[])
{
Image img; //创建对象
img.ReadBMP("Fruits.bmp");
img.WriteBMP("FruitsCopy.bmp");
img.WriteText("FruitsCopy.text");
//write your code here
//图像的左右翻转,如img.Flip(true);并保存结果图像文件
//图像的上下翻转,并保存结果图像文件
//图像的缩放,并保存结果图像文件
//获取图像的某点的像素值,并修改, 并保存结果图像文件
//使用拷贝构造函数创建新的对象
Image new_img(img);
//截取指定区域内的图像,并保存结果图像文件
//旋转图像并保存结果图像文件(简单起见,旋转角度为90度的整数倍)
//求图像的均值和方差,并在命令行输出
//交换两个图像的数据
Image img1("Baboon.bmp");
Image img2("Lena.bmp");
Swap(img1, img2);
//保存交换完的结果图像文件
img1.WriteBMP(***);
imag2.WriteBMP(***);
return 0;
}
2.要求
完成上述代码,并能保存成正确的结果图像。
以上均为老师给的实验内容,实现如下。
四、代码实现
Image.h
#pragma once
#ifndef IMAGE_H
#define IMAGE_H
class Image
{
public:
Image(); //无参数的构造函数,创建行列都为零的Image对象
Image(int h, int w); //构造函数重载,创建h行,w列的Image对象
Image(int h, int w, unsigned char val); //构造函数重载,创建的图像像素值都为val;
Image(char* ImageName); //构造函数重载,利用文件名从硬盘加载图像文件成为Image对象;
Image(unsigned char *m, int rows, int cols); //构造函数重载,从一维静态数组创建Image对象,图像的行数和列数由后面两个参数给出;
Image(unsigned char m[][100], int rows); //构造函数重载,从静态二维数组创建Image对象,图像的行数(二维数组的第一个维度)由第二个参数rows给出;
Image(unsigned char **m, int h, int w); //构造函数重载,从动态数组(二级指针)创建Image对象,图像的行数和列数由后面两个参数给出;
Image(const Image &im); //拷贝构造函数;
~Image(); //析构函数;
void ReadBMP(const char* filename); //从BMP文件中读入图像数据;
void WriteBMP(const char* filename);//将图像数据保存为BMP图像文件;
void ReadText(char* filename); //从文本文件中读入图像数据;
void WriteText(const char* filename); //将图像数据保存为文本文件;
unsigned char& At(int row, int col); //获取第row行第col列的像素点的值;
void Set(int row, int col, unsigned char value); //设置像素(row,col)为某值;
void Set(unsigned char value); //设置图像所有像素为同一值;
void Flip(int code); //图像的翻转; 根据code的值:0:左右翻转,1:上下翻转;
void Resize(int code); //图像的缩放;根据code的值:0:缩小一倍,1:放大一倍;
void Crop(int x1, int y1, int x2, int y2);//裁剪点(x1,y1)到点(x2,y2)的图像
void Rotate(int degree);//图像旋转的函数(简单起见,旋转角度为90度的整数倍)
void Mean_Variance(float &m, float &var);//求图像的均值和方差,利用参数输出
friend void Swap(Image &a, Image &b);//使用友元函数交换两个Image对象的数据
private:
unsigned char **data;
int height;
int width;
};
#endif
Image.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "Image.h"
#include <Windows.h>
#include<cstdlib>
#include<cstdio>
#include <iostream>
using namespace std;
//构造函数
Image::Image()
{
//write your code here
height = 0;
width = 0;
data = nullptr;
}
//构造函数重载
Image::Image(int h, int w)//构造函数重载,创建h行,w列的Image对象
{
height = h;
width = w;
//write your code here
data = new unsigned char* [h];
for (int i = 0; i < h; i++)
{
data[i] = new unsigned char[w];
}
}
// 其他重载构造函数的实现
// ......
Image::Image(int h, int w, unsigned char val)//构造函数重载,创建的图像像素值都为val;
{
height = h;
width = w;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
data[i][j] = val;
}
}
}
Image::Image(char* ImageName)//构造函数重载,利用文件名从硬盘加载图像文件成为Image对象;
{
ReadBMP(ImageName);
//cout << "被调用!" << endl;
}
Image::Image(unsigned char* m, int rows, int cols)//构造函数重载,从一维静态数组创建Image对象,图像的行数和列数由后面两个参数给出;
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
int k = 0;
k++;
m[k] = data[i][j];
}
}
}
Image::Image(unsigned char m[][100], int rows) //构造函数重载,从静态二维数组创建Image对象,图像的行数(二维数组的第一个维度)由第二个参数rows给出;
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < 100; j++)
{
m[i][j]=data[i][j];
}
}
}
Image::Image(unsigned char** m, int h, int w) //构造函数重载,从动态数组(二级指针)创建Image对象,图像的行数和列数由后面两个参数给出;
{
m = new unsigned char* [h];
for (int i = 0; i< h; i++)
{
m[i] = new unsigned char[w];
}
for (int i = 0; i < h; i++)
{
for (int j = 0; j <w ; j++)
{
m[i][j] = data[i][j];
}
}
}
//拷贝构造函数
Image::Image(const Image& im)
{
//write your code here
width = im.width;
height = im.height;
data = new unsigned char* [height];
for (int i = 0; i <height; i++)
{
data[i] = new unsigned char[width];
for (int j = 0; j < width; j++)
{
data[i][j] = im.data[i][j];
}
}
cout << "拷贝并创建新图像成功!" << endl;
}
//析构函数
Image::~Image()
{
for (int i = 0; i < height; i++)
{
delete[]data[i];
}
delete[]data;
//write your code here
}
//从硬盘读入图像文件;
void Image::ReadBMP(const char* filename)
{
FILE* fp = fopen(filename, "rb");
if (fp == NULL)
{
cout << "文件打开失败!" << endl;
exit(0);
}
BITMAPFILEHEADER fh;
BITMAPINFOHEADER ih;
fread(&fh, sizeof(BITMAPFILEHEADER), 1, fp);
fread(&ih, sizeof(BITMAPINFOHEADER), 1, fp);
width = ih.biWidth;
height = ih.biHeight;
if (height % 4 != 0)
{
height = (height* (ih.biBitCount) / 8 + 3) / 4 * 4;
height = height / 3;
}
if (width % 4 != 0)
{
width = (width * (ih.biWidth) / 8 + 3) / 4 * 4;
width = width / 3;
}
data=new unsigned char* [height];
for (int i = 0; i < height; i++)
{
data[i] = new unsigned char[width];
}
// fseek(fp, 54, 0);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
unsigned char r, g, b;
fread(&r, sizeof(unsigned char), 1, fp);
fread(&g, sizeof(unsigned char), 1, fp);
fread(&b, sizeof(unsigned char), 1, fp);
data[i][j] = r / 3 + g / 3 + b / 3;
//也可用以下代码
/*unsigned char m[3];
data[i][j] = m[0] / 3 + m[1] / 3 + m[2] / 3;
fread(&data[i][j],1,1,fp);*/
}
}
fclose(fp);
// cout << "文件读取成功!" << endl;
}
//保存图像;
void Image::WriteBMP(const char* filename)
{
FILE* fp = fopen(filename, "wb");
if (fp == NULL)
{
cout << "文件打开失败!" << endl;
exit(0);
}
BITMAPFILEHEADER fh;
BITMAPINFOHEADER ih;
// ih.biSizeImage = 737280;
fh.bfOffBits = 54;
fh.bfReserved1 = 0;
fh.bfReserved2 = 0;
fh.bfSize = 2102;
fh.bfType = 19778;
ih.biBitCount = 24;
ih.biCompression = 0;
ih.biXPelsPerMeter = 0;
ih.biYPelsPerMeter = 0;
ih.biClrImportant = 0;
ih.biSize = 40;
ih.biPlanes = 1;
ih.biClrUsed = 0;
ih.biWidth = width;
ih.biHeight = height;
ih.biSizeImage = ih.biWidth * ih.biHeight * 3;
fwrite(&fh, sizeof(BITMAPFILEHEADER), 1, fp);
fwrite(&ih, sizeof(BITMAPINFOHEADER), 1, fp);
for (int i = 0; i< height; i++)
{
for (int j = 0; j < width; j++)
{
for (int k = 0; k < 3; k++)
{
fwrite(&data[i][j], sizeof(unsigned char), 1, fp);
}
}
}
fclose(fp);
//cout << "文件存入成功!请查看!" << endl;
}
//从文本文件读取
void Image::ReadText(char* filename)
{
FILE* fp;
if ((fp = fopen(filename, "r+")) == NULL)
{
cout << "文件读取失败!" << endl;
exit(0);
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
fread(&data[i][j], sizeof(unsigned char), 1, fp);
}
}
fclose(fp);
}
//保存成文本文件
void Image::WriteText(const char* filename)
{
FILE* fp;
if ((fp = fopen(filename, "w+")) == NULL)
{
cout << "文件读取失败!" << endl;
exit(0);
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
fwrite(&data[i][j], sizeof(unsigned char), 1, fp);
}
}
fclose(fp);
// cout << "文件写入成功!" << endl;
}
//获取图像中指定点的值
unsigned char& Image::At(int row, int col)
{
//write your code here
cout << data[row - 1][col - 1] << endl;
return data[row - 1][col - 1];
}
void Image::Set(int row, int col, unsigned char value)//设置像素(row,col)为某值;
{
data[row - 1][col - 1] = value;
}
//设置图像为同一值
void Image::Set(unsigned char value)
{
//write your code here
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
data[i][j] = value;
}
}
}
//false 左右,true 上下;
void Image::Flip(int code)
{
//write your code here
unsigned char** t;
t = new unsigned char* [height];
for (int j = 0;j < height; j++)
{
t[j] = new unsigned char[width];
}
if (code == 0)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
t[i][j] = data[i][width - 1 - j];
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
data[i][j] = t[i][j];
}
}
cout << "左右翻转成功!请查看!" << endl;
}
else if (code == 1)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
t[i][j] = data[height - 1 - i][j];
}
}
for (int i = 0; i < height; i++)
{
for (int j=0;j<width;j++)
{
data[i][j] = t[i][j];
}
}
cout << "上下翻转成功!请查看!" << endl;
}
for (int i = 0; i < height; i++)
{
delete[]t[i];
}
delete[]t;
}
//图像缩小,放大
void Image::Resize(int code)
{
//write your code here
if (code == 0)
{
unsigned char** t;
t = new unsigned char* [height / 2];
for (int i = 0; i < height / 2; i++)
{
t[i] = new unsigned char[width/2];
}
for (int i = 0; i < height/2;i++ )
{
for (int j = 0; j < width/2; j++)
{
t[i][j] = data[i*2][j*2];
}
}
for (int i = 0; i < height/2;i++)
{
for (int j = 0; j < width/2; j++)
{
data[i][j] = t[i][j];
}
}
for (int i = 0; i < height / 2; i++)
{
delete[]t[i];
}
delete[]t;
width = width / 2;
height = height / 2;
cout << "文件缩小成功!请查看!" << endl;
}
else if(code == 1)
{
unsigned char** t;
t = new unsigned char* [height*2];
for (int i = 0; i < height*2; i++)
{
t[i] = new unsigned char[width*2];
}
for (int i = 0; i < height;i++ )
{
for (int j = 0; j < width;j++ )
{
t[2 * i][2*j] = data[i][j];
t[2 * i][2*j + 1] = data[i][j];
}
//对应奇数行的像素值对于偶数行的像素值
for (int j = 0; j < width*2; j++)
{
t[2 * i + 1][j] = t[2 * i][j];
}
}
data = new unsigned char* [height * 2];
for (int i = 0; i < height * 2; i++)
{
data[i] = new unsigned char[width * 2];
}
for (int i = 0; i < height * 2; i++)
{
for (int j = 0; j < width * 2; j++)
{
data[i][j] = t[i][j];
}
}
for (int i = 0; i < height*2; i++)
{
delete[]t[i];
}
delete[]t;
width = width * 2;
height = height * 2;
cout << "文件放大成功!请查看!" << endl;
}
}
void Image::Crop(int x1, int y1, int x2, int y2)//裁剪点(x1,y1)到点(x2,y2)的图像
{
height = fabs(y2 - y1)+1;
width = fabs(x2 - x1)+1;
unsigned char** t;
t = new unsigned char* [height];
for (int i = 0; i <height; i++)
{
t[i] = new unsigned char[width];
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
t[i][j] = data[y1+i][x1+j];
}
}
data = new unsigned char* [height];
for (int i = 0; i < height; i++)
{
data[i] = new unsigned char[width];
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
data[i][j] = t[i][j];
}
}
for (int i = 0; i < height; i++)
{
delete[]t[i];
}
delete[]t;
cout << "文件剪裁成功!请查看" << endl;
}
void Image::Rotate(int degree)//图像旋转的函数(简单起见,旋转角度为90度的整数倍)
{
int m;
unsigned char** t;
degree = degree / 90;
if (degree == 1)
{
t = new unsigned char* [width];
for (int i = 0; i < width; i++)
{
t[i] = new unsigned char[height];
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
t[j][height - 1 - i] = data[i][j];
}
}
data = new unsigned char* [width];
for (int i = 0; i < width; i++)
{
data[i] = new unsigned char[height];
}
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
data[i][j] = t[i][j];
}
}
m = height;
height = width;
width = m;
for (int i = 0; i < width; i++)
{
delete[]t[i];
}
delete[]t;
}
else if (degree == 2)
{
t = new unsigned char* [height];
for (int i = 0; i < height; i++)
{
t[i] = new unsigned char[width];
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
t[height - 1 - i][j] = data[i][j];
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
data[i][j] = t[i][j];
}
}
for (int i = 0; i < height; i++)
{
delete[]t[i];
}
delete[]t;
}
else if (degree == 3)
{
t = new unsigned char* [width];
for (int i = 0; i < width; i++)
{
t[i] = new unsigned char[height];
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
t[j][i] = data[i][j];
}
}
data = new unsigned char* [width];
for (int i = 0; i < width; i++)
{
data[i] = new unsigned char[height];
}
m = height;
height = width;
width = m;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
data[i][j] = t[i][j];
}
}
for (int i = 0; i < width; i++)
{
delete[]t[i];
}
delete[]t;
}
cout << "文件旋转成功!请查看!" << endl;
}
void Image::Mean_Variance(float& m, float& var)//求图像的均值和方差,利用参数输出
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
m = m + data[i][j];
}
}
m = m / (height * width);
cout << "图像的均值为" << m << endl;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
var = var + (data[i][j] - m) * (data[i][j] - m);
}
}
var = var / (height * width);
cout << "图像的方差为" << var << endl;
}
//实现友元函数,交换两个Image对象的数据
void Swap(Image& a, Image& b)//使用友元函数交换两个Image对象的数据
{
int h, w;
h=a.height;
a.height = b.height;
b.height = h;
w=a.width;
a.width = b.width;
b.width = w;
unsigned char** t=a.data;
a.data = b.data;
b.data = t;
//不需要把data数据挨个交换
}
源.cpp
#include "Image.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
Image img; //创建对象
img.ReadBMP("Fruits.bmp");
img.WriteBMP("FruitsCopy.bmp");
img.WriteText("FruitsCopy.text");
cout << "功能菜单如下:" << endl;
cout << "1.图像左右翻转" << endl;
cout << "2.图像上下翻转" << endl;
cout << "3.缩小图像一倍" << endl;
cout << "4.放大图像一倍" << endl;
cout << "5.修改图像的像素值" << endl;
cout << "6.拷贝并创建新的对象" << endl;
cout << "7.截取图像" << endl;
cout << "8.旋转图像" << endl;
cout << "9.求图像的均值和方差" << endl;
cout << "10.交换图像的数据并保存" << endl;
cout << "请输入序号:" << endl;
int a;
cin >>a;
switch (a)
{
case 1:
{
//write your code here
//图像的左右翻转,如img.Flip(true);并保存结果图像文件
img.Flip(0);
img.WriteBMP("FruitsFlip0.bmp");
break;
}
case 2:
{
//图像的上下翻转,并保存结果图像文件
img.Flip(1);
img.WriteBMP("FruitsFlip1.bmp");
break;
}
case 3:
{
//图像的缩放,并保存结果图像文件
img.Resize(0);
img.WriteBMP("FruitsResize0.bmp");
break;
}
case 4:
{
img.Resize(1);
img.WriteBMP("FruitsResize1.bmp");
break;
}
case 5:
{
//获取图像的某点的像素值,并修改, 并保存结果图像文件
cout << "请输入想要获取像素值的点:" << endl;
int a, b, c;
cin >> a >> b;
img.At(a, b);
cout << "请输入修改值:" << endl;
cin >> c;
img.Set(a, b, c);
img.WriteBMP("FruitSet.bmp");
break;
}
case 6:
{
//使用拷贝构造函数创建新的对象
Image new_img(img);
}
case 7:
{
//截取指定区域内的图像,并保存结果图像文件
int a, b, c, d;
cout << "请输入左上点的坐标:" << endl;
cin >> a >> b;
cout << "请输入右下点的坐标:" << endl;
cin >>c >> d;
img.Crop(a, b, c, d);
img.WriteBMP("FruitCrop.bmp");
break;
}
case 8:
{
//旋转图像并保存结果图像文件(简单起见,旋转角度为90度的整数倍)
cout << "请输入旋转角度:(例:90,180,270)" << endl;
int a;
cin >> a;
img.Rotate(a);
img.WriteBMP("FruitRotate.bmp");
break;
}
case 9:
{
//求图像的均值和方差,并在命令行输出
float m=0;
float var=0;
img.Mean_Variance(m, var);
break;
}
case 10:
{
//交换两个图像的数据
Image img1("Baboon.bmp");
Image img2("Lena.bmp");
Swap(img1,img2);
//保存交换完的结果图像文件
img1.WriteBMP("BaboonSwap.bmp");
img2.WriteBMP("LenaSwap.bmp");
break;
}
default:
cout << "输入错误!" << endl;
break;
}
return 0;
}
备注
实验中涉及到的bmp文件:
构造函数重载中有部分成员函数并未调用,可省去,也可当作数组的练习来完成。以上代码多次用到动态二维数组的创建和删除,可直接在类里写两个成员函数多次调用,这里不再演示。
五、总结
本实验主要是通过对成员数据data这个二维数组操作从而对图像进行操作,同时掌握类的实现也很重要。