The production of training sample set

Source: Internet
Author: User

In machine learning, depending on the processing problem, the required training samples are different, not all training samples can be searched on the network, all, sometimes need to solve the problem according to their actual needs, the production of their own sample data set.

MATLAB is a more powerful tool for semi-automated sample training sets.

1 run the MATLAB trainingimagelabeler function 1.1 Run Trainingimagelabeler

The program will pop up training image labeler Action box

1.2 Using the Add images to open the image you want

1.3 Click on ROI to start block diagram while capturing multiple ROI zones

1.4 End, save result

When it is time to finish, click Export ROIs, the export variable name will pop up, enter the variable name, such as Poritiveinstances, and click OK.

1.5 Getting the structural body positiveinstances

The structure contains two elements, Imagefilename and objectboundingboxes, respectively.

Imagefilename stores the name of the object

The objectboundingboxes holds the upper-left coordinate and the length-width of the corresponding ROI rectangle box.

(That is, line I represents the upper-left corner of the rectangle for the i roi and the width of the rectangle)

2 extract each ROI corresponding sub-graph from the captured original image to get the sample sub-graph (run SAMPLESMAKER.M function) 2.1 read the original image of the group 2.2 convert the non-grayscale image read into grayscale image 2.3 Get all ROI parameters for the group 2.4 get all ROI corresponding sub-graph of the group, stored in cell 2.5 if you need to visualize the intercepted sub-graphs, then the resulting sub-graphs are saved with the Imwrite function

The following is a complete procedure for making a sample, including three m files, which are implemented sequentially

The first running program (Samplesmaker)

The percent Change program realizes the percentage of the first image to be called Matlab sample-making function (run before running the function)% trainingimagelabeler%% read the original image that the group corresponds to (the path has been added) Imgname = positiveinstances.imagefilename;% Read the imageimginput= imread (imgname); % read in image% convert to the gray Imageif Ndims (imginput) ==3 imginput = Rgb2gray (imginput); end%% gets the parameters for all ROI of the reorganization rectposition (per row The element is the upper-left coordinate of each ROI and the length and width of the rectangle) rectposition=positiveinstances.objectboundingboxes; Numroi=size (rectposition,1);%roi number Img=cell (numroi,1);% storage per image sub-cell for K=1:numroi% upper left (x0,y0), x direction length xl,y direction length yl x0    =fix (Rectposition (k,1)); Y0=fix (Rectposition (k,2)); Xl=rectposition (k,3); yl=rectposition (k,4);    % k ROI corresponds to a sub-figure img{k}=imginput (Y0:Y0+YL-1,X0:X0+XL-1); % Save the corresponding sub-figure of K ROI Imwrite (Img{k},[num2str (k), '. jpg '], ' jpg '); end%% All the ROI information for the target image is guaranteed to be in the cell Rectpositioncell (k means that the target image is the first image) numimg=1;% the target image of the program is the initial image Sampledata.rectpositioncell {numimg}=rectposition;%% The sub-graph of all ROI corresponding to the target is guaranteed to exist in the cell Imgcell K-cell (k means that the target image is the first image) sampledata.imgcell{numimg}=img; Save (' Sampledata.mat ', ' sampleData ')

The second running program (SAMPLESMAKER_ADD)

Percent percent the program is a complement to the Samplesmaker%{samplesmaker implementation is a single image of the sub-map interception, now need to be a sub-image of another object interception, and need to save the results with the previous results, so that the program needs to be improved for the second image of the sub-map, This program should be used%}%% call Matlab sample making function (run before running the function)% trainingimagelabeler%% read the original image of the group corresponding to the original image of the name (the path has been added) Imgname = positiveinstances.imagefilename;% Read the imageimginput= imread (imgname); % read in image% convert to the gray Imageif Ndims (imginput) ==3 imginput = Rgb2gray (imginput); end%% load previous data load (' Sampledata.mat ') ; Numimg_before=size (sampledata.rectpositioncell,2); numimg=numimg_before+1;% the corresponding image number% the number of ROI that has been before this time Numro_before =0;for i=1:numimg_before numro_before=numro_before+size (sampledata.rectpositioncell{i},1); end%% extracts all the sub-graphs of that time and saves all the children % The current number of ROI rectposition=positiveinstances.objectboundingboxes;numroi=size (rectposition,1); for K=1:numROI% upper left corner (x0 , y0), X-Direction length xl,y direction length yl x0=fix (rectposition (k,1)), Y0=fix (Rectposition (k,2)), xl=rectposition (k,3); Yl=rectposition (k    , 4);    % k ROI corresponds to a sub-figure img{k}=imginput (Y0:Y0+YL-1,X0:X0+XL-1); % Save the corresponding sub-figure Imwrite of K roi (Img{k},[num2str (numro_before+k), '. JPG '], ' jpg '); end%% The ROI data of this time to be in the cell Rectpositioncell sampledata.rectpositioncell{numimg}=positiveinstances.objectboundingboxes; Sampledata.imgcell{numimg}=img;save (' Sampledata.mat ', ' sampleData ')

Third running Program (Samplesmaker_all)

Percentage this program consolidates all intercepted IMG into a single data set Imgdata (cell, each cell holds an image) and is loaded with the previous data load (' Sampledata.mat '), and the number of source images intercepted by the total sub-sample numimg=size ( sampledata.rectpositioncell,2)% total sampled images numsamples=0;for i=1:numimg    % i source image contains the number of sampled sub-graphs    n=size ( sampledata.rectpositioncell{i},1);    % Save the first source image contains the sampling sub-figure for    k=1:n        imgdata (numsamples+k) =sampledata.imgcell{i} (k,1);    End    % Total sampled image number    numsamples=numsamples+n;      Endimglabels=ones (numsamples,1); Save (' Imgdata.mat ', ' imgdata ', ' imglabels ');

  

Finally, all captured sample images are saved in the cell Imgdata, and the labels are stored in the Imglabels

After the sample sub-graph is extracted, the next step is to unify the size of all the sample sub-graphs

You can do this in the following code

Percent of the image uniform size clcclearclose all%% Import Sample Data Load (' Imgdata.mat '); Imgdata_sizechange=cell (Size (imgdata)); for K=1:numel ( Imgdata)    imgdata_sizechange{k}=imresize (imgdata{k},[36,36]),% indicates how large the image is to be unified      Imwrite (Imgdata_sizechange{k}, [Num2str (k), '. jpg '], ' jpg '); Endsave (' Imgdata_sizechange.mat ', ' imgdata_sizechange ');

  

The production of training sample set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.