近來經(jīng)常和心理系做實(shí)驗(yàn),總是有各種“什么什么隨機(jī)化,刺激的物理性質(zhì)保持一樣。。”的需求。之前做《 去掩蔽 》的實(shí)驗(yàn)時(shí),有一套圖片就是做的像素隨機(jī)化,這是最簡單的隨機(jī)化了。當(dāng)時(shí)圖像只有兩種像素,灰的和深灰的,而且深灰的比較少。
于是我就統(tǒng)計(jì)了深灰像素點(diǎn)的個(gè)數(shù),然后在一張同樣大的灰色圖片中的隨機(jī)位置灑深灰像素點(diǎn)。
?
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; } }
?
?


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("情緒學(xué)習(xí).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; }
然后就實(shí)現(xiàn)了漫天飛舞的隨機(jī)雪花效果啦~

(轉(zhuǎn)載請(qǐng)注明作者和出處: http://blog.csdn.net/xiaowei_cqu ?未經(jīng)允許請(qǐng)勿用于商業(yè)用途)
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
