当前位置:网站首页>[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
边栏推荐
- How technicians become experts in technical field
- Dynamic Host Configuration Protocol
- Ti am3352/54/59 industrial core board hardware specification
- stm32f1中断介绍
- 畢業總結
- 架构高可靠性应用知识图谱 ----- 架构演进之路
- File transfer protocol --ftp
- KVM相关
- Based on am335x development board arm cortex-a8 -- acontis EtherCAT master station development case
- 技术人员如何成为技术领域专家
猜你喜欢

Opencv——霍夫变换以及遇到的一些问题

Jenkins - data sharing and transfer between copy artifact plug-in builds

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

【历史上的今天】6 月 8 日:万维网之父诞生;PHP 公开发布;iPhone 4 问世

【历史上的今天】6 月 17 日:术语“超文本”的创造者出生;Novell 首席科学家诞生;探索频道开播

Teach you how to realize pynq-z2 bar code recognition

Cvpr22 collected papers | hierarchical residual multi granularity classification network based on label relation tree

原理图合并中的技巧

STM32的通用定时器与中断

【历史上的今天】6 月 25 日:笔记本之父诞生;Windows 98 发布;通用产品代码首次商用
随机推荐
启牛开户安全吗?怎么线上开户?
Snake C language
JS implementation of Slide Puzzle verification
KVM相关
4G-learn from great partners
Truth table of common anode digital tube
Opencv——几何空间变换(仿射变换和投影变换)
There appears to be a failure with your network connection Retrying.
批阅2022春季学期课程小论文提交情况
设计电商秒杀系统
后勤事务繁杂低效?三步骤解决企业行政管理难题
Jenkins - data sharing and transfer between copy artifact plug-in builds
【历史上的今天】6 月 5 日:洛夫莱斯和巴贝奇相遇;公钥密码学先驱诞生;函数语言设计先驱出生
I forgot my cell phone today
【模糊神经网络】基于matlab的模糊神经网络仿真
Skills in schematic merging
【历史上的今天】6 月 25 日:笔记本之父诞生;Windows 98 发布;通用产品代码首次商用
MySQL optimization tips
数据清洗工具flashtext,效率直接提升了几十倍数
Wangxinling, tanweiwei Shanhai (extended version of Chorus) online audition lossless FLAC Download