圖像處理-撥碼開關的狀態識別

来源:https://www.cnblogs.com/lmy25846877/archive/2019/12/13/12036706.html
-Advertisement-
Play Games

第一次寫博客,本文主要源於圖像處理大作業,不足之處,還望指正。 1. Introduction (5%) The task of the project is to find the dial-code switch in the figure below and calibrate the rec ...


第一次寫博客,本文主要源於圖像處理大作業,不足之處,還望指正。

1. Introduction (5%)

     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.

                        

     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.

2 Proposed approach (50%)

The proposed approach includes:

Tilt correction of image.

Mean filtering plus Binarization.

The center position matrix of each binary region is calculated.

Analyze the dial-code status.

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日
%作者:指尖琴韻
%目的:識別撥碼開關狀態並顯示
%參考:1、博客:https://blog.csdn.net/Joseph__Lagrange/article/details/96099117
%2、博客: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

  

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1 class Cat(Animal): 2 def __init__(self): 3 import warnings 4 warnings.warn("Cat類帶刪除線了", DeprecationWarning) 5 6 def run(self): 7 import warnings 8 w... ...
  • 有時候會遇到這種情況,某些屬性指向限制在特定範圍內,當別人調用的時候,只能賦值在這特定範圍內的值。這樣的情況有哪些呢?比如有一個屬性是用來放年齡的,那麼這個年齡就不能被設置為負數。還有人的性別隻有男跟女兩種選擇,別來一個雌雄同體。在這樣的情況下,就會用到封裝跟隱藏。 信息的封裝和隱藏 Java中通過 ...
  • 關閉防火牆,設置虛擬機和本機在同一網段,還是ping不同 解決方法:在VMware中點擊 編輯 >虛擬網路編輯器 >更改設置 >還原預設設置 然後重新配置虛擬機和本機在同一網段,關閉VMnet1,啟動VMnet8 ...
  • 軟體包:類似於電腦的文件管理方式,主要解決同名文件。 包幫助管理大型軟體系統:將語義類似的類組織到包中;解決類命名衝突的問題。 包可以包含類和子包。 關鍵字——package package語句作為Java源文件的第一條語句,指明該文件中定義的類所在的包。若預設該語句,則指定為無名包。 格式:pac ...
  • 目錄結構: —|controller —|Home.php —|model —|view —|welcome.php —|index.php 基本原理: 首頁 index.php 通過獲得地址欄中的路由名稱獲得對應控制器以及控制的方法名,通過require引入到index.php首頁中。通過引入的對 ...
  • 重載的概念 在同一個類中,允許存在一個以上的同名的方法,只要它們的參數個數或者參數類型不同的話就行。 重載的特點 與返回值類型無關,只看參數列表,且參數列表必須不同。(參數個數、參數類型、參數排列順序) 理解:就只要參數列表不要完全一樣就行。和返回值類型無關。 方法的可變個數的形參 在遇到不知道要給 ...
  • 前言 只有光頭才能變強。 文本已收錄至我的GitHub精選文章,歡迎Star : "https://github.com/ZhongFuCheng3y/3y" 相信大家對他也不陌生了,前後端交互中常常就以 來進行 數據交換 。而有的時候,我們也會將 直接保存在資料庫中。 可能就有人不太理解,為什麼要 ...
  • 昨天有使用soap傳輸數據到Webservice,其中字元串類型的都已經傳輸成功,但是有幾個參數傳輸失敗,java伺服器端收到的空值。 因為我是php的,然後接收端是java製作的,其中有幾個參數是list數組類型的,我剛開始將php的數組傳過去,服務端接收到的是空,然後再使用json格式還是不行。 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...