Image processing job (1)

Question 1 Grayscale scanning of black and white images

Implement a function s = scanLine4e(f, I, loc), where f is a grayscale image, I is an integer, and loc is a string. When loc is'row', I represents the number of rows. When loc is'column', I represents the number of columns. The output s is the pixel gray vector of the corresponding row or column.

Call this function to extract the pixel gray vector of the center row and center column of cameraman.tif and einstein.tif and draw the scanned gray sequence into a picture.

1. Procedure

①scanLine4e(f, I, loc) function

function [s] = scanLine4e(f, I, loc)

[row,column]=size(f);

if loc=='row'

    % I 代表行数

    s=f(I,:);

elseif loc=='column'

    % I 代表列数

     s=f(:,I);

end

end

②Main program

img=imread("资源文件\cameraman.tif");

[centerRow,centerColum]=size(img);

centerRow=round(centerRow/2);

centerColum=round(centerColum/2);

cameramanRow=scanLIne4e(img,centerRow,"row")

cameramanColum=scanLIne4e(img,centerColum,"column")

% 画图

x = linspace(0,256, 256); % 横坐标轴

y1 = cameramanRow;

y2 = cameramanColum;

% 绘图

figure

plotyy(x, y1, x,y2)  

scanLIne4e(img,centerRow,"row")

2. Running effect

①cameraman.tif picture

 

Figure 1 Scanning of cameraman.tif image

②einstein.tif picture

Figure 2 Scanning of cameraman.tif image

Question 2 The color image is converted to a black and white image.

A common problem in image processing is to convert a color RGB image into a monochrome grayscale image. The first commonly used method is to take the average of the three elements R, G, and B. The second commonly used method, also known as the NTSC standard, takes into account the human color perception experience. For the R, G, and B channels, different weighting coefficients are used, respectively, R channel 0.2989, G channel 0.5870, and B channel 0.1140 .

Implement a function g = rgb1gray(f, method). The function is to convert a 24-bit RGB image, f, into a grayscale image, g. The parameter method is a string, when its value is'average', The first conversion method is used, and when its value is'NTSC', the second conversion method is used. Use'NTSC' as the default mode.

Call this function to convert the provided images mandril_color.tif and lena512color.tiff into monochrome grayscale images using the above two methods, and briefly compare and discuss the results of the two methods.

1. Code

rgb1gray function

function [outputArg] = rgb1gray(f, method)

%函数g = rgb1gray(f, method).函数功能是将一幅24位的RGB图像,f,转换成灰度图像,g.

%参数 method 是一个字符串,当其值为’average’ 时,采用 第一种转换方法,

%当其值为’NTSC’时,采用第二种转换方法。将’NTSC’做为缺省方式。


%获取3通道

red=f(:,:,1);

green=f(:,:,2);

blue=f(:,:,3);

gray=f;

if method=='average'

    %采用 第一种转换方法:取三个元素R,G,B 的均值

    gray=(red+green+blue)/3;

elseif method=='NTSC'

    gray=0.2989*red+0.5870*green+0.1140*blue

else

    outputArg = "输入参数method错误";

end

outputArg = gray;

end

②The main calling function

img=imread("资源文件\mandril_color.tif");

gray=rgb1gray(img,"average");

imshow(gray)

2. Output result

①mandril_color.tif

 

Figure 3 (left) mandril_color.tif takes the average of 3 channels as the gray scale (right) weighted NTSC mode

②lena512color.tiff

 

Figure 4 (left) lena512color picture takes the average of 3 channels as the gray scale (right) weighted NTSC mode

3. in conclusion

The grayscale image obtained by simply averaging the pixel values ​​of the 3 channels will lose a lot of details, the picture becomes almost invisible, and only a small part of the dense dark area is retained. The grayscale image obtained after using the NTSC weighting mode has the largest weight of 0.5870 due to the G channel, and the green channel can retain more details, so the grayscale image is almost unaffected in detail preservation.

Question 3 Image two-dimensional convolution function

Implement a function g = twodConv(f, w), where f is a grayscale source image and w is a rectangular convolution kernel. The output image g is required to be the same size as the source image f (that is, the number of rows and columns of pixels). Please note that in order to meet this requirement, border pixel padding is required for the source image f. Please implement two solutions here. The first solution is pixel replication, and the corresponding option is defined as'replicate'. The filled pixel copy and its nearest image boundary pixel grayscale. The second scheme is zero-padding, the corresponding option is defined as'zero', and the filled pixel grayscale is 0. Set the second scheme as the default choice.

1. Code

①twodConv函数代码

function [outputArg] = twodConv(f, w,method)

[size_w,t]=size(w)

num=round(size_w/2) -1% 根据卷积核的大小决定要填充多少排

addrow=num*2 %addrow是要添加的圈数,如核大小为3时,添加两圈。核大小为5时,添加4圈

width=size(f, 1)

height=size(f, 2)

if method=='replicate'

   % 增加两圈

    top=f(1:width,1); %提取图像的上下左右像素

    bottom=f(1:width,height);

    left=f(1,1:height);

    right=f(width,1:height)

    % addrow=2

    temp = zeros(addrow*2 +width, addrow*2 + height);

    for i=1:addrow

        temp(addrow+1:width+addrow,i)=top; %赋值

        temp(addrow+1:width+addrow,height+addrow+i)=bottom;

        temp(i,addrow+1:height+addrow)=left;

        temp(width+addrow+i,addrow+1:height+addrow)=right;   

    end

    temp(addrow+1:width+addrow,addrow+1:height+addrow)=f(1:width,1:height) %中间部分保留为原图

    % 图像4个角的赋值

    temp(1:addrow,1:addrow)=f(1,1)

    temp(width+addrow:width+addrow*2,1:addrow)=f(width,1)

    temp(width+addrow:width+addrow*2,height+addrow:height+addrow*2)=f(width,height)

    temp(1:addrow,height+addrow:height+addrow*2)=f(1,height)%temp是复制完像素后得到的图片,核为3*3时,会复制两圈

    P = zeros(addrow*2 +width,addrow + height);

    for i = size_w:(addrow + width) %循环进行卷积操作

        for j = size_w:(addrow + height)

            for m = 1:size_w

                for n = 1:size_w

                    P(i, j) = P(i, j) + temp(i + m-2, j + n-2)*w(m, n);

                end

            end

        end

    end

    P = P(size_w:width + addrow, size_w:height);

elseif method=='zero'

    %像素填补0

    temp = zeros(addrow, addrow*2 +height ); %创建一张temp图像

    temp(size_w:(width +addrow), 1:addrow) = 0;

    temp(size_w:(width + addrow),size_w:(height +addrow)) = f;

    temp(size_w:(width+ addrow),width + size_w:width+ addrow*2) = 0;

    temp(width + size_w:width+ addrow*2, 1:width + addrow*2) = 0;

    P = zeros(addrow*2 +width,addrow + height);

    for i = size_w:(addrow + width)

        for j = size_w:(addrow + height)

            for m = 1:size_w

                for n = 1:size_w

                    P(i, j) = P(i, j) + temp(i + m-2, j + n-2)*w(m, n);

                end

            end

        end

    end

    P = P(size_w:width + addrow, size_w:height);

end

outputArg = P;

end

②主程序代码

img=imread("资源文件\cameraman.tif");

w=[0 0 1 0 0;

   0 0 1 0 0;

   1 1 -4 1 1;

   0 0 1 0 0;

   0 0 1 0 0];

% w=[0 1 0;

%    1 -4 1;

%    0 1 0];

convimg=twodConv(img, w,"zero");

% convimg2=convn(img,w);

% convimg2=conv2(img,w);

convimg=uint8(convimg)

imshow(convimg)

2. Experimental results

① Convolution of the edge pixels of the copied image

Figure 5 (Left) When the convolution kernel size is 3 (Right) When the convolution kernel size is 5

②Fill 0

The convolution effect of the 3*3 convolution kernel is compared with the conv2 function that comes with matlab.

Figure 6 (Left) Convolution filled with 0 when the convolution kernel size is 3 (Right) uses conv2

The convolution effect of the 5-dimensional convolution kernel is compared with the conv2 function that comes with matlab.

Figure 7 (Left) Convolution filled with 0 when the convolution kernel size is 5 (Right) uses conv2

Question 4 Normalized two-dimensional Gaussian filter kernel function

Realize a Gaussian filter kernel function w = gaussKernel(sig, m), where sig corresponds to σ in the definition of Gaussian function, and the size of w is m×m. Please note that if m is not provided here, it needs to be calculated and determined. If m has been provided but is too small, a warning message should be given. w requires normalization, that is, the sum of all elements is 1

The two-dimensional Gaussian filter kernel function is as follows, where σ x is equal to σ y .

 

1 . Code

function [outputArg] = gaussKernel(sig,m)

%高斯滤波核函数 w = gaussKernel(sig,m),

%其中 sig 对应于高斯函数定义中的σ,w 的大小为 m×m。

%请注意,这里如果m没有提供,需要进行计算确定。

%如果m已提供但过小, 应给出警告信息提示。

%w要求归一化,即全部元素加起来和为1

if isempty(m)

    %m为空,计算其大小

    m=1+2*ceil(3*sig);   %若m为空,则定义滤波窗口的大小

elseif m<2

    outputArg='m(窗口大小)过小,请重新输入参数!'

else

    %sig定义高斯函数的标准差      

    nCenter =floor(m/2);                   %定义滤波窗口中心的索引 

    % w 大小为m*m

    w=zeros(m,m)

    for i=-1*nCenter:nCenter

        for j=-1*nCenter:nCenter

            w(i+1+nCenter,j+1+nCenter)=exp(-1*((i)^2+(j)^2)/(2*sig^2))/(2*pi*sig^2);

        end

    end

    %归一化

    dSum=sum(sum(w));

    w=w/dSum;

    outputArg = w;

end

end

2. Experimental results

When σ=1 and the window size is 3*3, the Gaussian kernel obtained is as follows:

Question 5 Gaussian filtering of grayscale images

Call the function implemented above to perform Gaussian filtering on the grayscale images in questions 1 and 2 (cameraman, einstein, and the NTSC converted grayscale images corresponding to lena512color and mandril_color), using σ=1, 2, 3, 5. Choose a pixel padding scheme.

For the result under σ=1, compare with the result of directly calling the related function (you can simply calculate the difference image). Then, choose two images to compare the difference between the pixel copy and zero-fill filter results on the boundary under the condition that other parameters are unchanged.

1. Code

% 第5题

%问题1和2中的灰度图像

img1=imread("资源文件\cameraman.tif");%einstein.tif

%对应NTSC转化后的灰度图像

img2=imread("资源文件\mandril_color.tif");%lena512color.tiff

gray=rgb1gray(img2,"NTSC");

sigma=1 %2,3,5

w=gaussKernel(sigma,3);

%卷积

result=twodConv(img1, w,"replicate");%复制像素填充

result=uint8(result)

imshow(result)

2. Experimental results

① When σ=1,2,3,5, the filtering effect of cameraman.tif picture is shown in Figure 9.

 

 

Figure 8 The filter effect when cameraman.tif window size is 3 and σ is 1, 2, 3, and 5 respectively

 

Figure 9 (left) σ is 2, the window size is 5, (right) σ is 2, and the window size is 7.

When the window size is 3 and σ=1,2,3,5, the filtering effect of einstein.tif is shown in Figure 10.

Figure 10 The filter effect when the window size is 3 and σ is 1, 2, 3, and 5 respectively

When the window size is 5 and σ=1,2,3,5, the filtering effect of the mandril_color.tif image after NTSC grayscale processing is shown in Figure 11.

 

 

 

Figure 11 Mandril_color.tif gray-scale image filtering when the window size is 5 and σ=1,2,3,5

 

When the window size is 7, σ=1,2,3,5, the lena512color.tiff image filtering effect after NTSC grayscale processing is shown in Figure 12.

 

 

Figure 12 lena512color.tiff gray-scale image filtering when the window size is 5 and σ=1,2,3,5

When the window size is 7 and σ=1, the Gaussian filter correlation function is directly called, the code is as follows:

%直接调用相关函数

img2=imread("资源文件\lena512color.tiff");%lena512color.tiff

img2=rgb1gray(img2,"NTSC");

ksize=[7,7]

sigma=1

filter=fspecial('gaussian',ksize, sigma);   % 构建高斯函数

result2=imfilter(img2, filter, 'replicate');

result2

subimg=result2-result1

subimg=uint8(subimg)

imshow(subimg)

The image obtained by comparing the two images and doing the difference is shown in Figure 13.

(A) Manually written Gaussian filter effect (b) Call the built-in function to get the effect

(C) Make the difference between the two pictures to get the result

Figure 13 The window size is 7, the function realized by σ=1 is compared with the function provided by matlab

Using the image cameraman.tif, with the other parameters unchanged, the difference between the pixel copy and zero-padded filtering results on the boundary is shown in Figure 14. It can be seen that the filtered picture will have a black border with 0 filling .

Figure 14 (left) copy pixels (right) fill 0

Cameraman image pixel copy and zero padding sub-filter difference

After replacing a green pepper image, it is found that there is no black border of the cameraman image, and it is difficult to observe the difference with the naked eye, as shown in Figure 15.

Figure 15 (left) copy pixels (right) fill 0

However, looking at the edge pixels of the two images as a whole, the image obtained by the replicate method has a larger edge gray value, and the gray value obtained by the 0-filling method is smaller, as shown in Figure 16.

Figure 16 (top) copy mode (bottom) fill 0 mode green pepper image edge pixel comparison

 

Guess you like

Origin blog.csdn.net/Toky_min/article/details/108929552