zoukankan      html  css  js  c++  java
  • 【OpenCV】图像像素随机化:雪花漫天飞

    近来经常和心理系做实验,总是有各种“什么什么随机化,刺激的物理性质保持一样。。”的需求。之前做《去掩蔽》的实验时,有一套图片就是做的像素随机化,这是最简单的随机化了。当时图像只有两种像素,灰的和深灰的,而且深灰的比较少。


    于是我就统计了深灰像素点的个数,然后在一张同样大的灰色图片中的随机位置洒深灰像素点。

     

    int pix_count=0;
    	for(int i=0;i<img_width+eye_shift;i++){
    		uchar* p=sub_masker.ptr<uchar>(i);
    		for(int j=0;j<img_width+eye_shift;j++){
    			if(p[j*3]==78){
    				pix_count++;
    			}
    		}
    	}
    	//cout<<pix_count<<endl;
    	int pix_width=img_width+eye_shift;
    	while(pix_count>0){
    		int rand_x=rand()%pix_width;
    		int rand_y=rand()%pix_width;
    		uchar* p_pix=pix_masker.ptr<uchar>(rand_x);
    		if(p_pix[rand_y*3]==128){
    			p_pix[rand_y*3]=78;
    			p_pix[rand_y*3+1]=78;
    			p_pix[rand_y*3+2]=78;
    			pix_count--;
    			//	cout<<pix_count<<endl;
    		}
    	}
    大致效果如上图(*忽略几个字母,是叠加了目标刺激的效果)。

    但这次需要对一个刺激视频中每一帧做随机化,且视频是彩色的。

    我一开始的思路是对像素中每一个点,随机找另外一个点交换他们的RGB值。
    		Mat frame_copy(frame_rows,fram_cols, CV_8UC3,Scalar(0,0,0));
    		frame.copyTo(frame_copy);
    		Mat frame_tag=Mat::zeros(frame_rows,fram_cols, CV_8UC1);
    		for(int i=0;i<frame_rows;i++){
    			uchar* p=frame_copy.ptr<uchar>(i);
    			for(int j=0;j<fram_cols;j+=3){
    				uchar r=p[0];
    				uchar b=p[1];
    				uchar g=p[2];
    				int rand_row=rand()%frame_rows;
    				int rand_col=rand()%fram_cols;
    			//	cout<<rand_row<<" "<<rand_col<<endl;
    				uchar* rand_p=frame_copy.ptr<uchar>(rand_row);
    				p[0]=rand_p[rand_col*3+0];
    				p[1]=rand_p[rand_col*3+1];
    				p[2]=rand_p[rand_col*3+2];
    				rand_p[rand_col*3+0]=r;
    				rand_p[rand_col*3+1]=b;
    				rand_p[rand_col*3+2]=g;
    			}
    		}

    但不知为何,实现出来的效果是这样的。。。
    莫不是因为每个点都被随机了,在后面的时候又以一定的概率被随机回去了???

    于是还是改成了原来撒点的思路,对应新建一幅一样大的黑色图,逐点读取原图的像素点,如果遇到非黑色的点,就在新图随机找一个黑色的(也就是未被修改过的点)修改像素值为原图中此点的像素值。
    完整代码:
    int main(){
    	VideoCapture inputVideo("情绪学习.wmv");
    	if ( !inputVideo.isOpened()){
    		cout << "Could not open the input video."  << endl;
    		return -1;
    	}
    	Size S = Size((int) inputVideo.get(CV_CAP_PROP_FRAME_WIDTH), //Acquire input size
    		(int) inputVideo.get(CV_CAP_PROP_FRAME_HEIGHT));
    	int ex = static_cast<int>(inputVideo.get(CV_CAP_PROP_FOURCC)); // Get Codec Type- Int form
    	VideoWriter outputVideo; // Open the output
    	outputVideo.open("ZhongXing.wmv",CV_FOURCC('M','J','P','G'), 30,S, true);
    	srand((unsigned)time(NULL));
    	int frame_count=0;
    	while(true){
    		Mat frame;
    		inputVideo>>frame;
    		if(frame.empty())
    			break;
    		int frame_rows=frame.rows;
    		int fram_cols=frame.cols;
    		Mat frame_copy(frame_rows,fram_cols, CV_8UC3,Scalar(0,0,0));
    		Mat frame_tag=Mat::zeros(frame_rows,fram_cols, CV_8UC1);
    		for(int i=0;i<frame_rows;i++){
    			uchar* p_frame=frame.ptr<uchar>(i);
    			for(int j=0;j<fram_cols;j+=3){
    				uchar r=p_frame[j+0];
    				uchar b=p_frame[j+1];
    				uchar g=p_frame[j+2];
    				if((r>0)||(b>0)||(g>0)){
    					bool if_tag=false;
    					while(!if_tag){
    						int rand_row=rand()%frame_rows;
    						int rand_col=rand()%fram_cols;
    						uchar* p_tag=frame_tag.ptr<uchar>(rand_row);
    						uchar* p_copy=frame_copy.ptr<uchar>(rand_row);
    						if(p_tag[rand_col]==1)
    							continue;
    						else{
    							p_tag[rand_col]=1;
    							p_copy[rand_col*3+0]=r;
    							p_copy[rand_col*3+1]=b;
    							p_copy[rand_col*3+2]=g;
    							if_tag=true;
    						}
    					}
    				}
    			}
    		}
    		cout<<"Write frame: "<<frame_count++<<endl;
    		outputVideo<<frame_copy;
    	}
    	cout<<"Finished Writing"<<endl;
    	return 0;
    }

    然后就实现了漫天飞舞的随机雪花效果啦~





    (转载请注明作者和出处:http://blog.csdn.net/xiaowei_cqu 未经允许请勿用于商业用途)



  • 相关阅读:
    Lucene:(一)建立索引文件:2。建立索引文件(一)
    Lucene:(一)建立索引文件:2。建立索引文件(二)Segment文件
    92.外边距设置 Walker
    99.元素居中及样式重置 Walker
    94.外边距踩坑 Walker
    101.列表属性 Walker
    97.boxsizing属性 Walker
    98.溢出隐藏 Walker
    95.内边距设置 Walker
    96.内边距和边框踩坑 Walker
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3041025.html
Copyright © 2011-2022 走看看