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

来源: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
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...