How to use Matlab for image processing

Image processing using Matlab

Image processing is the process of manipulating the digital properties of an image to improve its quality or to obtain desired information from the image. It requires importing images in an image processing application, analyzing the images, and then operating on the images to obtain appropriate output that produces the desired results.

In this article, we will discuss the basics of image processing and analysis using Matlab to determine image features, adjust image features, and improve image quality.

Prerequisites

  • [Matlab] is installed on your computer.

  • Understand the basics of [Matlab].

Import images in Matlab

Importing an image involves bringing the image into Matlab's current directory. This way the image can be used.

To do this, open Matlab and execute the following command.

i = imread('name of the image;') %This will assign the image to i
imshow(i);
复制代码

Use Matlab to enhance images

This is the process of improving a digital image to obtain a result more suitable for visual display. Image enhancement can be achieved through image filtering and deblurring.

Image filtering

This is a form of image enhancement that emphasizes or omits selected attributes of an image. Image filtering mainly involves changing the concentration of certain pixels in an image.

Color filtering makes an image more attractive or emphasizes certain image pixels. For example, green can emphasize vegetation, while blue emphasizes bodies of water.

This process can make the image appear reddish, greenish, or bluish, depending on the concentration level applied. The imhist function gives a graphical representation (histogram) of the color intensity of each pixel in the image.

i =  imread('nyali.jpg');
imshow(i)
Red = i(:,:,1);
Green = i(:,:,2);
Blue = i(:,:,3);
temp = i;
复制代码
imhist(Red);
复制代码
imhist(Green);
复制代码
imhist(Blue);
复制代码
figure;
temp = i;
temp(:,:,1) = temp(:,:,1) + 100;
imshow(temp);
复制代码
figure;
imshow(i)
temp = i;
temp(:,:,2) = temp(:,:,2) + 100;
imshow(temp);
复制代码
temp = i;
temp(:,:,3) = temp(:,:,3) + 100;
imshow(temp);
复制代码

Image deblurring

Increases the sharpness of an image by making blurry pixels on the image sharper. To make this work, we first import the image using the code below.

i =  imread('nyali.jpg');       %this code imports the image
imshow(i)
复制代码
  • Create a blurred image i from the original image. This image will be used as a simulator for the deblurring process.

  • First, make a point spread function (PSF) with a specified linear motion (in my case I will use 50 pixels with an angle of 10 degrees) by using fspecial, then convolute the PSF with the image by using the imfilter function product.

PSF = fspecial('motion',50,10);
Idouble = im2double(i);
blurred = imfilter(Idouble,PSF,'conv','circular');
imshow(blurred)
复制代码
  • Use the deconvwnr command to restore blurred images.

wnr1 = deconvwnr(blurred,PSF);
imshow(wnr1)
复制代码

Get the number of objects contained in the image

The number of objects contained in an image can be determined by following the steps below.

  • Remove objects that do not require numerical values.

  • Give an image a uniform background by removing its original background.

  • Change the image to grayscale.

  • Creating a binary version of the image allows numerical analysis of the object to be analyzed.

The following code snippet is used in this process.

i = imread('imageName'); %import the image
imshow(i)
复制代码
se = strel('disk',150);
background = imopen(i,se);  %Performs morphological openning
imshow(background)
复制代码
  • 从原始图像中去除背景近似图像。这将形成一个具有统一背景但有点暗的结果图像。

i2 = i - background; 
imshow(i2)
复制代码
  • 将图像格式从RGB改为灰度。新处理的图像将被分配到i3 。

i3 = rgb2gray(i2);
imshowi3
复制代码
  • 使用imbinarize 命令来创建灰度图像的二进制版本i3 。

bw = imbinarize(i3);
bw = bwareaopen(bw,50);
imshow(bw)
复制代码

二进制版本的图像可以进行物体分析。结果的准确性取决于物体的大小、参数连通性和物体之间的间距。

请注意,绿色的物体不存在于二进制版本的图像中,因为真正的颜色(红、绿、蓝)不能被二进制化。
cc = bwconncomp(bw) % shows image information复制代码

在图像中找到一个特定颜色的区域

在图像中具有不同颜色的物体的区域可以通过颜色阈值法用Matlab确定。阈值化是根据强度将像素分配到某些类别。

颜色阈值处理也使我们能够确定地图中选定特征的区域。例如,在卫星地图中找到一个水体的面积。

为了证明这个概念,我将从一张卫星地图图片中确定一个水体的面积。该图片的分辨率为480 * 494 像素,深度为32,按卫星计算。

i = imread('l.victoria.PNG'); %import the image
imshow(i)
复制代码
i2 = rgb2gray(i); % make a grayscale image of i
imshow(i2)
复制代码
imhist(i2) %histogram for pixel distributions复制代码

从直方图上看,X轴代表强度值,Y轴代表像素数。

  • 在Matlab窗口工具栏中打开APPS,向下滚动到图像处理和计算机视觉,然后点击颜色阈值。

  • 在新的窗口中从工作区加载一个图像,然后选择一个颜色空间,点击HSV。

  • 调整标有H 的旋钮以去除背景色,调整S 和V 以使要分析的对象的边界平滑。

  • 通过点击show binary 按钮,创建一个二进制版本的结果图像。

  • 将二进制版本导入到工作区进行进一步分析,它将被标记为BW 。二进制版本的形状与湖泊的形状相似,我们可以用函数imshowpair ,直观地比较这两幅图像。

imshowpair(i,BW,'montage')
复制代码
  • 为了找到二进制版本图像的图像统计,使用regionprops 函数。

stats = regionprops('table',BW,'all')
复制代码

以像素为单位的区域面积是表格中的面积道具之和。

areainpixels = sum(props.Area)
复制代码

面积可以使用地图上可能提供的比例尺转换为平方公里。通常,比例尺有一个预先确定的数值,以米、英里或公里为单位表示。

使用imtool(i) 功能来测量线性比例尺的起点和终点之间的距离。测量值的单位是像素,它代表比例尺的预定尺寸。从像素转换到所需的单位。

总结

Matlab为图像处理提供了一个完美的环境,因为它的命令和片段很容易遵循和应用。

图像处理有广泛的应用领域,如。

  • 摄影。

  • 图像的地理数据分析。

  • 装饰品。

  • 了解生物结构。

  • 机器视觉,和娱乐。

从图像中提取的信息的准确性取决于用于处理图像的工具的质量,Matlab为图像处理提供了更好的工具。

Guess you like

Origin blog.csdn.net/qq_42003636/article/details/128835010