Principles and implementation of PHP image data reading: This post was last edited by hudie631489527 on 23:34:34
I haven't written an article for a long time, and my hands start to itch.
What we bring to you this time is a content about extracting image data from PHP.
There is no new technology here, just to let everyone know that PHP can also process images and the principles and methods of processing
A simple DEMO is provided here to read the data 'specific letters + number' at the specified position in the image (in this example, the image is from the cacti monitoring traffic image)
Separate the orders in the following two steps
I first perform the modulo operation. the implementation steps are as follows:
1. locate the image to recognize the region and retrieve this part from the image.
2. extract the modulus of this part and record it to the modulo file for later matching. (to obtain the complete modulo, you need to test multiple images)
II. image text extraction steps:
1. locate the image to recognize the region and retrieve this part from the image.
2. extract this part of the model and compare it with the data in the saved model file to find the data corresponding to this model.
The two steps are separated and I personally think it is necessary. although they have many similarities, they are two different processes after all.
I just thought about it for a moment, but I still thought I would like to introduce the second part first, and then introduce the first part, so that everyone may have a point of interest. first I will see the results and hook up your desires. hahaha...
Officially entered the topic:
First, release the class to be operated. click the right arrow below to view the class file.
Continued = $ continued; $ this-> warnning_data = $ warnning_data; $ this-> hourstep = $ hourstep ;}} /*************************************** ** create a template image and an array file ****************************** **/function readPicDot ($ picfile) {$ res = imagecreatefrompng ($ picfile); $ size = getimagesize ($ picfile); $ this-> width = $ size [0]; $ this-> height = $ size [1]; $ data = array (); for ($ j = 0; $ j <$ this-> height; ++ $ j) {/** obtain the required data segment * 262, 273 day.png */if ($ j> 262 & $ j <273) {for ($ I = 0; $ I <$ this-> width; ++ $ I) {if ($ I> 420) {$ rgb = imagecolorat ($ res, $ I, $ j); $ rgbarray = imagecolorsforindex ($ res, $ rgb ); if ($ rgbarray ['red'] <145 & $ rgbarray ['green'] <145 & $ rgbarray ['blue'] <145) {$ data [$ I] [$ j] = 1;} else {$ data [$ I] [$ j] = 0 ;}}} return $ data ;} function readMaxOutDot ($ picfile) {$ res = imagecreatefrompng ($ picfile); $ size = getimagesize ($ Picfile); $ this-> width = $ size [0]; $ this-> height = $ size [1]; $ data = array (); for ($ j = 0; $ j <$ this-> height; ++ $ j) {/** obtain the required data segment * 262,273 day.png */if ($ j> 262 & $ j <273) {for ($ I = 0; $ I <$ this-> width; ++ $ I) {if ($ I <180 & $ I> 90) {$ rgb = imagecolorat ($ res, $ I, $ j); $ rgbarray = imagecolorsforindex ($ res, $ rgb ); if ($ rgbarray ['red'] <145 & $ rgbarray ['green'] <145 & $ rgbarray ['blue'] <145) {$ data [$ I] [$ j] = 1;} else {$ data [$ I] [$ j] = 0 ;}}} return $ data ;} function readHourDot ($ picfile) {$ res = imagecreatefrompng ($ picfile); $ size = getimagesize ($ picfile); $ this-> width = $ size [0]; $ this-> height = $ size [1]; $ data = array (); for ($ j = 0; $ j <$ this-> height; ++ $ j) {/** obtain the required data segment * popup_module_view_hour.png * 25,245 #35,480 **/if ($ j> 30 & $ j <242) {for ($ I = 0; $ I <$ this-> width; ++ $ I) {if ($ I> 66 & $ I <486) {$ rgb = imagecolorat ($ res, $ I, $ j); $ rgbarray = imagecolorsforindex ($ res, $ rgb ); if ($ rgbarray ['red'] <50 & $ rgbarray ['green']> 180 & $ rgbarray ['blue'] <50) | ($ rgbarray ['red'] <145 & $ rgbarray ['green'] <145 & $ rgbarray ['blue'] <145 )) {$ data [$ I] [$ j] = 1;} else {$ data [$ I] [$ j] = 0 ;}}} return $ data ;} function cutData ($ data) {$ zimo = $ tmp = array (); $ num = $ len = 0; $ count_start = false; foreach ($ Data as $ k => $ v) {if (in_array (1, $ v) {$ zimo [$ num] [] = $ v; $ count_start = true; $ len ++; if ($ len> = 6) {// solve the problem of font concatenation $ len = 0; $ num ++; // add 1} else {$ len = 0; $ count_start = false;} if (! $ Count_start) {$ num ++ ;}// var_dump (array_keys ($ zimo )); // Fix the letter T $ res = $ this-> createZimoArray ($ zimo); // $ this-> debugPic ($ zimo [35]); // die (); // Print the complete modulo dot matrix/* foreach ($ zimo as $ k => $ v) {if ($ k = 35) {echo'
'; $ This-> debugPic ($ v) ;}} * // var_dump ($ res); end ($ res ); // move the data pointer $ last_two_data = prev ($ res); if (! Strcmp ($ last_two_data, '000000') {// indicates a problem with T splitting. $ last_three_data = prev ($ res); $ last_three_data_real = $ last_three_data. $ last_two_data; // add it to the T letter foreach ($ res as $ k => $ v) {if (! Strcmp ($ v, $ last_three_data) {$ res [$ k] = $ last_three_data_real ;}}return $ res;} // constructs the model array function createZimoArray ($ data) {$ str = array (); foreach ($ data as $ k => $ v) {$ str [$ k] = ''; foreach ($ v as $ ks =>$ vs) {$ str [$ k]. = implode ('', $ vs) ;}return $ str ;}// read the data model into the file function storeZimo ($ data) {$ str = $ this-> createZimoArray ($ data); $ picwords = APP_CACHE_PATH. 'picwords. php'; swritefile ($ picwords ,"
");} // Function matchZimo ($ str_arr) {$ need_str =''; $ picwords = $ this-> zimo_file; if (empty ($ this-> zimo_info) {$ this-> zimo_info = include_once ($ picwords);} foreach ($ str_arr as $ k => $ v) {foreach ($ this-> zimo_info as $ ks =>$ vs) {similar_text ($ v, $ vs, $ tmp); if ($ tmp> 99) $ need_str. = $ ks ;}}return $ need_str;} function debugPic ($ data) {echo''; Foreach ($ data as $ k => $ v) {echo'
'; Foreach ($ v as $ ks =>$ vs) {echo $ ."
";} Echo'
';} Echo'';} Function handleHourData ($ data) {$ data_info = array (); foreach ($ data as $ k = >$ v) {$ get = true; $ data_info [$ k] = 0; foreach ($ v as $ ks = >$ vs) {if ($ vs = 1 & $ get) {$ get = false;} if (! $ Get) {$ data_info [$ k] ++;} else {unset ($ data [$ k] [$ ks]) ;}} return $ data_info ;} /************************************ to retrieve data method ***************************** // total traffic function getPicWords ($ file) {$ need_str = ''; // Read Image dot matrix information $ data = $ this-> readPicDot ($ file); // process cut data, and write the array $ data = $ this-> cutData ($ data); // match the model data $ need_str = $ this-> matchZimo ($ data); return $ need_str ;} // get the maximum traffic speed function getMaxSpeed ($ file) {$ need_str = ''; // read the image dot matrix information $ data = $ this-> readMaxOutDot ($ file ); // process the cut data and write the array $ data = $ this-> cutData ($ data ); // match the model data $ need_str = $ this-> matchZimo ($ data); return $ need_str;} // analyze the traffic peak function parseFlow ($ file, $ info) in the hour chart) {// get the maximum traffic speed $ maxflow = floatval ($ this-> getMaxSpeed ($ file )); // Read Image dot matrix information $ data = $ this-> readHourDot ($ file); // data abstraction deformation $ data_info = $ this-> handleHourData ($ data ); $ max = max ($ data_info); $ radio = (float) ($ maxflow/$ max ); // traffic calculation ratio column $ timeradio = ceil ($ this-> hourstep/60); // number of cells per minute $ continued_time = $ this-> continued * $ timeradio; // quantize the duration with the dot matrix length $ count = 0; foreach ($ data_info as $ v) {if ($ v * $ radio> $ this-> warnning_data) {$ count ++; if ($ count >=$ continued_time) {echo "ServerID: $ info [sid] IP: $ info [ip] flow over :". $ this-> warnning_data.'
'; // Send an email break;} else {$ count = 0 ;}}}?>
I. Introduction to the text extraction function
Image to be operated. The text below is the text we need to extract.
After code processing, you can obtain the image.
ReadPicDot ($ picfile); // Read Image Dot Matrix Information?>
This image is represented by a group of 0 and 1. I believe everyone can see this string.
At this point, we have reduced the scope of our operation object, and now the problem arises. how can we get the number of characters and characters in the image?
This problem is also the most difficult. now we start cutting.
After code processing, you can obtain an example:
CutData ($ data); // process the cut data and write it into an array?>
Each block corresponds to one character (this is not a real character, but it is still unknown data, waiting for us to convert)
Careful friends should see that the second and third parts of inverted data actually represent a T, which will be processed separately in the subsequent code.
I want to explain it here because I calculated the width of the graph and the height of 10 pixels based on 6 pixels for each character. some may have asked, so how can you use these two numbers for calculation? I would like to say that these are observed in the text in this image, and 5 or 7 won't work, so I chose 6 as the width, and the height can easily be seen as 10.
MatchZimo ($ data); // match the mode data?>
Then convert the array that saves these strings to '123... 'String, and match with the model File (here we use it first, we will introduce it later), and then get the data we want
II. generate a model
The process of generating the model is the same as above, but at the end we need to manually identify and record the strings and corresponding characters in the array storing the characters. this step must be completed before the above, otherwise, there is no template file that can be used for matching.
Maybe the students want to say that this still requires manual identification, not intelligent. But there is no ready-made model file, so we must manually generate it. If the student studied the font model, he could see at a glance which type of model is used for the text in the image and which file is available at the same time, then he could just borrow it without manual generation.
Now, this article is here to get the fixed text in the file.
A demo with complete code. if you are interested, you can download and exchange the DEMO. click to download the DEMO.
Decompress the file and run pick_pic_data.php. you can specify the image file as needed.
Next we will introduce how to respond by detecting situations that exceed a fixed value based on waveforms
Reply to discussion (solution)
Thank you for sharing ......
Thank you for sharing.
Thank you for sharing your learning ....
Hello, I have a question to ask. In the piczimo. php file, how does one convert 0-9 to the following mode.
'0' => '123 ',
'1' => '123 ',
What about the A-Z? Thank you for your advice.