1.图像反转
void OpenCVQtGui::Progress_clicked()
{
flip(image, image, 2);//反转函数 0 上下反转;整数,水平发转;负数,水平垂直均反转
QImage img1 = QImage((const unsigned char*)(image.data), image.cols, image.rows, QImage::Format_RGB888);
ui.label_2->setPixmap(QPixmap::fromImage(img1));
ui.label_2->resize(QSize(img1.width(), img1.height()));
}
2.图像增加噪点
void OpenCVQtGui::salt(cv::Mat &image, int n)
{
for (int k=0; k<n; k++)
{
int i = qrand() % image.cols;
int j = qrand() % image.rows;
if (image.channels() == 1)
{
image.at<uchar>(j, i) = 255;
}
else if (image.channels() ==3)
{
image.at<cv::Vec3b>(j, i)[0] = 255;
image.at<cv::Vec3b>(j, i)[1] = 255;
image.at<cv::Vec3b>(j, i)[2] = 255;
}
}
}
3.遍历图像
void OpenCVQtGui::ColorReduce(cv::Mat &image,int div)
{
div = 64;
// int nl = image.rows;//行数
//
// int nc = image.cols*image.channels();
//
// for (int j= 0;j <nl; j++)
// {
// uchar* data = image.ptr<uchar>(j);
//
// for (int i = 0; i < nc; i++)
// {
//
// /*method one*/
// //data[i] = data[i] / div *div + div / 2;
// /*method two*/
// //data[i] = data[i] - data[i] % div + div / 2;
// /*method three*/
// uchar mask = 0xFF << 5;
//
// data[i] = (data[i] & mask) + div / 2;
// }
// }
//
/*iterator method four*/
cv::MatIterator_<cv::Vec3b> it = image.begin<cv::Vec3b>();
cv::Mat_<cv::Vec3b>::iterator itend = image.end<cv::Vec3b>();
for (;it!=itend;++it)
{
(*it)[0] = (*it)[0] / div*div + div / 2;
(*it)[1] = (*it)[1] / div*div + div / 2;
(*it)[2] = (*it)[2] / div*div + div / 2;
}
}
4.图像邻域操作虚化
void OpenCVQtGui::Sharpen(cv::Mat &image, cv::Mat &Result)
{
Result.create(image.size(), image.type()); //分配图像
for (int j=1; j<image.rows-1;j++)
{
const uchar *previous = image.ptr<const uchar>(j - 1); //上一行
const uchar *current = image.ptr<const uchar>(j); //当前行
const uchar *next = image.ptr<const uchar>(j + 1); //下一行
uchar *output = Result.ptr<uchar>(j); //输出行
//for (int i = 1; i< image.cols-1; i++)
for (int i = 1*image.channels(); i< (image.cols - 1)*image.channels(); i++)
{
*output++ = cv::saturate_cast<uchar>(
5*current[i]-current[i-1]-current[i+1]-previous[i]-next[i]);
}
}
Result.row(0).setTo(cv::Scalar(0));
Result.row(Result.rows-1).setTo(cv::Scalar(0));
Result.col(0).setTo(cv::Scalar(0));
Result.col(Result.cols-1).setTo(cv::Scalar(0));
}
5.定义感兴趣区域
void OpenCVQtGui::Merge_clicked()
{
cv::Mat image2 = cv::imread("Girl.jpg"); //Put Girl.jpg into the same pat
cv::Mat Result = image;
cvtColor(Result, Result, CV_BGR2RGB);
cv::Mat imageROI = Result(cv::Rect(0,0,image2.cols,image2.rows));
cv::addWeighted(imageROI, 1.0, image2, 0.3,0., imageROI);
cvtColor(Result, Result, CV_BGR2RGB);
QImage img1 = QImage((const unsigned char*)(Result.data), Result.cols, Result.rows, QImage::Format_RGB888);
ui.label_2->setPixmap(QPixmap::fromImage(img1));
ui.label_2->resize(QSize(img1.width(), img1.height()));
}