19,469
社区成员
发帖
与我相关
我的任务
分享
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
void salt(cv::Mat &image,cv::Mat &resultimg,int n){
resultimg=image;
for(int k=0;k<n;k++){
int i=rand()%resultimg.cols;
int j=rand()%resultimg.rows;
if(resultimg.channels()==1){//graylevel image
resultimg.at<uchar>(j,i)=255;
}else if(resultimg.channels()==3){//color image
resultimg.at<cv::Vec3b>(j,i)[0]=255;
resultimg.at<cv::Vec3b>(j,i)[1]=255;
resultimg.at<cv::Vec3b>(j,i)[2]=255;
}
}
}
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
//隐藏控制台窗口
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
//调用salt.cpp中子函数的方法
void salt(cv::Mat &image,cv::Mat &resultimg,int n);
int main()
{
const char *pstrImageName = "lena.jpg";
const char *pstrSaveImageName = "lena_resize.jpg";
const char *pstrWindowsSrcTitle = "原图 ";
const char *pstrWindowsDstTitle = "缩放图";
//初始化图像对象
cv::Mat image=cv::imread(pstrImageName);
cv::Mat resultimg ;
cv::namedWindow(pstrWindowsSrcTitle);
cv::namedWindow(pstrWindowsDstTitle);
salt(image,resultimg,3000);
cv::imshow("original",image);
cv::imshow("salt result",resultimg);
cv::waitKey(0);
return 1;
}
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
void salt(cv::Mat &image,cv::Mat &resultimg,int n){
image.copyto(resultimg);
for(int k=0;k<n;k++){
int i=rand()%resultimg.cols;
int j=rand()%resultimg.rows;
if(resultimg.channels()==1){//graylevel image
resultimg.at<uchar>(j,i)=255;
}else if(resultimg.channels()==3){//color image
resultimg.at<cv::Vec3b>(j,i)[0]=255;
resultimg.at<cv::Vec3b>(j,i)[1]=255;
resultimg.at<cv::Vec3b>(j,i)[2]=255;
}
}
}
然后调用的主程序main()可以按照预先方式将imshow()放置在一块先后进行
int main()
{
const char *pstrImageName = "lena.jpg";
const char *pstrSaveImageName = "lena_resize.jpg";
const char *pstrWindowsSrcTitle = "原图 ";
const char *pstrWindowsDstTitle = "加噪图";
//初始化图像对象
cv::Mat image,resultimg;
image=cv::imread(pstrImageName);
cv::namedWindow(pstrWindowsSrcTitle);
cv::namedWindow(pstrWindowsDstTitle);
salt(image,resultimg,3000);
cv::imshow("original",image);
cv::imshow("salt result",resultimg);
cv::waitKey(0);
return 1;
}