和心理系在做的实验,主要是研究基于双眼视差的立体视觉在不同的掩蔽类型以及不同视差条件下的去掩蔽效应。基于双眼视差的立体视觉不改变目标与掩蔽刺激之间的信噪比,但能使不同的刺激被知觉在不同的深度位置上以降低目标信号所受到的掩蔽作用。掩蔽作用依据机制不同,可分为能量掩蔽,即不受双眼立体视觉所造成的主观空间分离影响的;以及信息掩蔽,即可以被主观空间分离的。
实验分为几个阶段,最主要是在阶段II被试在存在掩蔽刺激的情况下判断目标图像中的四个字母是否相同。
我主要负责生成实验材料,以下是一些关键代码:
//旋转任意角度随机字母 Mat letterRandRotateMaskerImage(int tag_location) { int target_width=img_width+eye_shift+50; int target_height=img_width+50; Mat masker_img(target_height, target_width,CV_8UC3,Scalar(128,128,128)); Rect roi; roi.y = 0; roi.height = pics[0].rows; int ini_height = 0; int pic_num=pics.size(); int ang_num=angles.size(); while(1) { if((ini_height+pics[0].rows) > target_height) break; //initial roi roi.x = 0; while(1) { roi.y = ini_height; int pic_idx = rand()%pic_num; int rotate_angle = rand()%70; if((pic_idx==8||pic_idx==9)&&rotate_angle>45) rotate_angle=rotate_angle-45; int time_flag=rand()%2; int ver_flag=rand()%2; //rotate picture Mat rota_img = rotateImage(pics[pic_idx], rotate_angle, time_flag,ver_flag); //get roi roi.width = rota_img.cols; roi.height = rota_img.rows; if(roi.x < 0) roi.x = 0; if(roi.y < 0) roi.y = 0; if((roi.x+roi.width) > target_width) break; if((roi.y+roi.height) > target_height) break; Mat sub_img(masker_img,roi); for(int i=0;i<sub_img.rows;i++){ uchar* ptr_rotate=rota_img.ptr<uchar>(i); uchar* ptr_sub=sub_img.ptr<uchar>(i); for(int j=0;j<sub_img.cols;j++){ if( (ptr_rotate[j*3]<180)&&(ptr_rotate[j*3+1]<180) &&((ptr_rotate[j*3+2]<180))){ ptr_sub[j*3]=78; ptr_sub[j*3+1]=78; ptr_sub[j*3+2]=78; } } } roi.x = roi.x + pics[pic_idx].cols+2; } //next y position ini_height += pics[0].rows+2; } //cout<<"masker ok 2"<<endl; int video_width=img_width*2+border_width*4+img_shift; int video_height=img_width+border_width*2; Mat video_img(video_height,video_width,CV_8UC3,Scalar(0,0,0)); roi.width=img_width; roi.height=img_width; roi.x=border_width; roi.y=border_width; Mat left_video(video_img,roi); roi.x=rand()%10; roi.y=rand()%10; Mat right_target(masker_img,roi); roi.x+=eye_shift; Mat left_target(masker_img,roi); roi.x=border_width*3+img_shift+img_width; roi.y=border_width; Mat right_video(video_img,roi); if(tag_location==0){ left_target.copyTo(left_video); right_target.copyTo(right_video); } else if(tag_location==1){ left_target.copyTo(right_video); left_target.copyTo(left_video); } else if(tag_location==2){ left_target.copyTo(right_video); right_target.copyTo(left_video); } int center_x=video_img.rows/2; int center_y=video_img.cols/2; uchar* p_center=video_img.ptr<uchar>(center_x); p_center[center_y*3]=255; p_center[center_y*3+1]=255; p_center[center_y*3+2]=255; return video_img; }
生成图片其实为左右两幅,用立体镜放映可以使被试看到“立体”效果,当右眼图像内容相对左眼是向右“偏移”时,会有远离人眼的效果,即掩蔽刺激在目标刺激之后。
//生成随机目标图像 Mat targetImage(int tag_same) { int id_same,id_dif; if(tag_same){ id_same=rand()%26; id_dif=id_same; }else{ int dif_tmp=rand()%diff_size; id_same=diffs[dif_tmp]; if(dif_tmp%2==0){ id_dif=diffs[dif_tmp+1]; }else{ id_dif=diffs[dif_tmp-1]; } } int id_location=rand()%4; int textWidth=pics[id_same].cols; int textHeight=pics[id_same].rows; int x[4],y[4]; x[0] = img_width/2-textWidth-text_shift; y[0] = img_width/2-textHeight/2; x[1] = img_width/2-textWidth/2; y[1] = img_width/2-textHeight-text_shift; x[2]=img_width/2+text_shift; y[2]=y[0]; x[3]=x[1]; y[3]=img_width/2+text_shift; Mat targetImage(img_width,img_width, CV_8UC1, 255); Mat subImage1(targetImage,Rect(x[0],y[0],textWidth,textHeight)); Mat subImage2(targetImage,Rect(x[1],y[1],textWidth,textHeight)); Mat subImage3(targetImage,Rect(x[2],y[2],textWidth,textHeight)); Mat subImage4(targetImage,Rect(x[3],y[3],textWidth,textHeight)); Mat aGrayImg; cvtColor(pics[id_same], aGrayImg, CV_BGR2GRAY); if(id_location!=0) aGrayImg.copyTo(subImage1); if(id_location!=1) aGrayImg.copyTo(subImage2); if(id_location!=2) aGrayImg.copyTo(subImage3); if(id_location !=3) aGrayImg.copyTo(subImage4); int difTextWidth=pics[id_dif].cols; int difTexttHeight=pics[id_dif].rows; int difX=0; int difY=0; if(id_location==0){ difX = img_width/2-difTextWidth-text_shift; difY = img_width/2-difTexttHeight/2; } else if(id_location==1){ difX= img_width/2-difTextWidth/2; difY = img_width/2-difTexttHeight-text_shift; } else if(id_location==2){ difX=img_width/2+text_shift; difY=img_width/2-difTexttHeight/2; } else{ difX=img_width/2-difTextWidth/2; difY=img_width/2+text_shift; } Mat subDifImage(targetImage,Rect(difX,difY,difTextWidth,difTexttHeight)); Mat bGrayImg; cvtColor(pics[id_dif], bGrayImg, CV_BGR2GRAY); bGrayImg.copyTo(subDifImage); // imshow("target",targetImage); return targetImage; }*生成的目标刺激和掩蔽刺激后面要合成一张图,目标刺激的左右眼图是在同一视差
还有保证各种刺激条件的随机性的代码:
void randVector(vector<int>&same_ids, vector<int>&type_ids, vector<int>&location_ids, int numbers,int tag) { for(int i=0;i<numbers;i++){ same_ids[i]=0; type_ids[i]=0; location_ids[i]=0; } int same_number=numbers/2; //type_ids 固定,same_ids 和 location_ids 随机 if(tag==0){ int same_count=0; while(same_count<same_number){ int rand_id=rand()%numbers; if(same_ids[rand_id]==0){ same_ids[rand_id]=1; ++same_count; } } int same_location_number=same_number/3; for(int s=0;s<2;s++){ for(int t=1;t<3;t++){ int location_count=0; while(location_count<same_location_number){ int rand_id=rand()%numbers; if(same_ids[rand_id]==s&&location_ids[rand_id]==0){ location_ids[rand_id]=t; ++location_count; } } } } } //end of tag==0 //location_ids 固定,type_ids 和 same_ids 随机 else if(tag==1){ int same_count=0; while(same_count<same_number){ int rand_id=rand()%numbers; if(same_ids[rand_id]==0){ same_ids[rand_id]=1; ++same_count; } } int type_numbers=same_number/4; for(int s=0;s<2;s++){ for(int t=1;t<4;t++){ int type_count=0; while(type_count<type_numbers){ int rand_id=rand()%numbers; if(same_ids[rand_id]==s&&type_ids[rand_id]==0){ type_ids[rand_id]=t; ++type_count; } } } } }// end of tag==1 //location_ids ,type_ids 和 same_ids 都随机 else if(tag==2){ int same_count=0; while(same_count<same_number){ int rand_id=rand()%numbers; if(same_ids[rand_id]==0){ same_ids[rand_id]=1; ++same_count; } } int same_location_number=same_number/3; for(int s=0;s<2;s++){ for(int t=1;t<3;t++){ int location_count=0; while(location_count<same_location_number){ int rand_id=rand()%numbers; if(same_ids[rand_id]==s&&location_ids[rand_id]==0){ location_ids[rand_id]=t; ++location_count; } } } } int same_type_number=same_location_number/4; for(int s=0;s<2;s++){ for(int l=0;l<3;l++){ for(int t=1;t<4;t++){ int type_count=0; while(type_count<same_type_number){ int rand_id=rand()%numbers; if(same_ids[rand_id]==s&&location_ids[rand_id]==l&&type_ids[rand_id]==0){ type_ids[rand_id]=t; ++type_count; } } } } } } return ; }
这段代码写得比较笨拙。。。