内容为研一的DIP数字图像处理课程作业,记录为主
题意:相机使用bayer filter来进行图像获取,给出一张图像,分别显示拜尔滤镜下的RGB通道的效果,并将RGB通道结果相加显示结果
选取的原图:
得到的RGB通道图和相加后的效果图:
Matlab代码实现:
clc clear img = imread('../images/**改为自己的图片名称和路径**.jpg'); [m,n,k] = size(img); red = zeros(m,n); green = zeros(m,n); blue = zeros(m,n); red_img=zeros(m,n); green_img=zeros(m,n); blue_img=zeros(m,n); result = zeros(m,n,3); for i=1:m %构造bayer滤波器 for j=1:n if((mod(i,2)==1&&mod(j,2)==0)||(mod(i,2)==0&&mod(j,2)==1)) green(i,j)=1; end if(mod(i,2)==1&&mod(j,2)==1) blue(i,j)=1; end if(mod(i,2)==0&&mod(j,2)==0) red(i,j)=1; end end end for i=1:m %计算不同通道的结果 for j=1:n if(red(i,j)==1) red_img(i,j)=img(i,j,1); result(i,j,1)=img(i,j,1); elseif(green(i,j)==1) green_img(i,j)=img(i,j,2); result(i,j,2)=img(i,j,2); else blue_img(i,j)=img(i,j,3); result(i,j,3)=img(i,j,3); end end end % subplot(221); %显示不同通道的结果,对应题目中a的要求 注释掉后面第二问的图片展示并取消此处注释可运行 % imshow(red_img/255); % title('Red'); % subplot(222); % imshow(green_img/255); % title('Green'); % subplot(223); % imshow(blue_img/255); % title('Blue'); % subplot(224); % imshow(result/255); % title('RGB'); new_green_img = green_img; new_red_img = red_img; new_blue_img = blue_img; new_result = zeros(m,n,3); red_img = padarray(red_img,[1,1]); green_img = padarray(green_img,[1,1]); blue_img = padarray(blue_img,[1,1]); for i=1:m %计算插值后的结果 for j=1:n count1 = get_count(green_img,i,j); count2 = get_count(red_img,i,j); count3 = get_count(blue_img,i,j); new_green_img(i,j) = (green_img(i,j)+green_img(i+1,j)+green_img(i+2,j)+green_img(i,j+1)+green_img(i+1,j+1)+green_img(i+2,j+1)+green_img(i,j+2)+green_img(i+1,j+2)+green_img(i+2,j+2))/count1; new_red_img(i,j) = (red_img(i,j)+red_img(i+1,j)+red_img(i+2,j)+red_img(i,j+1)+red_img(i+1,j+1)+red_img(i+2,j+1)+red_img(i,j+2)+red_img(i+1,j+2)+red_img(i+2,j+2))/count2; new_blue_img(i,j) = (blue_img(i,j)+blue_img(i+1,j)+blue_img(i+2,j)+blue_img(i,j+1)+blue_img(i+1,j+1)+blue_img(i+2,j+1)+blue_img(i,j+2)+blue_img(i+1,j+2)+blue_img(i+2,j+2))/count3; end end new_result(:,:,1) = new_red_img(:,:); new_result(:,:,2) = new_green_img(:,:); new_result(:,:,3) = new_blue_img(:,:); subplot(231); %显示插值后的结果 imshow(img); title('Raw imge'); subplot(232); imshow(new_red_img/255); title('interpolation Red'); subplot(233); imshow(new_green_img/255); title('interpolation Green'); subplot(234); imshow(new_blue_img/255); title('interpolation Blue'); subplot(235); imshow(new_result/255); title('interpolation RGB'); function count = get_count(img,i,j) %计算像素不为0的点 count =0; if(img(i,j)~=0) count = count+1; end if(img(i+1,j)~=0) count = count+1; end if(img(i+2,j)~=0) count = count+1; end if(img(i,j+1)~=0) count = count+1; end if(img(i+1,j+1)~=0) count = count+1; end if(img(i+2,j+1)~=0) count = count+1; end if(img(i,j+2)~=0) count = count+1; end if(img(i+1,j+2)~=0) count = count+1; end if(img(i+2,j+2)~=0) count = count+1; end end