第一次写博客,本文主要源于图像处理大做业,不足之处,还望指正。app
1. Introduction (5%)less
The task of the project is to find the dial-code switch in the figure below and calibrate the rectangular box, while displaying the dial code in the lower left corner.ui
To solve the problem, first of all the image tilt correction to ensure that the dial code switch and the image edge parallel. The next step is to read the binary image of the dial code switch and calculate the coordinates of the pixel in the center of each binary region, so as to prepare for the next step of analyzing the status of the dial code switch and determine the status of the dial code switch according to the coordinate positions of "0" and "1". Finally, draw the target rectangle box and the bottom left rectangle box, fill in the converted number in the lower left rectangle.this
2 Proposed approach (50%)spa
The proposed approach includes:.net
①Tilt correction of image.3d
②Mean filtering plus Binarization.code
③The center position matrix of each binary region is calculated.orm
④Analyze the dial-code status.blog
⑤Draw rectangle.
⑥Draw the bottom left corner to fill the rectangle box and put in the Switch code.
2.1 Tilt correction of image
Hough transform is used to detect the straight line in the image and calculate the inclination Angle.
I1 = edge(I,'Sobel','horizontal') % Detect image edges
subplot(2,4,2); imshow(I1); title("边缘检测");
[H , T, R] = hough(I1,'Theta',-89:89); % Hough transform is used to detect straight lines
ccc = max(H); %Find the most likely inclination Angle of the line
[value, rot_theta] = max(ccc);
if rot_theta~=175 %Robustness of the algorithm
rot = rot_theta+270;
else
rot = rot_theta;
end
I2 = imrotate(I,rot,'bilinear', 'loose'); %Tilt correction
subplot(2,4,3); imshow(I2); title("校订后的图像");
2.2 Mean filtering plus Binarization
After using mean filtering to blur the edge information, the dial-code switch is separated from the background by binarization.
A=filter2(fspecial('average',9),I2); % Mean filtering
I4 = imbinarize(A,220); % Image binarization, threshold set to 220
subplot(2,4,4),imshow(I4);title("二值化");
2.3 The center position matrix of each binary region is calculated
Use Matlab's own function to find the center point of each dial-code switch area, and prepare for the subsequent 0,1 analysis.
B=bwboundaries(I4); %Find the edge matrix for I4
[L, n]=bwlabel(I4, 4); % Returns an L matrix of the same size as I4
stats = regionprops(L,'all'); %get the properties of region
Cen = cat(1, stats.Centroid);% Construct multidimensional array
b = Cen(:,2); %Returns the ordinate of the center point of each region
2.4 Analyze the dial-code status
According to the coordinates of the center point, the sum of the maximum and minimum values less than half is set as 1, otherwise set as 0.
C = zeros(1,n);
for i = 1:n
if b(i)<(max(b)+min(b))/2
% Less than the halfway point is 1, and greater than the halfway point is 0
C(i)=1;
else
C(i)=0;
end
end
2.5 Draw rectangle
First, use the border function to detect the initial target position, and then use the Rectangle function to draw the Rectangle box
I0 = medfilt2(I); %Median filter fuzzy edge information
bw0 = imbinarize(I, graythresh(I0)); %A suitable threshold of the picture is found by using the most large class variance method, that is the threshold of binarization.
bw0= bwareaopen(bw0,50,4); %Delete smaller objects in binary images
B0=bwboundaries(bw0); %Gets the outline of bw0
[L0, n0]=bwlabel(bw0, 4); %Returns an L0 matrix of the same size as bw0
subplot(2,4,5),imshow(I);title("结果");
hold on;
for i=1:1:n0
boundary1=B0{i};
rectangle('position',[min(boundary1(:,2))-5,min(boundary1(:,1))-2,...
10+max(boundary1(:,2))-min(boundary1(:,2)),...
5+max(boundary1(:,1))-min(boundary1(:,1))],'edgecolor','b')
end %Draws the target rectangle box
2.6 Draw the bottom left corner to fill the rectangle box and put in the Switch code
Draw and fill the bottom left Rectangle using the Rectangle function, and put the transformed code into it.
rectangle('position',[0,432,500,50],'edgecolor','r') %Draws the bottom left rectangle
fill([0,0,520,520],[432,482,482,432],'b')% Fill the rectangle with blue
Switch_code = num2str(C);
str = ['Switch code: ',Switch_code];
text(0,455,str,'FontSize',8) %Fill in the switch code
3 Experimental results and analysis (40%)
To validate the accuracy of the algorithm, the intermediate process image is shown.
Figure 1 is the image after edge detection. It is obvious to see the two lines in the figure. In addition, Roberts, Prewitt, Log and Canny operators are also tried, and the performance of Prewitt and sobel operators is approximately the same, while other operators perform poorly.
Figure 2 shows a rotated corrected image with the dial code switch in the correct horizontal position. The effect of hough transform on line detection is verified.
In figure 3, gaussian filter is used to blur the edges, which facilitates the next step of extracting the target-dial-code switch.
Figure 4 shows the dial-code switch after binarization. It is obvious to see the status of the dial-code switch.
The processing results of the last four images were all accurate. Meanwhile, see table 1, in order to test the time consumption of the algorithm, I conducted 10 tests on the four images, and the average value obtained was 0.440, 0.117, 0.423 and 0.416 (s), respectively. For general assembly operations, can be fully satisfied to go.
The shortcoming of this algorithm lies in the fact that hough_transform used in the image correction process fails to deal with all images successfully. If the least-square method is used to calculate the inclination Angle, the result may be better.
F
P |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
Mean (S) |
1 |
0.449 |
0.412 |
0.452 |
0.436 |
0.440 |
0.428 |
0.444 |
0.431 |
0.418 |
0.486 |
0.440 |
2 |
0.420 |
0.431 |
0.429 |
0.411 |
0.414 |
0.409 |
0.396 |
0.419 |
0.414 |
0.430 |
0.417 |
3 |
0.467 |
0.420 |
0.421 |
0.409 |
0.440 |
0.410 |
0.402 |
0.417 |
0.422 |
0.424 |
0.423 |
4 |
0.407 |
0.404 |
0.419 |
0.453 |
0.416 |
0.417 |
0.404 |
0.406 |
0.406 |
0.423 |
0.416 |
4 Conclusion (2%)
In order to read the dial-code switch state, this paper proposes a method based on hough transform. The method of image do first tilt correction, make the dial the code switch located at the state level after correction. After using the binary to dial the code switch and background separation, finally successfully read dial the code switch state.
clear; clc; close all; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %修改于2019年11月16日 %做者:指尖琴韵 %目的:识别拨码开关状态并显示 %参考:一、博客:https://blog.csdn.net/Joseph__Lagrange/article/details/96099117 %二、博客:https://blog.csdn.net/qq_27901091/article/details/77878238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% %原图对比 tic I = imread('4.png'); subplot(2, 4, 1); imshow(I);title('原图'); %% %图像倾斜校订 I1 = edge(I,'sobel','horizontal'); subplot(2,4,2); imshow(I1); title("边缘检测"); [H,T,R] = hough(I1,'Theta',-89:89); %霍夫变换检测直线 ccc = max(H); [value, rot_theta] = max(ccc); if rot_theta~=175 rot = rot_theta+270; else rot = rot_theta; end I2 = imrotate(I,rot,'bicubic', 'loose');%倾斜校订 subplot(2,4,3); imshow(I2); title("校订后的图像"); %% %二值化 A=filter2(fspecial('average',9),I2); %均值滤波 I4 = imbinarize(A,220); %对图像二值化,阈值设为220 subplot(2,4,4),imshow(I4);title("二值化后的图像"); %% %计算每块二值化区域后的中心位置坐标矩阵 B=bwboundaries(I4); [L, n]=bwlabel(I4, 4); stats = regionprops(L,'all'); Cen = cat(1, stats.Centroid); b = Cen(:,2); %% %0,1分析 C = zeros(1,n); for i = 1:n if b(i)<(max(b)+min(b))/2 C(i)=1; else C(i)=0; end end %% %为目标矩形框作参数准备 I0 = medfilt2(I); bw0 = imbinarize(I, graythresh(I0)); bw0= bwareaopen(bw0,50,4); B0=bwboundaries(bw0); [L0, n0]=bwlabel(bw0, 4); %% %绘制矩形框 subplot(2,4,5),imshow(I);title("结果"); hold on; for i=1:1:n0 boundary1=B0{i}; rectangle('position',[min(boundary1(:,2))-5,min(boundary1(:,1))-2,... 10+max(boundary1(:,2))-min(boundary1(:,2)),... 5+max(boundary1(:,1))-min(boundary1(:,1))],'edgecolor','b') end %% %绘制左下角填充矩形框 rectangle('position',[0,432,500,50],'edgecolor','r') fill([0,0,520,520],[432,482,482,432],'b') %% %绘制Switch code Switch_code = num2str(C); str = ['Switch code: ',Switch_code]; text(0,455,str,'FontSize',8) %% toc