OpenCV中的图像复制机制


本人OpenCV入门,写了一个给图像文件加噪声的子函数salt.cpp,然后再主函数main中调用,用以显示原图像文件和加噪声文件之间的明显对比。但是现在出现了一个问题,如果我先调用salt()函数,然后imshow()显示原图和加噪之后的图片,会发现显示的两幅图片均是加噪后的结果,原图片被修改了。但是我若是先imshow原图片,然后加噪声salt(),此时再用imshow()加噪的图片,才是我想要的结果。其实这对程序本身没什么影响,但是我想知道怎样才能达到如下目的:先调用salt(),然后imshow()原图片和加噪图片,使得原始图像和加噪图像均能正常地显示?(原始图像和加噪图像能正常的对比显示?)

--------------------------------------分割线 --------------------------------------

Ubuntu Linux下安装OpenCV2.4.1所需包

Ubuntu 12.04 安装 OpenCV2.4.2

CentOS下OpenCV无法读取视频文件

Ubuntu 12.04下安装OpenCV 2.4.5总结

Ubuntu 10.04中安装OpenCv2.1九步曲

基于QT和OpenCV的人脸识别系统

--------------------------------------分割线 --------------------------------------

语言叙述不是很清晰:具体代码请大家过目

先是加噪的子函数salt(),独立存储为salt.cpp

#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;
                }
        }
       
}

然后是调用salt()的主函数,存储为ex1.cpp

#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;
}

运行结果

OpenCV中的图像复制机制

是不是我的子函数设计出现了问题?本人cPP基础不好 还请这里的大神多多指教!

后来 经过自己的摸索 发现

更多详情见请继续阅读下一页的精彩内容:

  • 1
  • 2
  • 下一页

相关内容