在Qt里按钮控件默认对应一个on_pushButton_clicked()成员,如果想用点击信号,在代码中实现on_pushButton_clicked()成员即可。
灰度处理、灰度直方图、灰度均衡、梯度锐化、Laplace锐化
void mainwindow::on_pushButton_clicked()//选择文件
{
QString testFileName = QFileDialog::getOpenFileName(this, tr(""), current_qpath_, "files(*)");
srcImg = imread(testFileName.toStdString());
cvtColor(srcImg, grayImg, CV_BGR2GRAY);
Mat temp;
QImage Qtemp;
cvtColor(srcImg, temp, CV_BGR2RGB);//BGR convert to RGB
Qtemp = QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
ui->label->setPixmap(QPixmap::fromImage(Qtemp));
//返回缩放到给定大小定义的矩形的图像副本。
/**
* @brief scaled 返回缩放到给定大小定义的矩形的图像副本。
* @param KeepAspectRatio 图像被缩放到内部尺寸尽可能大的矩形,保留长宽比
* @param SmoothTransformation
*/
Qtemp = Qtemp.scaled(250, 250, Qt::KeepAspectRatio, Qt::SmoothTransformation);
/**
此属性保存标签是否将扩展其内容以填满所有可用空间。
当启用且标签显示像素图时,它将缩放像素图以填充可用空间。
该属性的默认值为false。*/
ui->label->setScaledContents(true);
ui->label->resize(Qtemp.size());
ui->label->show();
}
getOpenFileName()
QString fileName = QFileDialog::getOpenFileName(this, tr(“Open File”),
“/home”,
tr(“Images (*.png *.xpm *.jpg)”));
void mainwindow::on_select_files_clicked()//BGR转灰度
{
//Mat gray;
grayImg.create(srcImg.rows, srcImg.cols, CV_8UC1);
QImage Qtemp;
for (int i = 0; i < srcImg.rows; i++)
for (int j = 0; j < srcImg.cols; j++) {
grayImg.at<uchar>(i, j) = (int)0.11 * srcImg.at<Vec3b>(i, j)[0]
+ 0.59 * srcImg.at<Vec3b>(i, j)[1]
+ 0.3 * srcImg.at<Vec3b>(i, j)[2];
}
Qtemp = QImage((const uchar*)(grayImg.data), grayImg.cols, grayImg.rows, grayImg.cols * grayImg.channels(), QImage::Format_Indexed8);
ui->label_1->setPixmap(QPixmap::fromImage(Qtemp));
Qtemp = Qtemp.scaled(250, 250, Qt::KeepAspectRatio, Qt::SmoothTransformation);
ui->label_1->setScaledContents(true);
ui->label_1->resize(Qtemp.size());
ui->label_1->show();
}
高斯噪声
//为图像添加高斯噪声
Mat addGaussianNoise(Mat& srcImag)
{
Mat dstImage = srcImag.clone();
for (int i = 0; i < dstImage.rows; i++)
{
for (int j = 0; j < dstImage.cols; j++)
{
//添加高斯噪声
dstImage.at<Vec3b>(i, j)[0] = saturate_cast<uchar>(dstImage.at<Vec3b>(i, j)[0] + generateGaussianNoise(2, 0.8) * 32);
dstImage.at<Vec3b>(i, j)[1] = saturate_cast<uchar>(dstImage.at<Vec3b>(i, j)[1] + generateGaussianNoise(2, 0.8) * 32);
dstImage.at<Vec3b>(i, j)[2] = saturate_cast<uchar>(dstImage.at<Vec3b>(i, j)[2] + generateGaussianNoise(2, 0.8) * 32);
}
}
return dstImage;
}
椒盐噪声
Mat addSaltNoise(const Mat srcImage, int n)
{
Mat dstImage = srcImage.clone();
for (int k = 0; k < n; k++)
{
//随机取值行列
int i = rand() % dstImage.rows;
int j = rand() % dstImage.cols;
//图像通道判定
if (dstImage.channels() == 1)
{
dstImage.at<uchar>(i, j) = 255; //盐噪声
}
else
{
dstImage.at<Vec3b>(i, j)[0] = 255;
dstImage.at<Vec3b>(i, j)[1] = 255;
dstImage.at<Vec3b>(i, j)[2] = 255;
}
}
for (int k = 0; k < n; k++)
{
//随机取值行列
int i = rand() % dstImage.rows;
int j = rand() % dstImage.cols;
//图像通道判定
if (dstImage.channels() == 1)
{
dstImage.at<uchar>(i, j) = 0; //椒噪声
}
else
{
dstImage.at<Vec3b>(i, j)[0] = 0;
dstImage.at<Vec3b>(i, j)[1] = 0;
dstImage.at<Vec3b>(i, j)[2] = 0;
}
}
return dstImage;
}
仿射变换
void mainwindow::on_affine_clicked()
{
QImage Qtemp, Qtemp2;
Point2f srcTri[3], dstTri[3];
Mat rot_mat(2, 3, CV_32FC1);
Mat warp_mat(2, 3, CV_32FC1);
Mat dst, RGB;
cvtColor(srcImg, RGB, CV_BGR2RGB);
dst = Mat::zeros(RGB.rows, RGB.cols, RGB.type());
srcTri[0] = Point2f(0, 0);
srcTri[1] = Point2f(RGB.cols - 1, 0); //缩小一个像素
srcTri[2] = Point2f(0, RGB.rows - 1);
dstTri[0] = Point2f(RGB.cols * 0.0, RGB.rows * 0.33);
dstTri[1] = Point2f(RGB.cols * 0.85, RGB.rows * 0.25);
dstTri[2] = Point2f(RGB.cols * 0.15, RGB.rows * 0.7);
/**
* @brief getAffineTransform函数从三对对应点计算仿射变换。该函数计算仿射变换的 2×3 矩
* @param 源图像中三角形顶点的坐标。
* @param 目标图像中相应三角形顶点的坐标。
*/
warp_mat = getAffineTransform(srcTri, dstTri);
/**
* @brief 仿射变换 实现图像的旋转和平移。 类似功能函数M = cv2.getRotationMatrix2D(center, angle, scale)
* @param src_img:原始图像
* @param M:变换矩阵
* @param dsize:输出图像的大小。
*/
warpAffine(RGB, dst, warp_mat, RGB.size());
Qtemp2 = QImage((const unsigned char*)(dst.data), dst.cols, dst.rows, dst.step, QImage::Format_RGB888);
ui->label_3->setPixmap(QPixmap::fromImage(Qtemp2));
Qtemp2 = Qtemp2.scaled(250, 250, Qt::KeepAspectRatio, Qt::SmoothTransformation);
ui->label_3->setScaledContents(true);
ui->label_3->resize(Qtemp2.size());
ui->label_3->show();
}
透视变换
void mainwindow::on_perspective_clicked()
{
QImage Qtemp, Qtemp2;
Point2f srcQuad[4], dstQuad[4];
Mat warp_matrix(3, 3, CV_32FC1);
Mat dst, RGB;
cvtColor(srcImg, RGB, CV_BGR2RGB);
dst = Mat::zeros(RGB.rows, RGB.cols, RGB.type());
srcQuad[0] = Point2f(0, 0); //src top left
srcQuad[1] = Point2f(RGB.cols - 1, 0); //src top right
srcQuad[2] = Point2f(0, RGB.rows - 1); //src bottom left
srcQuad[3] = Point2f(RGB.cols - 1, RGB.rows - 1); //src bot right
dstQuad[0] = Point2f(RGB.cols * 0.05, RGB.rows * 0.33); //dst top left
dstQuad[1] = Point2f(RGB.cols * 0.9, RGB.rows * 0.25); //dst top right
dstQuad[2] = Point2f(RGB.cols * 0.2, RGB.rows * 0.7); //dst bottom left
dstQuad[3] = Point2f(RGB.cols * 0.8, RGB.rows * 0.9); //dst bot right
/**
* @brief CV_EXPORTS_W Mat getPerspectiveTransform(InputArray src, InputArray dst, int solveMethod = DECOMP_LU);
* @param src:源图像中待测矩形的四点坐标
* @param sdt:目标图像中矩形的四点坐标
*/
warp_matrix = getPerspectiveTransform(srcQuad, dstQuad);//warp_matrix 透视变换矩阵
/**
* @brief warpPerspective:对图像进行透视变换,就是变形
* @param InputArray src:输入的图像
* @param OutputArray dst:输出的图像
* @param InputArray M:透视变换的矩阵
* @param Size dsize:输出图像的大小
*/
warpPerspective(RGB, dst, warp_matrix, RGB.size());
Qtemp2 = QImage((const unsigned char*)(dst.data), dst.cols, dst.rows, dst.step, QImage::Format_RGB888);
ui->label_3->setPixmap(QPixmap::fromImage(Qtemp2));
Qtemp2 = Qtemp2.scaled(250, 250, Qt::KeepAspectRatio, Qt::SmoothTransformation);
ui->label_3->setScaledContents(true);
ui->label_3->resize(Qtemp2.size());
ui->label_3->show();
}