[MATLAB Image Processing Toolbox Getting Started Tutorial 1] Quick Start-"Image Import and Export" and "Detecting Circles in Images"


Written in the front: 2020.10, the first month of the transfer, undergraduate automation major, master photoelectric science and engineering major, doctoral optical engineering major. I still consider myself an undergraduate, because I only got the undergraduate degree certificate. The project I did during my master's degree has nothing to do with image processing. I taught myself MATLAB Iamge Processing Toolbox to increase the scope of my study, but I don't know which day I will use it. I want to update this series first to keep a record of my learning in the next few years, and second, I sincerely hope that I can stick to it. I am well aware that if I want to successfully graduate with a Ph.D. or meet the status of a Ph.D., it is not enough to just study these. I hope that the community leaders will speak lightly, and I hope to make progress with everyone.

Introduction to MATLAB Image Processing Toolbox

The MATLAB Help Center document describes:
"Image Processing Toolbox™ provides a comprehensive set of reference standard algorithms and workflow apps for image processing, analysis, visualization, and algorithm development. You can use deep learning and traditional image processing techniques to perform image segmentation, Image enhancement, denoising, geometric transformation and image registration. The toolbox supports processing two-dimensional, three-dimensional and arbitrarily large images.
Image Processing Toolbox App allows you to automate common image processing workflows. You can divide image data interactively , Compare image registration techniques, and perform batch processing on large data sets. With visualization functions and apps, you can explore images, three-dimensional volumes, and videos; adjust contrast; create histograms; and perform operations on regions of interest (ROI).
You The execution speed of algorithms can be increased by running algorithms on multi-core processors and GPUs. Many toolbox functions support C/C++ code generation for desktop prototypes and embedded vision system deployment."

The summary is: MATLAB image processing toolbox can implement any operation (nonsense) on your images of any size.

Knowledge framework of MATLAB Image Processing Toolbox

Image processing toolbox includes the following contents:

1 Image processing toolbox quick start: basic knowledge learning;
2 import, export and conversion: image data import and export, image type conversion;
3 display and exploration: interactive tools for image display and exploration;
4 Geometric transformation and image registration: use intensity correlation, feature matching or control point mapping to scale, rotate, perform other N-dimensional transformations and align images;
5 Image filtering and enhancement: contrast adjustment, morphological filtering, deblurring, attention-based Region processing;
6 image segmentation and analysis: region analysis, texture analysis, pixel and image statistics;
7 deep learning for image processing: use convolutional neural networks to perform image processing tasks, such as removing image noise and creating based on low-resolution images For high-resolution images, deep learning toolbox is required at this time;
8 3D volume image processing: filtering, segmentation and other image processing operations on 3D volume data;
9 Code generation: generating C code and MEX functions for toolbox functions;
10 GPU computing: Run image processing code on the graphics processing unit (GPU).

The author currently does not know the specific role of each module, and may only be qualified to say this when I am lucky enough to update all the modules. If you have a basic foundation, you can choose the part you are interested in to continue learning according to the general description above.

After finishing the introduction to MATLAB Image Toolbox, let's start the first part of Image processing toolbox quick start learning.

Image Processing Toolbox Quick Start-"Image Import and Export" and "Detecting Circles in Images"

1 Import, process and export of basic images

1.1 Image reading and display

MATLAB uses imread and imshow to read and display pictures, similar to OpenCV.
Read pictures from the workspace:

I=imread('1.jpg')

At this point, picture 1.jpg has been read in, and I is an array representing the picture.
Since what is read is a color picture, the number I is a three-dimensional array
display image:

imshow(I)

Zhao Yun has gentian skin and handsome thief.
The pictures read in before can be displayed.

1.2 Check how the image is displayed in the workspace

You can use the whos command to check the storage mode of the image read by imread in the workspace. To put it bluntly, you can view the size, data type, etc. of the array I, which can also be viewed in the workspace.
Continue typing in the MATLAB command line window

whos I

You can get:Insert picture description here

1.3 Improve image contrast

Use the imhist function to view the pixel intensity distribution of the image, but note that imhist can only view the histogram of the grayscale image, so it is called the grayscale histogram. Use rgb2gray to convert the RGB image into a grayscale image, continue to input:

J=rgb2gray(I);
imhist(J);

That is, Zhao Yun-Gentiana's grayscale image J and its grayscale histogram are obtained.
Grayscale
Gray histogram
The gray-scale histogram reflects the contrast of the image. The wider the histogram, the stronger the contrast, and vice versa.

The histeq function is used for histogram equalization to enhance the contrast of the image.
Keep typing:

K=histeq(J);
figure;imshow(K);
figure;imhist(K);

The grayscale image and its histogram after the histogram equalization can be obtained.
Zhao Yun-Gentian after histogram equalization
Grayscale histogram after equalization
It can be seen that the black and white contrast of the processed grayscale image is more obvious, and the distribution range of the grayscale histogram is wider.

1.4 Write pictures to disk

Use imwrite to write picture K to disk:

imwrite(K,'ok.jpg');

You can write the picture into the current working folder of MATLAB.
If you want to write the picture to any specified folder, enter the complete path between the two single quotes.

We can use the imfinfo function to view the information of the pictures in the folder, such as the size, format, and color type of the picture.
Keep typing

imfinfo('ok.jpg');

It

can be seen that the returned image information type is an array type.

2 Detect and measure circular objects in the image

This section explains how to use the imfindcircles function of MATLAB to detect circular objects in the image, and mark the detected circles through the viscircles function.

2.1 Load the image and determine the range of the search circle

In order to facilitate the example, the picture used this time is the "color potato chips" that comes with MATLAB. First, imread reads the picture and displays it with imshow.

 srcImage=imread('coloredChips.png');
 imshow(srcImage);

Insert picture description here
This picture is perfect as a recognition example of a circular object. There are both blue and red potato chips with strong color contrast, and yellow which is similar to the background color. More importantly, there are some overlapping circles in the picture, which are close to reality. Target detection in this situation is also more challenging.

After loading the picture, the next step is to determine the radius of the search circle, here is the imdistline function. Then enter:

d=imdistline;

Insert picture description here
It can be seen that there is a ruler-like thing added to the previously loaded color potato chip image. We can drag this ruler and move the boxes on both sides of the ruler to measure the diameter of the circle in the image. Here we only need to measure An approximate radius is enough.
Insert picture description here
Insert picture description here
Randomly measured two, the diameter is 40-50 pixels, so the radius range is 20-25, of course, to be safe, you can also set a bit larger, this interval [20,25] should be remembered, the following circle recognition is required Used, but pay attention, don’t forget to delete the imhistline tool after determining the radius range, which is the object d above.

delete(d);

2.2 Initial attempt at circle recognition

Before circle recognition, let us get familiar with the function used: imfindcircles.

[centers,radii]=imfindcircles(A,radiusRange);

A is the input rgb image, radiusRange is the approximate radius of the search circle we determined in the previous step, and the returned centers and radii are the detected center position and the corresponding circle radius.
We enter in MATLAB:

[centers,radii]=imfindcircles(srcImage,[20,25]);

Insert picture description here

But found that the center and radius returned are actually empty arrays. It doesn't matter, this is a normal phenomenon, but the circle is not detected. At least we will simply use the imfindcircles function, but how can we detect the circle? Continue to look down.

2.3 In-depth imfindcircles function

Let's take a closer look at the function imfindcircles. Through the help documentation of MATLAB, we found this syntax:

[centers,radii,metric]=imfindcircles(A,radiusRange,Name,Value);

The parameters A and radiusRange have already been mentioned above, the following Name is the parameter name passed to the imfindcircles function, and the value is the corresponding parameter value.
The specific Name is: ObjectPolarity/Method/Sensitivity/EdgeThreshold. We analyze the parameters one by one:
1 ObjectPolarity
This parameter is used to specify the brightness relationship between the detected target and the background. The specific values ​​and meanings are:
'bright' (default): search for round objects that are brighter than the background;
'dark': search for round objects that are darker than the background.

So, how do we determine whether the target is brighter or darker than the background? You can use the following method:
Use rgb2gray to convert the color potato chips to grayscale.

grayImage=rgb2gray(srcImage);
 imshow(grayImage)

Insert picture description here
It can be seen that most of the circles in the grayscale image are darker and darker than the background color, so it is more appropriate to set the ObjectPolarity parameter in this image to'dark'.

2 Method
This parameter is used to specify the algorithm of the accumulator array, with the following two values:
'PhaseCode' (default): Atherton and Kerbyson's phase encoding method;
'TwoStage': This algorithm uses two-stage circular Hough transform.

3 Sensitivity
This parameter sets the detection sensitivity of the circular Hough transform accumulator array. This parameter has an effect on both Method parameters. The default value is 0.85 and can be set to a number within 0-1.
Higher sensitivity can detect weak circles and occluded circles, but it also increases the risk of false detection.

4 EdgeThreshold
This parameter is used to determine the gradient threshold of edge pixels in the image. When the threshold is set to a lower value, imfindcircles will detect more circular objects (with weak and strong edges). As the threshold value increases, it will detect fewer circles with weak edges. Generally, by default, imfindcircles uses the function graysthresh to automatically select the edge gradient threshold.

In the returned parameter list, we found that there is one more parameter metric. This parameter returns the intensity of the detected circle, which is the relative intensity of the center of the circle.

The MATLAB help file lists several tips for using the imfindcircles function:
①When the input minimum radius is less than 5, the detection accuracy of imfindcircles will be affected.
②Using the default algorithm (PhaseCode) will be faster than TwoStage when the radius is approximated.
③ Both detection algorithms have their advantages and disadvantages, and the detection results vary with the input picture.
④ When the center of the circle is outside the picture, the imfindcircles function cannot detect the circle.
⑤ When detecting binary images, the imfindcircles function will preprocess it. Similarly, when detecting rgb images, the imfindcircles function will first convert them to grayscale images.

2.4 Try circle recognition again

After a thorough understanding of the role of each parameter in the imfindcircles function, the real circle recognition is then carried out, and the detected circle is displayed in the original image through the viscircles function.

Type in MATLAB:

 [centers,radii]=imfindcircles(srcImage,[20,25],'ObjectPolarity','dark','Sensitivity',0.9);
 imshow(srcImage);
 h=viscircles(centers,radii);

Insert picture description here
We found that some of the circles have been detected. Continue to increase the detection sensitivity:

 [centers,radii]=imfindcircles(srcImage,[20,25],'ObjectPolarity','dark','Sensitivity',0.92);
delete(h);%删除之前画的圆
 h=viscircles(centers,radii);

Insert picture description here
It can be seen that more circles were detected this time. Let's try another detection method, with the other parameters unchanged:

 [centers,radii]=imfindcircles(srcImage,[20,25],'ObjectPolarity','dark','Sensitivity',0.92,'Method','twostage');
delete(h);
 h=viscircles(centers,radii);

Insert picture description here
This time, except for the yellow circle that is brighter than the background, all the other circles were detected.
It can be seen that using the TwoStage algorithm can detect more circles at a sensitivity of 0.92. Generally speaking, the two algorithms are complementary and have different advantages. The default PhaseCode method is faster, anti-noise, and stable, but if you want to achieve the same detection effect as the TwoStage algorithm, you need higher sensitivity. We use the default algorithm and try it at 0.95 sensitivity:

 [centers,radii]=imfindcircles(srcImage,[20,25],'ObjectPolarity','dark','Sensitivity',0.95);
delete(h);
 h=viscircles(centers,radii);

Insert picture description here
The effect is the same as the detection result of the TwoStage algorithm at a sensitivity of 0.92.

2.5 Perfect circle recognition

After the above steps of detection, except for the yellow potato chips that are brighter than the background, everything else is detected. Let's detect the yellow circle and set the ObjectPolarity parameter to bright:

 [centers,radii]=imfindcircles(srcImage,[20,25],'ObjectPolarity','bright','Sensitivity',0.95);
delete(h);
 h=viscircles(centers,radii);

Insert picture description here
Three yellow circles were detected, and one circle with a fuzzy border was not detected. When we adjust the sensitivity to 0.95, this circle can be detected, but in addition, orange and blue circles are detected. Interested students can try it.
So how to detect this yellow circle without repeating the detection? A parameter we overlooked EdgeThreshold comes in handy. The yellow circle with blurred border has a lower edge gradient. The default edge gradient value cannot identify the edge of this circle, so we have to reduce the value of the edge gradient:

 [centers,radii]=imfindcircles(srcImage,[20,25],'ObjectPolarity','bright','Sensitivity',0.92,'EdgeThreshold',0.1);
delete(h);
 h=viscircles(centers,radii);

Insert picture description here
It can be seen that all the yellow circles have been detected (although there is still a green...).

2.6 Draw all detected circles

This section lists the complete code of MATLAB to identify the circle, for reference only

srcImage=imread('coloredChips.png');
imshow(srcImage);
d=imdistline;
delete(d);
[centersDark,radiiDark]=imfindcircles(srcImage,[20,25],'ObjectPolarity','dark','Method','TwoStage','Sensitivity',0.92);
hDark=viscircles(centersDark,radiiDark,'EdgeColor','G'); %设定画出的圆的边界为绿色,其中EdgeColor不同版本不同,博主用的2014,新版本的MATLAB用Color即可。
[centersBright,radiiBright]=imfindcircles(srcImage,[20,25],'ObjectPolarity','bright','Sensitivity',0.92,'EdgeThreshold',0.1);
hBright=viscircles(centersBright,radiiBright,'EdgeColor','B');

Insert picture description here
You're done, all the circles are detected.
You can try to change the values ​​of various parameters in the imfindcircles function to see if there are any amazing results.

When writing an article for the first time, it is inevitable that the layout is not good or wrong. Please correct me.

Guess you like

Origin blog.csdn.net/weixin_43024526/article/details/109270165