CQ出版 Interface記事用サンプル。 トラ技カメラ+FIFOからデータを得て、 エリア判定後、デバイスに保存

Dependencies:   SDFileSystem mbed

Revision:
1:c822ea77c7b2
Parent:
0:1648bb4e70e5
Child:
2:3aed552f3385
--- a/main.cpp	Thu Sep 26 06:59:31 2013 +0000
+++ b/main.cpp	Fri Sep 27 03:19:01 2013 +0000
@@ -5,6 +5,8 @@
   http://mbed.org/users/diasea/code/OV7670_with_AL422B_Color_Size_test/
 
 */
+#include <string.h>
+#include <math.h>
 #include "mbed.h"
 #include <algorithm>
 #include "ov7670.h"
@@ -33,15 +35,26 @@
 int sizex = 0;
 int sizey = 0;
 
-int create_header(FILE *fp, int width, int height) ;
+#define AREA_X 32
+#define AREA_Y 24
+
+char canvas[160][120] ; //18.5KB
+signed int  AreaDiff[2][5][5];   // 5x5 Area = 32x24dot Each Block
+int face_bank = 0;
+
+
 
 int main() {
     char color_format = 0;
     char c;
+    int d1,d2 ;
     int mx, pixel , i;
-    int pixc;
-    char packet[40] = { 0 };
-    
+    int pixc,  pixelg;
+    int sort[3];    
+    int ay = 0;
+
+    memset( AreaDiff, '\0', sizeof(AreaDiff));
+
     pc.baud(115200);
 RESJMP:
     //pc.printf("Camera resetting..\r\n");
@@ -75,6 +88,8 @@
     // CAPTURE and SEND LOOP
     while(1)
     {
+        memset( &AreaDiff[face_bank], '\0', sizeof(AreaDiff[face_bank]));
+        
         pc.printf("Hit Any Key to send RGBx160x120 Capture Data.\r\n") ;
         while(!pc.readable()) ;
         c = pc.getc() ;
@@ -86,24 +101,110 @@
         while(camera.CaptureDone() == false);
         camera.ReadStart();
         pixc = 0;
+        ay = 0;
         for (int y = 0;y < SIZEY;y++) {
-            int r,g,b,d1,d2 ;
-            mx = 0;
+            ay = y / AREA_Y;    
             for (int x = 0;x < SIZEX;x++) {
                 d1 = camera.ReadOneByte() ; // upper nibble is XXX , lower nibble is B
                 d2 = camera.ReadOneByte() ; // upper nibble is G   , lower nibble is R
                 pixel = ((unsigned int)d2 << 8) + ((unsigned int)d1 & 0xff);
-
+                
+                // Make GrayScale
+                // make 6bit Code on Each Color
+                sort[0] = ((pixel >> 11) & 0x1f)<<1;    // r
+                sort[1] = ((pixel >>  5) & 0x3f);       // g
+                sort[2] = ((pixel      ) & 0x1f)<<1;    // b
+                
+                int max = 0, min = 0xffff;
+                for (int i=0;i<3;i++){
+                    if (sort[i] > max) max = sort[i];
+                    if (sort[i] < min) min = sort[i];
+                }
+                int gray = ((max + min) / 2) ;  
+                
+                pixelg = ((gray>>1)<<11) + (gray<<5) + (gray>>1);
+                pixel = pixelg;
+                canvas[x][y] = (char)( pixelg ) & 0x3f;    // 6bit gray scale
+                
+                // Make Difference AreaData
+                AreaDiff[face_bank][x/AREA_X][ay] += ( gray << 2 );    // 8bit scaler! 
+                
                 // send <CRLF> and Count on each 16dot.
                 if ( !(x%16) ){
                     pc.printf("\r\n%04x: ",pixc ) ;
                     pixc +=16;
                 }
                 pc.printf("%04x ",pixel) ;
+                
             }
             
         }
         camera.ReadStop();
+        
+        // Difference Check!!
+        pc.printf("\r\n" ) ;
+        int backArea = ( face_bank + 1 )&0x01;
+        for (int y = 0; y < 5; y++) {
+            pc.printf("Diff: " ) ;
+            for (int x = 0; x < 5; x++) {
+                int dif = abs( AreaDiff[backArea][x][y] - AreaDiff[face_bank][x][y] );
+                if (dif >= 20000) {
+                    pc.printf("X%5d " , dif) ;
+                }else{
+                    pc.printf("O%5d " , dif) ;
+                }
+            }
+            pc.printf("                                             " ) ;
+            pc.printf("                                             \r\n" ) ;
+        }
+        
+        
+                
+/*
+        // Median Filter
+        for (int y = 1;y < SIZEY-1;y++) {
+            for (int x = 1;x < SIZEX-1;x++) {
+                int sortM[9] = { 0 };
+                int p=0;
+
+                // Get Around Pixels
+                for (int k = -1; k < 2; k++) {
+                    for (int j = -1; j < 2; j++) {
+                        sortM[p++] = canvas[x+j][y+k];   //grayscale data
+                    }
+                }
+                // sort now...
+                for (int k = 0; k < 8; k++){
+                    for (int j = 8; j > k; j--) {
+                        if (sortM[j] < sortM[j - 1]) {
+                            int dmy = sortM[j];
+                            sortM[j] = sortM[j - 1];
+                            sortM[j - 1] = dmy; 
+                        }
+                    }
+                }
+
+                canvasM[x][y] = sortM[4]; // Center Data!
+            }            
+        }
+        
+        // Send Median Display
+        pixc = 0;
+        for (int y = 0;y < SIZEY;y++) {
+            for (int x = 0;x < SIZEX;x++) {
+                // send <CRLF> and Count on each 16dot.
+                if ( !(x%16) ){
+                    pc.printf("\r\n%04x: ",pixc ) ;
+                    pixc +=16;
+                }
+                pc.printf("%04x ",canvasM[x][y]) ;
+            }
+        }
+        */
+        face_bank++;
+        face_bank&=0x01;
+
+
         //pc.printf("\r\n") ;
     }
 }
@@ -111,6 +212,7 @@
 
 
 /*
+int create_header(FILE *fp, int width, int height) ;
 
 #if defined(BITMAPFILE) || defined(BAYERBITMAPFILE)
 #define BITMAPFILE