Renesas GR-PEACH OpenCV Development / gr-peach-opencv-project-sd-card_update

Fork of gr-peach-opencv-project-sd-card by the do

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cProcess.cpp Source File

cProcess.cpp

00001 #include "cProcess.hpp"
00002 #include "mStorage.hpp"
00003 
00004 Mat _m_cvtYcrcb2Rgb(uint8_t *_src, int _weight, int _heigh, int _bytes_per_pixel)
00005 {
00006     Mat _dst(_heigh, _weight, CV_8UC3);
00007     int m_stride = _bytes_per_pixel * _weight;
00008     int Y,U,V;
00009     int r,g,b;
00010     for(int ih = 0; ih < _heigh; ih++)
00011     {
00012         for(int iw = 0; iw < _weight; iw++)
00013         {
00014             if(iw % 2 == 0)
00015             {
00016                 Y = _src[ih*m_stride + iw*2 + 0];
00017                 U = _src[ih*m_stride + iw*2 + 1];
00018                 V = _src[ih*m_stride + iw*2 + 3];
00019             }
00020             else
00021             {
00022                 Y = _src[ih*m_stride + iw*2 + 0];
00023                 U = _src[ih*m_stride + iw*2 -1];
00024                 V = _src[ih*m_stride + iw*2 + 1];
00025             }
00026             
00027             r = Y + 1.4075*(V-128);
00028             g = Y - 0.3455*(U-128) - 0.7169*(V-128);
00029             b = Y + 1.7790*(U-128);
00030             
00031             if (r < 0)
00032                 r = 0;
00033             else if (r > 255)
00034                 r = 255;
00035             
00036             if (g < 0)
00037                 g = 0;
00038             else if (g > 255)
00039                 g = 255;
00040             
00041             if (b < 0)
00042                 b = 0;
00043             else if (b > 255)
00044                 b = 255;
00045             
00046             _dst.at<Vec3b>(ih, iw)[0] = r;
00047             _dst.at<Vec3b>(ih, iw)[1] = g;
00048             _dst.at<Vec3b>(ih, iw)[2] = b;
00049         }
00050     }
00051     return _dst;
00052 }
00053 
00054 Mat _m_cvtYcrcb2Gray(uint8_t *_src, int _weight, int _heigh, int _bytes_per_pixel)
00055 {
00056     Mat _dst(_heigh, _weight, CV_8UC1);
00057 
00058     int rows = 0;
00059     int cols = 0;
00060     for(int i = 0; i < (((_weight*_heigh*_bytes_per_pixel)+ 31u) & ~31u); i+=_bytes_per_pixel)
00061     {
00062         if((i-1)/2 > (rows+1)*_weight - 1)
00063         {
00064             rows++;
00065             cols = 0;
00066         }
00067         _dst.at<uchar>(rows,cols++) = _src[i];
00068     }
00069     return _dst;
00070 }
00071 
00072 Mat _m_cvtRgb5652Rgb(uint8_t *_src, int _w, int _h, int _bytes_per_pixel)
00073 {
00074     Mat _res(_h,_w,CV_8UC3,Scalar(0,0,0));
00075     const uint16_t lc_RedMask = 0xF800;
00076     const uint16_t lc_GreenMask = 0x07E0;
00077     const uint16_t lc_BlueMask =  0x001F;
00078     int idx = 0;
00079     for(int i = 0; i < _w*_h*_bytes_per_pixel; i+=2, idx++)
00080     {
00081         uint16_t lo_PixelVal = (_src[ i ]) | (_src[ i + 1 ] << 8);
00082         uint8_t lo_RedVal = (lo_PixelVal & lc_RedMask) >> 11;
00083         uint8_t lo_GreenVal = (lo_PixelVal & lc_GreenMask) >> 5;
00084         uint8_t lo_BlueVal = (lo_PixelVal & lc_BlueMask);
00085 
00086         lo_RedVal <<= 3;
00087         lo_GreenVal <<= 2;
00088         lo_BlueVal <<= 3;
00089         _res.at<Vec3b>(idx)[0] = lo_BlueVal;
00090         _res.at<Vec3b>(idx)[1] = lo_GreenVal;
00091         _res.at<Vec3b>(idx)[2] = lo_RedVal;
00092     }
00093     return _res;
00094 }
00095 
00096 void _m_cvtGray2Rgb565(Mat _gray,uint8_t *_src, int _weight, int _heigh, int _bytes_per_pixel)
00097 {
00098     for(int i = 0;i < _weight*_heigh*_bytes_per_pixel;i += 2)
00099     {
00100         uint8_t grayValue = _gray.at<uchar>(((i/2)/_weight)%_heigh,(i/2)%_weight);
00101         _src[i+1] = ((grayValue>>3)<<3)|(grayValue>>5);
00102         _src[i] = ((grayValue>>2)<<5)|(grayValue>>3);
00103     }
00104 }
00105 
00106 void writeMatToTxt(Mat _mat, char* file_name)
00107 {
00108     int channels;
00109     FILE *myFileW = fopen(file_name,"w");
00110     if(myFileW != NULL){
00111         togle_led(LED_BLUE);
00112         channels = _mat.channels();
00113         if(channels == 1)
00114         {
00115             togle_reset(LED_RED,LED_GREEN);
00116         }
00117         for(int i=0; i<_mat.rows; i++)
00118         {
00119             for(int j=0; j<_mat.cols; j++)
00120             {
00121                 int r,g,b;
00122                 char _buf[15];
00123                 if(channels == 1)
00124                 {
00125                     r = _mat.at<uchar>(i,j);
00126                     sprintf(_buf,"%d,",r);
00127                     fprintf(myFileW, "%s", _buf);
00128                 }
00129                 else{
00130                     r = _mat.at<Vec3b>(i, j)[0];
00131                     g = _mat.at<Vec3b>(i, j)[1];
00132                     b = _mat.at<Vec3b>(i, j)[2];
00133                     sprintf(_buf,"%d,%d,%d,",r,g,b);
00134                     fprintf(myFileW, "%s", _buf);
00135                 }
00136             }
00137         }
00138         fclose(myFileW);
00139     }
00140     else{
00141         togle_led(LED_RED);
00142     }
00143 }
00144 
00145 void togle_led(DigitalOut led)
00146 {
00147     led = 1;
00148     wait(0.1);
00149     led = 0;
00150     wait(0.1);
00151 }
00152 
00153 void togle_reset(DigitalOut led1, DigitalOut led2)
00154 {
00155     led1 = 1;
00156     led2 = 1;
00157     wait(0.1);
00158     led1 = 0;
00159     led2 = 0;
00160     wait(0.1);
00161 }
00162 
00163 uint8_t* cvtMat2RGBA444(int w, int h, Mat src) // w<=40; h <=40
00164 {
00165     uint8_t res[100*100*2 + 6];
00166     
00167     memset(res,0,sizeof(res));
00168     /*6 bytes header*/
00169     res[0] = w & 0x00FF;
00170     res[2] = h & 0x00FF;
00171     res[4] = 0x02;
00172     
00173     int counter = 6;
00174     for(int i = 0; i < h;i++)
00175     {
00176         for(int j = 0; j < w; j++)
00177         {
00178             int b = src.at<Vec3b>(i,j)[0];
00179             int g = src.at<Vec3b>(i,j)[1];
00180             int r = src.at<Vec3b>(i,j)[2];
00181             
00182             res[counter++] = (g & 0xF0) | (b >> 4);//Green, Blue
00183             res[counter++] = 0xF0 | (r>>4);//Alph, Red
00184         }
00185     }
00186     return res;
00187 }