计算机视觉
图像处理

【图像处理】双眼去掩蔽

和心理系在做的实验,主要是研究基于双眼视差的立体视觉在不同的掩蔽类型以及不同视差条件下的去掩蔽效应。基于双眼视差的立体视觉不改变目标与掩蔽 刺激之间的信噪比,但能使不同的刺激被知觉在不同的深度位置上以降低目标信号所受到的掩蔽作用。掩蔽作用依据机制不同,可分为能量掩蔽,即不受双眼立体视 觉所造成的主观空间分离影响的;以及信息掩蔽,即可以被主观空间分离的。

实验分为几个阶段,最主要是在阶段II被试在存在掩蔽刺激的情况下判断目标图像中的四个字母是否相同。

我主要负责生成实验材料,以下是一些关键代码:

  1. //旋转任意角度随机字母
  2. Mat letterRandRotateMaskerImage(int tag_location)
  3. {
  4.     int target_width=img_width+eye_shift+50;
  5.     int target_height=img_width+50;
  6.     Mat masker_img(target_height, target_width,CV_8UC3,Scalar(128,128,128));
  7.     Rect roi;
  8.     roi.y = 0;
  9.     roi.height = pics[0].rows;
  10.     int ini_height = 0;
  11.     int pic_num=pics.size();
  12.     int ang_num=angles.size();
  13.     while(1)
  14.     {
  15.         if((ini_height+pics[0].rows) > target_height) break;
  16.         //initial roi
  17.         roi.x = 0;
  18.         while(1)
  19.         {
  20.             roi.y = ini_height;
  21.             int pic_idx = rand()%pic_num;
  22.             int rotate_angle = rand()%70;
  23.             if((pic_idx==8||pic_idx==9)&&rotate_angle>45)
  24.                     rotate_angle=rotate_angle-45;
  25.             int time_flag=rand()%2;
  26.             int ver_flag=rand()%2;
  27.             //rotate picture
  28.             Mat rota_img = rotateImage(pics[pic_idx], rotate_angle, time_flag,ver_flag);
  29.             //get roi
  30.             roi.width = rota_img.cols;
  31.             roi.height = rota_img.rows;
  32.             if(roi.x < 0) roi.x = 0;
  33.             if(roi.y < 0) roi.y = 0;
  34.             if((roi.x+roi.width) > target_width) break;
  35.             if((roi.y+roi.height) > target_height) break;
  36.             Mat sub_img(masker_img,roi);
  37.             for(int i=0;i<sub_img.rows;i++){
  38.                 uchar* ptr_rotate=rota_img.ptr<uchar>(i);
  39.                 uchar* ptr_sub=sub_img.ptr<uchar>(i);
  40.                 for(int j=0;j<sub_img.cols;j++){
  41.                     if( (ptr_rotate[j*3]<180)&&(ptr_rotate[j*3+1]<180) &&((ptr_rotate[j*3+2]<180))){
  42.                         ptr_sub[j*3]=78;
  43.                         ptr_sub[j*3+1]=78;
  44.                         ptr_sub[j*3+2]=78;
  45.                     }
  46.                 }
  47.             }
  48.             roi.x = roi.x + pics[pic_idx].cols+2;
  49.         }
  50.         //next y position
  51.         ini_height += pics[0].rows+2;
  52.     }
  53.     //cout<<“masker ok 2″<<endl;
  54.     int video_width=img_width*2+border_width*4+img_shift;
  55.     int video_height=img_width+border_width*2;
  56.     Mat video_img(video_height,video_width,CV_8UC3,Scalar(0,0,0));
  57.     roi.width=img_width;
  58.     roi.height=img_width;
  59.     roi.x=border_width;
  60.     roi.y=border_width;
  61.     Mat left_video(video_img,roi);
  62.     roi.x=rand()%10;
  63.     roi.y=rand()%10;
  64.     Mat right_target(masker_img,roi);
  65.     roi.x+=eye_shift;
  66.     Mat left_target(masker_img,roi);
  67.     roi.x=border_width*3+img_shift+img_width;
  68.     roi.y=border_width;
  69.     Mat right_video(video_img,roi);
  70.     if(tag_location==0){
  71.         left_target.copyTo(left_video);
  72.         right_target.copyTo(right_video);
  73.     }
  74.     else if(tag_location==1){
  75.         left_target.copyTo(right_video);
  76.         left_target.copyTo(left_video);
  77.     }
  78.     else if(tag_location==2){
  79.         left_target.copyTo(right_video);
  80.         right_target.copyTo(left_video);
  81.     }
  82.     int center_x=video_img.rows/2;
  83.     int center_y=video_img.cols/2;
  84.     uchar* p_center=video_img.ptr<uchar>(center_x);
  85.     p_center[center_y*3]=255;
  86.     p_center[center_y*3+1]=255;
  87.     p_center[center_y*3+2]=255;
  88.     return video_img;
  89. }

这是最主要的掩蔽刺激,任意旋转角度的字母,主要存在信息掩蔽。还有另外几种掩蔽类型:切割后的字母,随机像素,以及随机相位,都是在此种图像处理的结果。

生成图片其实为左右两幅,用立体镜放映可以使被试看到“立体”效果,当右眼图像内容相对左眼是向右“偏移”时,会有远离人眼的效果,即掩蔽刺激在目标刺激之后。

  1. //生成随机目标图像
  2. Mat targetImage(int tag_same)
  3. {
  4.     int id_same,id_dif;
  5.     if(tag_same){
  6.         id_same=rand()%26;
  7.         id_dif=id_same;
  8.     }else{
  9.         int dif_tmp=rand()%diff_size;
  10.         id_same=diffs[dif_tmp];
  11.         if(dif_tmp%2==0){
  12.             id_dif=diffs[dif_tmp+1];
  13.         }else{
  14.             id_dif=diffs[dif_tmp-1];
  15.         }
  16.     }
  17.     int id_location=rand()%4;
  18.     int textWidth=pics[id_same].cols;
  19.     int textHeight=pics[id_same].rows;
  20.     int x[4],y[4];
  21.     x[0] = img_width/2-textWidth-text_shift;
  22.     y[0] = img_width/2-textHeight/2;
  23.     x[1] = img_width/2-textWidth/2;
  24.     y[1] = img_width/2-textHeight-text_shift;
  25.     x[2]=img_width/2+text_shift;
  26.     y[2]=y[0];
  27.     x[3]=x[1];
  28.     y[3]=img_width/2+text_shift;
  29.     Mat targetImage(img_width,img_width, CV_8UC1, 255);
  30.     Mat subImage1(targetImage,Rect(x[0],y[0],textWidth,textHeight));
  31.     Mat subImage2(targetImage,Rect(x[1],y[1],textWidth,textHeight));
  32.     Mat subImage3(targetImage,Rect(x[2],y[2],textWidth,textHeight));
  33.     Mat subImage4(targetImage,Rect(x[3],y[3],textWidth,textHeight));
  34.     Mat aGrayImg;
  35.     cvtColor(pics[id_same], aGrayImg, CV_BGR2GRAY);
  36.     if(id_location!=0)
  37.         aGrayImg.copyTo(subImage1);
  38.     if(id_location!=1)
  39.         aGrayImg.copyTo(subImage2);
  40.     if(id_location!=2)
  41.         aGrayImg.copyTo(subImage3);
  42.     if(id_location !=3)
  43.         aGrayImg.copyTo(subImage4);
  44.     int difTextWidth=pics[id_dif].cols;
  45.     int difTexttHeight=pics[id_dif].rows;
  46.     int difX=0;
  47.     int difY=0;
  48.     if(id_location==0){
  49.         difX = img_width/2-difTextWidth-text_shift;
  50.         difY = img_width/2-difTexttHeight/2;
  51.     }
  52.     else if(id_location==1){
  53.         difX= img_width/2-difTextWidth/2;
  54.         difY = img_width/2-difTexttHeight-text_shift;
  55.     }
  56.     else if(id_location==2){
  57.         difX=img_width/2+text_shift;
  58.         difY=img_width/2-difTexttHeight/2;
  59.     }
  60.     else{
  61.         difX=img_width/2-difTextWidth/2;
  62.         difY=img_width/2+text_shift;
  63.     }
  64.     Mat subDifImage(targetImage,Rect(difX,difY,difTextWidth,difTexttHeight));
  65.     Mat bGrayImg;
  66.     cvtColor(pics[id_dif], bGrayImg, CV_BGR2GRAY);
  67.     bGrayImg.copyTo(subDifImage);
  68.     //  imshow(“target”,targetImage);
  69.     return targetImage;
  70. }

*生成的目标刺激和掩蔽刺激后面要合成一张图,目标刺激的左右眼图是在同一视差

还有保证各种刺激条件的随机性的代码:

  1. void randVector(vector<int>&same_ids, vector<int>&type_ids, vector<int>&location_ids, int numbers,int tag)
  2. {
  3.     for(int i=0;i<numbers;i++){
  4.         same_ids[i]=0;
  5.         type_ids[i]=0;
  6.         location_ids[i]=0;
  7.     }
  8.     int same_number=numbers/2;
  9.     //type_ids 固定,same_ids 和 location_ids 随机
  10.     if(tag==0){
  11.         int same_count=0;
  12.         while(same_count<same_number){
  13.             int rand_id=rand()%numbers;
  14.             if(same_ids[rand_id]==0){
  15.                 same_ids[rand_id]=1;
  16.                 ++same_count;
  17.             }
  18.         }
  19.         int same_location_number=same_number/3;
  20.         for(int s=0;s<2;s++){
  21.             for(int t=1;t<3;t++){
  22.                 int location_count=0;
  23.                 while(location_count<same_location_number){
  24.                     int rand_id=rand()%numbers;
  25.                     if(same_ids[rand_id]==s&&location_ids[rand_id]==0){
  26.                         location_ids[rand_id]=t;
  27.                         ++location_count;
  28.                     }
  29.                 }
  30.             }
  31.         }
  32.     } //end of tag==0
  33.     //location_ids 固定,type_ids 和 same_ids 随机
  34.     else if(tag==1){
  35.         int same_count=0;
  36.         while(same_count<same_number){
  37.             int rand_id=rand()%numbers;
  38.             if(same_ids[rand_id]==0){
  39.                 same_ids[rand_id]=1;
  40.                 ++same_count;
  41.             }
  42.         }
  43.         int type_numbers=same_number/4;
  44.         for(int s=0;s<2;s++){
  45.             for(int t=1;t<4;t++){
  46.                 int type_count=0;
  47.                 while(type_count<type_numbers){
  48.                     int rand_id=rand()%numbers;
  49.                     if(same_ids[rand_id]==s&&type_ids[rand_id]==0){
  50.                         type_ids[rand_id]=t;
  51.                         ++type_count;
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.     }// end of tag==1
  57.     //location_ids ,type_ids 和 same_ids 都随机
  58.     else if(tag==2){
  59.         int same_count=0;
  60.         while(same_count<same_number){
  61.             int rand_id=rand()%numbers;
  62.             if(same_ids[rand_id]==0){
  63.                 same_ids[rand_id]=1;
  64.                 ++same_count;
  65.             }
  66.         }
  67.         int same_location_number=same_number/3;
  68.         for(int s=0;s<2;s++){
  69.             for(int t=1;t<3;t++){
  70.                 int location_count=0;
  71.                 while(location_count<same_location_number){
  72.                     int rand_id=rand()%numbers;
  73.                     if(same_ids[rand_id]==s&&location_ids[rand_id]==0){
  74.                         location_ids[rand_id]=t;
  75.                         ++location_count;
  76.                     }
  77.                 }
  78.             }
  79.         }
  80.         int same_type_number=same_location_number/4;
  81.         for(int s=0;s<2;s++){
  82.             for(int l=0;l<3;l++){
  83.                 for(int t=1;t<4;t++){
  84.                     int type_count=0;
  85.                     while(type_count<same_type_number){
  86.                         int rand_id=rand()%numbers;
  87.                         if(same_ids[rand_id]==s&&location_ids[rand_id]==l&&type_ids[rand_id]==0){
  88.                             type_ids[rand_id]=t;
  89.                             ++type_count;
  90.                         }
  91.                     }
  92.                 }
  93.             }
  94.         }
  95.     }
  96.     return ;
  97. }

这段代码写得比较笨拙。。。

转载注明来源:CV视觉网 » 【图像处理】双眼去掩蔽

分享到:更多 ()
扫描二维码,给作者 打赏
pay_weixinpay_weixin

请选择你看完该文章的感受:

0不错 0超赞 0无聊 0扯淡 0不解 0路过

评论 3

评论前必须登录!