图像处理学习笔记(一)


1.1 基础的图像处理

1.1.1 函数列表

  • cat
    C = cat(dim,A1,A2,…,An) concatenates A1, A2, … , An along dimension dim.You can use the square bracket operator [] to concatenate. For example, [A,B] or [A B] concatenates arrays A and B horizontally, and [A; B] concatenates them vertically.

1.1.2 代码及仿真结果

1.	im = imread('apples.jpg');  
2.	imr = im(:,:,1); % get red channel  
3.	img = im(:,:,2); % get green channel  
4.	imb = im(:,:,3); % get blue channel  
5.	% crop 50 x 50 pixel region starting at (100,120)  
6.	im_crop = im(100:150,120:170,:);  
7.	figure  
8.	imshow(im_crop);  

在这里插入图片描述

截取部分像素点
9.	% swap the red and blue image channels  
10.	im2 = cat(3,im(:,:,3),im(:,:,2),im(:,:,1));  
11.	figure  
12.	imshow(im2);  

在这里插入图片描述

交换RGB通道的颜色,等效于反色
13.	% sub-sample every 2nd pixel in vertical direction and 5rd pixel in horizontal direction  
14.	im_subsamp = im(1:2:end,1:5:end,:);  
15.	figure  
16.	imshow(im_subsamp);  

在这里插入图片描述

等间隔抽样,等效于压缩
17.	im_flipped = im(end:-1:1,:,:); % flip image (vertical)  
18.	figure  
19.	imshow(im_flipped);  

在这里插入图片描述

对第一维度(行)进行翻转,等效于垂直翻转

1.2 图像展示

1.2.1 函数列表

  • imshow(f, [low high])
    all values in the image which are less than or equal to low are displayed as black, and all values greater than or equal to high are displayed as white. Values in between are displayed as intermediate intensity values using the default number of levels.

  • impixelinfo
    create a Pixel Information tool,(x coordinate,y coordinate) pixel value

  • imdistline(gca,[a b],[c d])
    sets the first endpoint at the (x,y) coordinate (a, c) and the second endpoint at the coordinate (b, d).
     Impixelinfo

  • impixelinfo
    create a Pixel Information tool,(x coordinate,y coordinate) pixel value , always stands after function imshow()

  • h = imdistline(gca,[a b],[c d])
    sets the first endpoint at the (x,y) coordinate (a, c) and the second endpoint at the coordinate (b, d)

1.2.2 代码及运行结果

1.	clear all;close all;  
2.	f = imread('apples.jpg');  
3.	imshow(rgb2gray(f), [50 150])   
4.	% works even if all 3 channels are not identical, but then the original image is not greyscale  
5.	% imshow with a RGB image appears to ignore the 'DisplayRange' argument.  
6.	% imshow(f)  
7.	impixelinfo;  
8.	%create a Pixel Information tool,(x coordinate,y coordinate) pixel value  
9.	h = imdistline(gca,[1 100],[400 400])  
10.	% h = imdistline(gca,[a b],[c d])  
11.	% sets the first endpoint at the (x,y) coordinate (a, c) and the second endpoint at the coordinate (b, d).  
12.	% also can use imdistline directly;  

在这里插入图片描述

1.3 写入图像及压缩

1.	f = imread('apples.jpg');  
2.	imwrite(f,'C:\Users\yf\Documents\course\图像处理\week1\apples_copy.jpg','quality',20)  
3.	% K = imfinfo('apples_copy.jpg')  
4.	% imwrite(f,filename,'quality',q),q is an integer between 0 and 100 whicn indicate compression degree  

在这里插入图片描述

apples_copy.jpg

1.4 图像数据类型及转换

1.4.1 函数列表
在这里插入图片描述

1.4.2 数据类型解析

matlab中读取图片后保存的数据是uint8类型(8位无符号整数,即1个字节),以此方式存储的图像称作8位图像,好处相比较默认matlab数据类型双精度浮点double(64位,8个字节),自然可以节省很大一部分存储空间。

详细来说imread把灰度图像存入一个8位矩阵,当为RGB图像时,就存入8位RGB矩阵中。例如,彩色图像像素大小是400*300( 高 * 宽 ),则保存的数据矩阵为400*300*3,其中每个颜色通道值是处于0~255之间。

但是虽然matlab中读入图像的数据类型是uint8,而在图像矩阵运算的时候,使用的数据类型却是double类型。一是为了保证精度,二是因为如果不转换,在对uint8进行加减时会产生溢出。

matlab读入图像的数据是uint8,而matlab中数值一般采用double型(64位)存储和运算。所以要先将图像转为double格式的才能运算 1. I1 = im2double(img); % 把图像转换成double精度类型(0~1) 2. I2 = double(img)/255; % uint8转换成double,作用同im2double 补充说明一下,im2double( )和double( )的区别。double( img)就是简单的数据类型转换,将无符号整型转换为双精度浮点型double,但是数据大小没有变化,原本数据是0~255之间,转化后还是0~255。例如原来是255,那么转换后为255.0,小数位0个数是由double数据长度决定,实际数据大小还是255,只不过这个255已经是double类型空间存储了,再增加不会发生溢出情况。而im2double(img)则不仅仅是将uint8转换到double类型,而且把数据大小从0~255映射到0~1区间。 ## 1.5 Image histograms 1.函数列表
  • Z = imlincomb(K1,A1,K2,A2,…,Kn,An)
    computes the linear combination of images, A1, A2, … , An, with weights K1, K2, … , Kn according to: Z = K1A1 + K2A2 + … + Kn*An
  • imhist(im_gray)
    plot a histogram for a single channel.

2.代码及运行结果

1.	clear all;close all;  
2.	im = imread('apples.jpg');  
3.	im_gray = imlincomb(0.1,im(:,:,1), 0.1,im(:,:,2), 0.8,im(:,:,3));  
4.	% Z = imlincomb(K1,A1,K2,A2,...,Kn,An)  
5.	% computes the linear combination of images, A1, A2,, An, with weights K1, K2,, Kn according to:  
6.	% Z = K1*A1 + K2*A2 + ... + Kn*An  
7.	imhist(im_gray)  

在这里插入图片描述

8.	im = imread('apples.jpg');  
9.	figure;  
10.	subplot(3,1,1)  
11.	imhist(im(:,:,1)) % red channel  
12.	ylabel('red')  
13.	subplot(3,1,2)  
14.	imhist(im(:,:,2)) % green channel  
15.	ylabel('green')  
16.	subplot(3,1,3)  
17.	imhist(im(:,:,3)) % blue channel  
18.	ylabel('blue')  

在这里插入图片描述

Tutorial Activity 1: Basic Image Operations

1. 思路分析:
题目要求:
a) 给棋盘分成均匀的8×8块区域,对应的行、列取值范围皆为0到7间的整数(包括端点值)
b) 判断某块棋盘内的棋子是黑棋、白棋,或是空白。
c) 画出对应棋盘的棋子
在这里插入图片描述

为了判断棋盘内是否有棋子,我们利用当棋盘内所有像素点的灰度值与方格左上角第一个像素点的灰度值相同时,该棋盘内没有棋子,否则有棋子。

当方格内有棋子时,为了判断其为黑棋抑或是白棋,我们以一个方格内白色像素点(灰度为255)占总像素点的百分比为评判标准,称为white_pixel,经过计算发现,当white_pixel大于85%时,棋子判别的准确率达到100%。

2. 代码与解析如下所示:
子函数board_position(row,col)

1.	function im = board_position(row,col)  
2.	    % Load Image  
3.	    im = imread('chess.png');  
4.	    % Obtain the size of the image  
5.	    [rows, columns] = size(im);  
6.	      
7.	    % Calculate the board size   
8.	    board_size = rows/8;  
9.	    %divide the board into 64 area
10.	      
11.	    % Calculate the require position  
12.	    row_start = row * board_size + 1;  
13.	    col_start = col * board_size + 1;  
14.	      
15.	    row_end = row_start + board_size - 1;  
16.	    col_end = col_start + board_size - 1;  
17.	      
18.	    % Crop region starting at (100,120)  
19.	    im_crop = im(row_start : row_end,col_start:col_end,:);  
20.	   
21.	 	   % convert unit8 into double to do matrix operation.
22.	    im_gray = rgb2gray(im_crop);  
23.	    % 在原棋盘上显示选中的区域  
24.	    sig = zeros(size(im));  
25.	    sig(row_start : row_end,col_start:col_end,:,1) = 255;  
26.	    sig = uint8(sig);
27.	    %虽然上面赋值是在0~255,但在设sig数组时的默认类型为double,所以要用uint8函数转为uint8type  
28.	    pos = imlincomb(0.5,im,0.5,sig,'uint8');  
29.	    figure  
30.	    imshow(pos)  
31.	    xlabel('column')  
32.	    ylabel('row')  
33.	    impixelinfo;  
34.	    % Detemien how many zero(white) values in the image  
35.	    n = nnz(im_gray)  
36.	    % returns the number of nonzero elements in matrix X.  
37.	    m = numel(im_gray)  
38.	    % returns the number of elements, n, in array A, equivalent to prod(size(A)).  
39.	      
40.	    if all(im_gray(:) == im_gray(1))  
41.	        disp('Empty board');  
42.	    else  
43.	        white_pixel = (n/m) * 100%value in (0,1]  
44.	          
45.	        if white_pixel > 85  
46.	            disp('white piece');%当非白区占大多数,则认为为白棋  
47.	        else  
48.	            disp('black piece');  
49.	        end  
50.	    end  
51.	  
52.	    % Display the croped image  
53.	    figure  
54.	    imshow(im_gray)  
55.	end  
调用函数
1.	row = 5;  
2.	column = 6;  
3.	im_board = board_position(row,column);  

输出结果如下
在这里插入图片描述
在这里插入图片描述

该棋子被判别为黑棋

Tutorial Activity 2: Spot the Difference !

1.函数列表

  • bwlabel
    L = bwlabel(BW,conn) returns a label matrix, where conn specifies the connectivity.

  • imbinarize
    BW = imbinarize(I,T):creates a binary image from image I using the threshold value T. T can be a global image threshold, specified as a scalar luminance value, or a locally adaptive threshold, specified as a matrix of luminance values.

  • imshowpair
    obj = imshowpair(___,method):uses the visualization method specified by method.method ’montage’ means that places A and B next to each other in the same image.

  • rectangle(‘Position’,pos)
    creates a rectangle in 2-D coordinates. Specify pos as a four-element vector of the form [x y w h] in data units. The x and y elements determine the location and the w and h elements determine the size. The function plots into the current axes without clearing existing content from the axes.

  • regionprops
    Measure properties of image regions, properties we use is list as follow.
    ‘BoundingBox’ Position and size of the smallest box containing the region, returned as a 1-by-(2*Q) vector. The first Q elements are the coordinates of the minimum corner of the box. The second Q elements are the size of the box along each dimension. For example, a 2-D bounding box with value [5.5 8.5 11 14] indicates that the (x,y) coordinate of the top-left corner of the box is (5.5, 8.5), the horizontal width of the box is 11 pixels, and the vertical height of the box is 14 pixels.

2.找不同

(1)思路分析
1.想要找出两张图的不同之处,可以逐个对比像素点的RGB值,并将其转换为灰度值以便后续计算,从而得到灰度值差值矩阵。进一步处理矩阵,先设定一个合适的阈值,若矩阵中的差值元素超过阈值,则认为两处不同。最后利用imlincomb函数将原图与差值图叠加,得到“找不同”游戏的答案。
(2)代码及运行结果

1.	%% Tutorial Activity 2: Spot the Difference !  
2.	clc;close all;clear all;  
3.	f = imread('spot_the_difference.png');  
4.	[M,N,D] = size(f);  
5.	im1 = f(:,1:N/2,:);  
6.	im2 = f(:,N/2+1:end,:);  
7.	  
8.	im_diff=im1-im2;  
9.	im_diff=rgb2gray(im_diff);%change into grayscale,(in range double[0,255])  
10.	im_diff=im_diff>40;%要转换为double类型进行计算,如用uint8,可能数据溢出  
11.	im_diff=cat(3,im_diff*255,zeros(size(im_diff)),zeros(size(im_diff)));  
12.	  
13.	%Concatenate arrays,which 3 is number of dimension.  
14.	im_diff = uint8(im_diff);%change into uint8scale  
15.	im_diff1 = im_diff;  
16.	im_diff = imlincomb(0.5,im1,10,im_diff,'uint8');  
17.	figure;  
18.	subplot(1,3,1);imshow(im1);  
19.	subplot(1,3,2);imshow(im2);  
20.	subplot(1,3,3);imshow(im_diff)  
21.	% figure;imshow(im_diff1)  
22.	% d =  bwconncomp(im_diff1)  

在这里插入图片描述

3.计算连通区域
(1)思路分析
我们想借助bwlabel或者bwconncomp函数获取连通区域的属性,以更好地标记处两张图像的不同点,为了计算连通区域,我们需要将RGB图转换为二值图。先仿照之前的做法,计算出两图像像素点RGB差值,再转换为灰度值,借助graythresh算出合理的阈值,并以此为标准,再将灰度值进一步转换为二值图。采用bwlabel、regionprops计算连通区域及其属性,最后借助rectangle函数绘图。

(2)代码及运行结果

1.	clc;close all;clear all;  
2.	f = imread('spot_the_difference.png');  
3.	[M,N,D] = size(f);  
4.	img1 = f(:,1:N/2,:);  
5.	img2 = f(:,N/2+1:end,:);  
6.	  
7.	% calculate the size   
8.	img1_size = size(img1);  
9.	img2_size = size(img2);  
10.	  
11.	% 防止图像不匹配  
12.	if(~isequal(img1_size, img2_size))  
13.	    error('The two images must be the same size and shade of colour.');  
14.	end  
15.	  
16.	% %计算图像差值  
17.	% kep_kulonbseg1 = img2 - img1;  
18.	% kep_kulonbseg2 = img1 - img2;  
19.	% %比如图像a和b,做a(3,3,2-b(3,31),如果相减是个负数的话matla会自动处理为0,  
20.	% kep_kulonbseg = kep_kulonbseg1 + kep_kulonbseg2;     
21.	  
22.	% 计算图像差值  
23.	im_diff = img1 - img2;  
24.	figure;imshow(im_diff)  
25.	% 转换为灰度值  
26.	gray_diff = rgb2gray(im_diff);  
27.	  
28.	%使用 graythresh 计算阈值。阈值归一化至范围 [0, 1]29.	treshold = graythresh(gray_diff);  
30.	% treshold = 0.4;%也可以自己设定阈值  
31.	%使用阈值将图像转换为二值图像。  
32.	BW = imbinarize(gray_diff,treshold);  
33.	figure;imshow(BW)  
34.	%在二值图像旁边显示原始图像。  
35.	figure;imshowpair(gray_diff,BW,'montage')  
36.	connect_area = bwlabel(BW,8);  
37.	connect_num =  bwconncomp(BW)  
38.	%对二维二值图像中的连通分量进行标注  
39.	%详见参考资料:https://ww2.mathworks.cn/help/images/ref/bwlabel.html?searchHighlight=bwlabel&s_tid=srchtitle#bupqqy6-1-n  
40.	stat = regionprops(connect_area, 'BoundingBox');  
41.	% 获取连通区域的属性  
42.	figure  
43.	subplot(1,2,1),  
44.	imshow(img1);  
45.	title('image 1');  
46.	  
47.	%用矩形框出连通区域  
48.	subplot(1,2,2),  
49.	imshow(img2);  
50.	title('image 2');  
51.	for k = 1:size(stat)  
52.	    rectangle('Position', stat(k).BoundingBox, 'EdgeColor','r','LineWidth',2);  
53.	end  

在这里插入图片描述

差值灰度图(左)及二值图(右)

在这里插入图片描述

运行程序可知,共有68个连通区域。
在这里插入图片描述

4.改进方向
(1)函数列表

  • imdilate
    J = imdilate(I,SE) dilates the grayscale, binary, or packed binary image I, returning the dilated image, J. SE is a structuring element object or array of structuring element objects, returned by the strel or offsetstrel functions.
  • strel
    SE = strel(‘disk’,r,n) creates a disk-shaped structuring element, where r specifies the radius and n specifies the number of line structuring elements used to approximate the disk shape. Morphological operations using disk approximations run much faster when the structuring element uses approximations.

(2)分析
不足:由于计算两图RGB差值矩阵时,会存在断点。故基于8连通计算连通区域时,一个“不同点”(蛋糕奶油花纹)可能存在多个连通区域,因而被多次标记,不符合预期结果。后续可采用其他算法将靠近的连通区域判定为一个“不同点”,从而实现一个矩形框对应一个“不同点”
在这里插入图片描述

蛋糕奶油花纹处的差值二值图

改进:采用strel、imdilate函数对二值图进行膨胀,使相邻的连通区域合并,效果如下

(3)部分代码与运行结果

1.	%用imdilate函数对二值图进行膨胀,修正连通区域。  
2.	BW_inflation= imdilate(BW,strel('disk',7));  
3.	figure;imshow(BW_inflation);  
4.	connect_area = bwlabel(BW_inflation,8);  
5.	connect_num =  bwconncomp(BW_inflation)  
6.	%对二维二值图像中的连通分量进行标注  
7.	%详见参考资料:https://ww2.mathworks.cn/help/images/ref/bwlabel.html?searchHighlight=bwlabel&s_tid=srchtitle#bupqqy6-1-n  
8.	stat = regionprops(connect_area, 'BoundingBox');  
9.	% 获取连通区域的属性  
10.	figure  
11.	subplot(1,2,1),  
12.	imshow(img1);  
13.	title('image 1');  
14.	  
15.	%用矩形框出连通区域  
16.	subplot(1,2,2),  
17.	imshow(img2);  
18.	title('image 2');  
19.	for k = 1:size(stat)  
20.	    rectangle('Position', stat(k).BoundingBox, 'EdgeColor','r','LineWidth',1);  
21.	end 

在这里插入图片描述
运行程序可知,共有14个连通区域,数量大大减少
在这里插入图片描述

参考资料:

matlab图像类型转换以及uint8、double、im2double、im2uint8和mat2gray等说明_无鞋童鞋的博客-CSDN博客

Matlab 中 imregionalmax函数 和 bwconncomp函数的使用_学生的博客-CSDN博客

  • 5
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值