基础图像处理操作

  • 1.1 基础的图像处理
  • 1.2 图像展示
  • 1.3 写入图像及压缩
  • 1.4 图像数据类型及转换
  • Tutorial Activity 1: Basic Image Operations
  • Tutorial Activity 2: Spot the Difference !
  • 参考资料:

目录

  • 1.1 基础的图像处理
  • 1.2 图像展示
  • 1.3 写入图像及压缩
  • 1.4 图像数据类型及转换
  • Tutorial Activity 1: Basic Image Operations
  • Tutorial Activity 2: Spot the Difference !
  • 参考资料:

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,3,1),如果相减是个负数的话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博客

图像处理学习笔记(一)相关推荐

  1. 数字图像处理学习笔记(三):ORB算法(尺度不变特征变换)Oriented FAST and Rotated BRIEF

    数字图像处理学习笔记(三):ORB算法(尺度不变特征变换)Oriented FAST and Rotated BRIEF 一.概述 参考:特征点匹配+特征检测方法汇总 ORB的全称是Oriented ...

  2. 数字图像处理学习笔记(二):SIFT(尺度不变特征变换)算法

    数字图像处理学习笔记(二):SIFT(尺度不变特征变换)算法 一.概述: 提到特征点算法,首先就是大名鼎鼎的SIFT算法了.SIFT的全称是Scale Invariant Feature Transf ...

  3. 数字图像处理学习笔记(一):特征检测和匹配概述

    数字图像处理学习笔记(一):特征检测和匹配概述 参考博客: 特征点的匹配 SIFT特征详解 数字图像处理学习笔记(二):SIFT(尺度不变特征变换)算法 1.特征点概述 如何高效且准确的匹配出两个不同 ...

  4. 数字图像处理学习笔记(三)——空间分辨率和灰度分辨率、等偏爱曲线

    数字图像处理(Digital Image Processing)是通过计算机对图像进行去除噪声.增强.复原.分割.提取特征等处理的方法和技术.本专栏将以学习笔记形式对数字图像处理的重点基础知识进行总结 ...

  5. 基于python的数字图像处理--学习笔记(三)

    基于python的数字图像处理--学习笔记(三) 前言 一.灰度拉伸 二.幂律(伽马)变换 三.对数变换 前言 进入冈萨雷斯的第三章内容,并用python实现功能.我更改了代码源,之前找到太烂了,代码 ...

  6. 数字图像处理学习笔记(十五)——图像复原与重建

    数字图像处理(Digital Image Processing)是通过计算机对图像进行去除噪声.增强.复原.分割.提取特征等处理的方法和技术.本专栏将以学习笔记形式对数字图像处理的重点基础知识进行总结 ...

  7. matlab bwmorph spur,matlab图像处理学习笔记-数学形态与二值图像操作

    matlab图像处理学习笔记-数学形态与二值图像操作 数学形态学主要处理的是二值图像,因为二值图像的处理操作比较简单. 9.1 数学形态学图像处理 基本思想:利用一个称作结构元素(structurin ...

  8. 数字图像处理学习笔记(六)——数字图像处理中用到的数学操作

    数字图像处理(Digital Image Processing)是通过计算机对图像进行去除噪声.增强.复原.分割.提取特征等处理的方法和技术.本专栏将以学习笔记形式对数字图像处理的重点基础知识进行总结 ...

  9. 高光谱图像处理学习笔记

    高光谱图像处理学习笔记 面试需要,所以来学习一下高光谱图像处理的相关知识 文章目录 高光谱图像处理学习笔记 一.高光谱图像相关的概述 一.高光谱图像相关的概述 1.常见的光谱范围 红外光谱范围一般是7 ...

  10. 数字图像处理学习笔记(八)——图像增强处理方法之点处理

    数字图像处理(Digital Image Processing)是通过计算机对图像进行去除噪声.增强.复原.分割.提取特征等处理的方法和技术.本专栏将以学习笔记形式对数字图像处理的重点基础知识进行总结 ...

最新文章

  1. CentOS6.8安装Python3.6.3
  2. 文件上传下载-修改文件上传大小
  3. react 改变css样式_web前端入门到实战:编写CSS代码的8个策略,资深开发工程师总结...
  4. mac下查看redis安装路径_干货!win10环境下Redis安装、启动教程
  5. 2019福建省c语言知识点,2019最新C语言知识整理(干货)
  6. GDB中创建要素数据集
  7. java html写入到word文档_java 使用jacob将html页面写入word
  8. python中init是什么_详细解读Python中的__init__()方法
  9. 虚拟机服务器做ghost,图文教程:利用VMware虚拟机一步一步学着做GHOSTX——转自无约而来...
  10. 面试现场简单几道java算法题,你能写出几道?
  11. Phase2 Day13 MyHashMap
  12. 【观察】海外本地化机遇与挑战并存,跨境电商如何跑出“加速度”?
  13. 西电微机系统课程设计步进电机开环控制系统
  14. Win11电脑名如何更改?Win11更改电脑名的方法
  15. Web在线打印设计器即将推出,像Excel一样在线设计模板
  16. vue根据pc端、移动端做路由适配
  17. windows平台下的oracle ORA-01031的解决方法
  18. css技术点二:字体图标(阿里巴巴字体图标使用)
  19. php获取蓝奏云直连,最新php蓝奏云直链api接口源代码
  20. 知道创宇区块链实验室受邀参加“2021 CCF中国区块链技术大会”

热门文章

  1. 运维派 企业面试题45 创建10个 用户 ; ping探测主机是否在线
  2. 《终身成长》读书笔记
  3. table 超级详细的 商品订单列表
  4. pcie数据反_理解PCIE链路反转和极性反转
  5. maven仓库的优先级,profile的优先级
  6. 三线一单”大气环境质量底线体系与划分技术方法
  7. MOS管当开关控制时,为什么一般用PMOS做上管NMOS做下管?
  8. 基于springboot的高校疫情打卡上报系统设计与实现 文档+项目源码及数据库文件+演示视频
  9. python实现汇率转换
  10. 解决ES Data too large问题