当前位置:网站首页>[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
边栏推荐
- Machine learning (x) reinforcement learning
- OS module and os Learning of path module
- 图灵机启动顺序
- Jenkins - 内置变量访问
- 系统管理员设置了系统策略,禁止进行此安装。解决方案
- Ionic4 realizes half star scoring
- 数据清洗工具flashtext,效率直接提升了几十倍数
- 云平台kvm迁移本地虚拟机记录
- yarn下载报错There appears to be trouble with your network connection. Retrying.
- High reliability application knowledge map of Architecture -- the path of architecture evolution
猜你喜欢

批阅2022春季学期课程小论文提交情况

SQL injection bypass (3)

Based on am335x development board arm cortex-a8 -- acontis EtherCAT master station development case

File transfer protocol --ftp

SQL 注入绕过(三)

SQL injection bypass (2)

【历史上的今天】5 月 29 日:共享软件先驱诞生;ChromeBox 推出;VoodooPC 创始人出生

Jenkins - email notification plug-in

Starting sequence of Turing machine

Jenkins - data sharing and transfer between copy artifact plug-in builds
随机推荐
ScheduledThreadPoolExecutor源码解读(二)
【历史上的今天】6 月 25 日:笔记本之父诞生;Windows 98 发布;通用产品代码首次商用
Solve the problem that the page cannot scroll when ionic4 uses the hammerjs gesture press event
I forgot my cell phone today
Is it safe for qiniu to open an account? How do I open an account online?
MySQL interview question set
Ti am3352/54/59 industrial core board hardware specification
Cloud platform KVM migration local virtual machine records
Wangxinling, tanweiwei Shanhai (extended version of Chorus) online audition lossless FLAC Download
The system administrator has set the system policy to prohibit this installation. Solution
共阳极数码管真值表
SQL injection Bypass (2)
General timer and interrupt of stm32
Prometheus 2.27.0 新特性
贪吃蛇 C语言
原理图合并中的技巧
Jenkins - 訪問 Jenkins 自定義參數變量,處理變量值中含有空格
文件传输协议--FTP
架构高可靠性应用知识图谱 ----- 微服务架构图谱
11 timers for STM32F103