当前位置:网站首页>[2D code image correction and enhancement] simulation of 2D code image correction and enhancement processing based on MATLAB
[2D code image correction and enhancement] simulation of 2D code image correction and enhancement processing based on MATLAB
2022-06-28 02:38:00 【FPGA and MATLAB】
1. Software version
matlab2013b
2. Algorithm flow overview
The image region and boundary of the two-dimensional code are obtained by morphological processing ;
The convex hull algorithm is used to calculate the point set on the boundary ;
Then according to the point set to find the four vertices of the two-dimensional code ,
Then perspective transformation correction , The points in each lattice are obtained by two-dimensional code segmentation .
Normalize the two-dimensional code image . Complete QR code correction .
Then do experiments to compare with other methods using edge detection plus hough The recognition rate of the image corrected by transformation .
Perform illumination enhancement before binarization , Homomorphic filtering and histogram equalization are used to do , Then use ostu Method for binarization .
By locating the position of the three corners of the QR code , And then through expansion treatment , And connect the area . Delete the connected area that is not where the three probe elements are located , Then the remaining connected areas are corroded , Edge detection , The point sets on the obtained edges are connected by convex hull method ,
Roughly determine the boundary of the QR code , Then use the shortest distance to the four external lines to determine the four vertices .
3. Part of the source code
clc;
clear all;
close all;
warning off;
addpath 'func\'
%filename ='1.jpg';
%filename ='2.jpg';
%filename ='3.jpg';
filename ='4.jpg';
X = imread(filename);
% Get image related information
[R,C,K] = size(X);
if K == 3
f = rgb2gray(X);
else
f = X;
end
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use wavelet filtering + An improved median filtering scheme
figure
subplot(141);
imshow(f);
title(' original image ');
f = imadjust(f,[0.1 0.8],[]);
f = func_wavelet_filter(f);
subplot(142);
imshow(f);
title(' Image after wavelet filtering and ray enhancement ');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% be based on OSTU Method
G = f;
f = func_ostu(f);
f =~f;
subplot(143);
imshow(f);
title('ostu Binary image processing ');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
% Morphological expansion
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
SE = strel('disk',8);
I = imdilate(f,SE);
% Delete small areas
I = bwareaopen(I,20000);
% Find the center of gravity of the whole picture
subplot(144);
imshow(I);
title(' Morphological dilation image ');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Mark connected domains
[L,num]=bwlabel(I,8);
figure
subplot(141);
imshow(L);
% Eliminate irrelevant connected domains , Here, the elimination measures are improved , In the previous method, many test images cannot be processed correctly
[R,C,K] = size(I);
L2 = zeros(R,C);
for i=1:num
[r,c]=find(L==i); % Calculate the coordinate
if isempty(r) == 0 & isempty(c) == 0
a2(i) = min(r); % Calculate the coordinate
a1(i) = max(r); % Calculate the coordinate
b2(i) = min(c); % Calculate the coordinate
b1(i) = max(c); % Calculate the coordinate
% Judge whether it is the correct area according to the area
Lss = a1(i) - a2(i);
Wss = b1(i) - b2(i);
if (Lss>=0.9*Wss & Lss<=1.1*Wss) | (Wss>=0.9*Lss & Wss<=1.1*Lss)
for j = 1:length(r)
L2(r(j),c(j)) = L(r(j),c(j));
end
end
end
end
SE = strel('disk',8);
T=imerode(L2,SE);
T=imfill(T,'holes');
subplot(142);
imshow(T);
title(' Morphological corrosion ');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Edge extraction
T2=edge(T,'prewitt');
subplot(143);
imshow(T2);
title(' Edge extraction ');
% Point set of convex hull to be judged , This part has been improved , The original method is incomplete , Will not be able to detect edge problems
[y,x] = find(T2==1);
img = ones(size(T2));
p = [];
for i=1:length(x)
img(y(i),x(i))=0;
p=[p;x(i),y(i)];
end
subplot(144);
imshow(img);
% Let's calculate the convex hull
[t,index] = max(p(:,2));
% find y The biggest point
tmp_p = p(index,:);
% Set a and y The largest point is parallel to the point
tmp_heng =[tmp_p(1)+100,tmp_p(2)];
for i=1:length(p)
% Find the sum of each point y The angle of the largest point , The angle between oneself and oneself NAN
Ang(i) = func_angle(tmp_heng,p(i,:),tmp_p);
end
Ang = Ang';
p =[p,Ang];
% Sort by the third column , The third column is the included angle degrees
p = sortrows(p,3);
%re It's like a stack
re{1} = p(length(p),1:2);
re{2} = p(1,1:2);
re{3} = p(2,1:2);
top = 3;
for i=3:length(p)-1
while func_multi(p(i,1:2),re{top-1},re{top})>=eps
top=top-1;
if top<=1
break;
end
end
top=top+1;
re{top}=p(i,1:2);
end
% Here is the line connecting the points found on the convex hull
for i=2:top
imgs = drawline(img,re{i-1}(1),re{i-1}(2),re{i}(1),re{i}(2));
end
imgs = drawline(img,re{1}(1),re{1}(2),re{top}(1),re{top}(2));
figure
subplot(231);
imshow(imgs)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculate the four vertices
jiaodian2 = func_cal_point(imgs);
% Here, the similarity matching method is used to modify the initial vertex
[jiaodian3,jiaodian4] = func_cal_point_adjust(jiaodian2,G);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% correction
Z=func_adjust(jiaodian4,G);
subplot(232);
imshow(uint8(G))
hold on
for i = 1:4
plot(jiaodian2(i,2),jiaodian2(i,1),'g.');
hold on;
text(jiaodian2(i,2),jiaodian2(i,1),num2str(i));
end
subplot(233);
imshow(uint8(G))
hold on
for i = 1:4
plot(jiaodian4(i,2),jiaodian4(i,1),'r.');
hold on;
plot(jiaodian3(i,2),jiaodian3(i,1),'g.');
hold on;
end
subplot(234);
imshow(uint8(G))
hold on
for i = 1:3
line([jiaodian4(i,2),jiaodian4(i+1,2)],[jiaodian4(i,1),jiaodian4(i+1,1)],'Color',[0 1 0]);
hold on;
end
line([jiaodian4(end,2),jiaodian4(1,2)],[jiaodian4(end,1),jiaodian4(1,1)],'Color',[0 1 0]);
subplot(235);
Z2 = histeq(uint8(Z));
imshow(Z2);
subplot(236);
thresh=graythresh(Z2);
f = im2bw(Z2,thresh);
imshow(f);
%%
% Segment the final result
[R,C] = size(Z2);
Z3 = edge(Z2,'canny');
% Line scan
tmp1 = 0;
tmp2 = 0;
for i = 2:R
tmp1 = f(i-1,:);
tmp2 = f(i,:);
diff1(i) = abs(mean(tmp1 - tmp2));
end
% Search minimum
[pks1,locs1]=findpeaks(-1*diff1,'minpeakdistance',3); % Find peaks
% Column scan
tmp1 = 0;
tmp2 = 0;
for i = 2:C
tmp1 = f(:,i-1);
tmp2 = f(:,i);
diff2(i) = abs(mean(tmp1 - tmp2));
end
% Search minimum
[pks2,locs2]=findpeaks(-1*diff2,'minpeakdistance',3); % Find peaks
locs1 = [locs1];
locs2 = [locs2];
% Get normalized QR code
QRcode = zeros(length(locs1),length(locs2));
for i = 1:length(locs1)
for j = 1:length(locs2)
QRcode(i,j) = f(locs1(i),locs2(j));
end
end
% compensate
QRcode = QRcode;
figure;
subplot(3,2,1);
imshow(Z2);
subplot(3,2,2);
imshow(Z3);
subplot(3,2,3);
plot(diff1);
hold on;
plot(locs1,diff1(locs1),'k^','markerfacecolor',[1 0 0]);
title(' Horizontal edge projection ');
subplot(3,2,4);
plot(diff2);
hold on;
plot(locs2,diff2(locs2),'k^','markerfacecolor',[1 0 0]);
title(' Vertical edge projection ');
subplot(3,2,5);
imshow(Z2,[]);
title(' Perspective corrected image ');
subplot(3,2,6);
imshow(QRcode,[]);
title(' Normalized QR code ');
4. Simulation results




A10-38
边栏推荐
- Solve the problem that the page cannot scroll when ionic4 uses the hammerjs gesture press event
- MySQL optimization tips
- Dynamic Host Configuration Protocol
- Jenkins - groovy postbuild plug-in enriches build history information
- Leetcode topic [array] -228- summary interval
- Keil "St link USB communication error" solution
- 【云原生】-Docker安装部署分布式数据库 OceanBase
- Jenkins - built in variable access
- Data governance and data standards
- 关于st-link usb communication error的解决方法
猜你喜欢

Starting sequence of Turing machine

Interpretation of the source code of scheduledthreadpoolexecutor (II)

【历史上的今天】6 月 2 日:苹果推出了 Swift 编程语言;电信收购联通 C 网;OS X Yosemite 发布

Skills in schematic merging

A set of sai2 brushes is finally finished! Share with everyone!

【历史上的今天】6 月 11 日:蒙特卡罗方法的共同发明者出生;谷歌推出 Google 地球;谷歌收购 Waze

数据清洗工具flashtext,效率直接提升了几十倍数

【历史上的今天】6 月 25 日:笔记本之父诞生;Windows 98 发布;通用产品代码首次商用

SQL injection Bypass (2)

Complex and inefficient logistics? Three steps to solve problems in enterprise administration
随机推荐
Jenkins - email notification plug-in
【历史上的今天】6 月 2 日:苹果推出了 Swift 编程语言;电信收购联通 C 网;OS X Yosemite 发布
【历史上的今天】6 月 3 日:微软推出必应搜索引擎;Larry Roberts 启动阿帕网;Visual Basic 之父出生
【方块编码】基于matlab的图像方块编码仿真
The system administrator has set the system policy to prohibit this installation. Solution
4G-learn from great partners
How to use data-driven "customer lifecycle management" to improve lead conversion rate and customer satisfaction?
Résumé de la graduation
High reliability application knowledge map of Architecture -- the path of architecture evolution
Graduation summary
【历史上的今天】6 月 5 日:洛夫莱斯和巴贝奇相遇;公钥密码学先驱诞生;函数语言设计先驱出生
New choice for database Amazon Aurora
畢業總結
云平台kvm迁移本地虚拟机记录
OS module and os Learning of path module
Machine learning (x) reinforcement learning
Wangxinling, tanweiwei Shanhai (extended version of Chorus) online audition lossless FLAC Download
Leetcode topic [array] -228- summary interval
Mysql数据库基础:DML数据操作语言
ScheduledThreadPoolExecutor源码解读(二)