[digital identification] digital instrument identification based on MATLAB [including Matlab source code 693]

Posted by Avalanche on Thu, 17 Feb 2022 21:17:02 +0100

1, Introduction

1 grayscale

The process of converting color image into gray image is called image graying. The pixel value in the color image is determined by the three RGB components, and each component has 0-255 (256) choices. In this way, the pixel value of a pixel can have 16 million possibilities (256256256), while the pixel value of the pixel of the gray image is a special color image with the same three RGB component values, and there are only 256 possibilities. Therefore, in image processing, all kinds of images are often grayed into grayscale images first for subsequent processing and reduce the amount of calculation. Grayscale refers to an image that contains only brightness information and no color information. Black and white photos are grayscale images, which are characterized by continuous changes in brightness from dark to light. Like the color image, the description of gray image still reflects the distribution and characteristics of overall and local chromaticity and brightness levels of the whole image,

Benefits of using grayscale images:
① RGB values are the same.
② The image data, that is, the palette index value, is the actual RGB value, that is, the brightness value.
③ Because it is a 256 tone palette, a byte in the image data represents a pixel, which is very neat.
Therefore, gray image is generally used in image processing.
To represent a grayscale image, you need to quantify the brightness value. There are four methods:

(1) Component method
Taking the brightness of the three components in the color image as the gray value of the three gray images, a gray image can be selected according to the application needs.
(2) Maximum method
The maximum value of the three component brightness in the color image is taken as the gray value of the gray image.
(3) Mean method
The three component brightness in the color image is averaged to obtain the gray value of the gray image.
(4) Weighted average method
According to the importance and other indicators, the three components are weighted and averaged with different weights. Since human eyes are most sensitive to green and least sensitive to blue, a reasonable gray image can be obtained by weighted average of RGB three components according to the following formula, f(i,j)=0.30R(i,j)+0.59G(i,j)+0.11B(i,j)).

2 binarization

Image binarization is to set the gray value of pixels on the image to 0 or 255, that is, the whole image presents an obvious black-and-white effect. A binary image that can still reflect the overall and local characteristics of the image is obtained by selecting an appropriate threshold for 256 gray-scale images. All pixels whose gray level is greater than or equal to the threshold value are determined to belong to a specific object, and their gray level value is 255. Otherwise, these pixels are excluded from the object area, and the gray level value is 0, indicating the background or exceptional object area. In digital image processing, binary image plays a very important role. Firstly, the binarization of image is conducive to the further processing of image, which makes the image simple, reduces the amount of data, and highlights the contour of the object of interest. Secondly, to process and analyze the binary image, we must first binarize the gray image to get the binary image.
Common algorithms for binarization include:

Global binarization: an image includes target object, background and noise. In order to directly extract the target object from multi valued digital image, the most commonly used method is to set a global threshold T, and use t to divide the image data into two parts: pixel groups larger than T and pixel groups smaller than t. Set the pixel value of the pixel group greater than t to white (or black), and the pixel value of the pixel group less than t to black (or white). Global binarization has great defects in representing image details. In order to make up for this defect, local binarization method appears.

Local binarization: divide the whole image into n windows according to certain rules. For each of the N windows, divide the pixels in the window into two parts according to a unified threshold T for binarization. Local binarization also has a defect. The flaw lies in the selection of the uniform threshold. This threshold is obtained without reasonable calculation. Generally, it is the draw value of the window. This leads to the defect of global Binarization in every window. In order to solve this problem, local adaptive binarization method appears.

Local adaptive binarization: on the basis of local binarization, the setting of threshold is more reasonable. The threshold value of this method is calculated by setting a parameter equation for various local characteristics such as the average value E of pixels in the window, the square P of the difference between pixels, and the root mean square value Q between pixels, for example: T=aE+bP+c*Q, where a, B and C are free parameters. In this way, the binary image can better show the details in the binary image.

3 reverse
The actual meaning of reverse color is to reverse the values of R, G and B. If the quantization level of the color is 256, the R, G and b values of the new image are 255 minus the R, G and b values of the original image. This is for all images, including true color images, color images with color palette (also known as pseudo color images), and gray images. The true color map does not have a palette, and each pixel uses 3 bytes to represent the three components of R, G and B. Therefore, the processing is very simple. Write the inverted R, G and b values into the new graph. For example, the color of a point is (0,0,0), and the reverse color is (255255255). For a color map with a palette, the data in its bitmap is only an index value in the corresponding palette. We only need to reverse the colors in the palette to form a new palette, and the bitmap data can be reversed without moving.

2, Source code

close all

plantype = 2;% 1 Is reading pointer, 2 is reading number
file = 'C:\Users\lenovo\Desktop\111186778Plan_Recognise\Summary of digital pointer dial recognition\4.tif';%Pointer
% file = 'H:\picture\digital\28-20.tif';%number
I0 = imread(file);
figure;imshow(I0);title('Picture to be read');
scale = 1000/length(I0);
Irsz = imresize(I0,scale);
clear I0;
%%%%%%2.1: Read pointer%%%%%%
if plantype == 1
    %%%%%%2.1.1:Cut the picture and subtract it from the template picture%%%%%%
    load plan1;
    Ic = imcrop(Irsz,Rect);  
    Isub = Istd - Ic ;
    level = graythresh(Isub);
    Ib = im2bw(Isub,level);
    %  figure;imshow(Ib);title('bwimage');%----------------------------------
    %%%%%%2.1.2: Binarization, find pointer%%%%%%
    topy = find(sum(Ib')>0,1, 'first');
    topx = find(Ib(topy,:)>0,1,'first');
    % figure(1);hold on;
    % plot([O(1),topx],[O(2),topy]);
    %%%%%%2.1.3: reading%%%%%%
    zz = [topx,topy];
    thetazz = -atan( (zz(2)-O(2))/(zz(1)-O(1)) );
    if thetazz<0
        thetazz = thetazz + pi;
    thetakd = -atan( (Kd(2,:)-O(2)) ./ (Kd(1,:)-O(1)) );
    nidx = find(thetakd<0);
    thetakd(nidx) = thetakd(nidx) + pi;

    stepidx = find( thetakd<=thetazz,1,'first');
    para1 = Fsv / (length(Kd)-1);
    para2 = para1*(stepidx-2);
    dushu = para2 + ( thetazz - thetakd(stepidx-1) ) /( thetakd(stepidx)-thetakd(stepidx-1) )*para1;
%%%%%%2.2: Read numbers%%%%%%
else if plantype ==2
        if length(size(Irsz))==3
        clear Irsz;
        If = imfill(Ig);
        Ib = im2bw(If,graythresh(If));
        clear If;
        % figure;imshow(Ib);%-----------------------------------

        SE = strel('square',25);
        Ied = imerode(Ib,SE);
        clear Ib;
        L = bwlabel(Ied);
        stats = regionprops(L,'Area');
        idx = find([stats.Area] == max([stats.Area]));
        [r,c] = find( L==idx );
        clear L;
        rect = [c1,r1,c2-c1,r2-r1];
        Ic = imcrop(Ig,rect);clear Ig;
        % figure;imshow(Ic);%-------------------------------------

        level = graythresh(Ic)/2;   %Binarization of cut image
        Ib=im2bw(Ic,level);clear Ic;
        mp = mean(mean(Ib(1,:))+mean(Ib(end,:))...
        if mp>0.5
            Ib = ~Ib;
        % figure;imshow(Ib);%-------------------

        [m,n] = size(Ib);
        SE = strel('square',round(m/50)); %Preliminary denoising, image opening operation (corrosion before expansion) to remove noise
        Iop = imopen(Ib,SE);
         %  figure;imshow(Iop);%------------------   

        SE = strel('square',round(m/25));    
        Icl = imclose(Iop,SE);clear Iop; %Closed operation, hoping to make up the components of numbers LED Segments are connected together
         %   figure;imshow(Icl);title('Icl')%-------------------
        Ith = bwmorph(Icl,'thin',inf);%The picture is refined to change the number into thin lines
         %  figure; imshow(Ith);%----------------- 

        sRI = sum(Ith');    %Further denoising
        upidx = find(sRI>0, 1, 'first' );
        dnidx = find(sRI>0, 1, 'last');
        pse = round((dnidx-upidx)/40);
        SE = strel('square',pse);
        Ioc = imopen(Icl,SE);
        Ith = bwmorph(Ioc,'thin',inf);clear Iop;
        %  figure; imshow(Ith);title('Ith')%-----------------

        %%%%%%%%%%%%%%%%%%%%%%%%%Character processing and recognition%%%%%%%%%%%%%%%%%%%%%%%%
        %%%cut(The readings are considered to be single row)
        sRI = sum(Ith');   
        upidx = find(sRI>0, 1, 'first' );
        dnidx = find(sRI>0, 1, 'last');
        udidx = upidx:dnidx;
        ud = length(udidx);
        thrlen = ud/2; %determine LED Preliminary threshold of whether the segment is lit
        Ith = Ith(udidx,:);
        % figure; imshow(Ith);%-----------------
        %%Vertical cutting
        BsCI = (sCI>0); %take x Axis projection matrix scI The matrix that becomes 0 is only 1
        dBsCI = diff(BsCI); %look for BscI The first step in the range of elements with 1 in, dBscI Elements in only-1,0,1 Three kinds
        idxp = find(dBsCI==1)+1; %record dBscI The position of the element with 1 in, plus 1 is the corresponding BscI Starting position of element group 1 in
        idxn = find(dBsCI==-1);%record dBscI Zhongwei-1 Element position, corresponding to BscI End position of element group 1 in, and idxp Same length
        if length(idxp)~=length(idxn)
        Cidxchar = [idxp;idxn]; %A 2-row multi column matrix, each column corresponds to a character, and the first line of the column starts at the end of the second line
        dCidxchar = diff(Cidxchar);
        tdCidx = find(dCidxchar < thrlen/2);
        %%The symbol obtained by processing the cutting will“'"Type character returns to the previous character
        if ~isempty(tdCidx)
            ltdCidx = length(tdCidx);
            delemt0 = [];
            delemt1 = [];
            delemt2 = [];
            for i=1:ltdCidx
                lridx = Cidxchar(1,tdCidx(i)):Cidxchar(2,tdCidx(i));   
                tcharI = Ith(:,lridx)';
                if m>1
                    sRtc = sum(tcharI);
                    sRtc = tcharI;
                BsRtc = (sRtc>0);
                dBsRtc = diff(BsRtc);
                cupidx = find(dBsRtc==1,1,'first')+1;
                cdnidx = find(dBsRtc==-1, 1, 'last' );
                hc = cdnidx-cupidx;
                if ~isempty(hc)&&hc<thrlen/2&&cdnidx<ud*0.3
                    delemt0 = [tdCidx(i),delemt0];
                if ~isempty(hc)&&hc>thrlen/2 && hc<thrlen && cdnidx<ud*2/3
                    delemt1 = [tdCidx(i),delemt1];
                if ~isempty(hc)&&hc>thrlen/2 && hc<thrlen && cupidx<ud*2/3
                    delemt2 = [tdCidx(i),delemt2];

            if ~isempty(delemt1)
                Cidxchar(2,delemt1-1) = Cidxchar(2,delemt1);
            if ~isempty(delemt2)
                Cidxchar(1,delemt2+1) = Cidxchar(1,delemt2);
            delemt = sort([delemt0,delemt1,delemt2]);
            if ~isempty(delemt) 
                Cidxchar(:,delemt) = [];

        nchar = length(Cidxchar);%Number of characters in the reading (including decimal point)
        %     %%%%Show cutting effect%---------------------------------
        %     for i=1:nchar
        %         figure;
        %         imshow(Ith(:,Cidxchar(1,i):Cidxchar(2,i)));
        %     end

        %%%Determine each LED Whether the segment is on or not    
        ntab = [ 
                 1 1 1 1 1 1 0  %0
                 0 1 1 0 0 0 0  %1
                 1 1 0 1 1 0 1  %2
                 1 1 1 1 0 0 1  %3
                 0 1 1 0 0 1 1  %4
                 1 0 1 1 0 1 1  %5
                 1 0 1 1 1 1 1  %6
                 1 1 1 0 0 0 0  %7
                 1 1 1 1 1 1 1  %8
                 1 1 1 1 0 1 1  %9
                 0 0 0 0 0 0 0  %.
                 0 0 0 0 0 0 1  %-
        result = [];  
        for i=1:nchar
            lridx = Cidxchar(1,i):Cidxchar(2,i); 
             if length(lridx) < thrlen/2
                charI = [zeros(ud,int16(thrlen)-length(lridx)),Ith(:,lridx)];
                charI = Ith(:,lridx);
             [m,n] = size(charI);
        %               figure;imshow(charI)%-------------------
            %%%%%agd paragraph LED,Since the characters may be inclined, take one fourth as the judgment standard
            xthrlen = thrlen/4;
            ayidx = 1:int16(m/8);      %a paragraph y The axial length is taken as one eighth of the total length
            asCchar = sum( charI( ayidx , :) ); %a paragraph LED of x Axial projection
            BasCchar = asCchar(find(asCchar<2));
            sBa = sum(BasCchar);
            fa = (sBa > xthrlen); %a paragraph LED Is it on 
            gyidx = int16(7*m/16):int16(9*m/16);
            gsCchar = sum(charI(gyidx,:));%g paragraph LED of x Axial projection
            BgsCchar = gsCchar(find(gsCchar<2));
            sBg = sum(BgsCchar);
            fg = (sBg > xthrlen); %g paragraph LED Is it on
            dyidx = int16(7*m/8):m;
            dsCchar = sum(charI(dyidx,:));    %d paragraph LED of x Axial projection
            BdsCchar = dsCchar(find(dsCchar<2));
            sBd = sum(BdsCchar);
            fd = (sBd > xthrlen); %d paragraph LED Is it on
            %%%%%fedc paragraph LED,Project to y Generally, the shaft is not inclined, and half is taken as the judgment standard
            ythrlen = thrlen/2;
            fyidx = 1:int16(m/2); fxidx = 1:int16(n/2);
            fsRchar = sum(charI(fyidx,fxidx)');%f paragraph LED of y Axial projection
            BfsRchar = (fsRchar>0);
            sBf = sum(BfsRchar);
            ff = (sBf>ythrlen);               %f paragraph LED Is it on
            eyidx = int16(m/2):m; exidx = 1:int16(n/2);
            esRchar = sum(charI(eyidx,exidx)');%e paragraph LED of y Axial projection
            BesRchar = (esRchar>0);
            sBe = sum(BesRchar);
            fe = (sBe>ythrlen);               %e paragraph LED Is it on
            cyidx = int16(m/2):m; cxidx = int16(n/2):n;
            csRchar = sum(charI(cyidx,cxidx)');%c paragraph LED of y Axial projection
            BcsRchar = (csRchar>0);
            sBc = sum(BcsRchar);
            fc = (sBc>ythrlen);               %c paragraph LED Is it on
            byidx = 1:int16(m/2); bxidx = int16(n/2):n;
            bsRchar = sum(charI(byidx,bxidx)');%b paragraph LED of y Axial projection
            BbsRchar = (bsRchar>0);
            sBb = sum(BbsRchar);
            fb = (sBb>ythrlen);               %b paragraph LED Is it on

3, Operation results

4, Remarks

Complete code or add QQ 1564658423

Topics: MATLAB image processing