Subversion Repositories seema-scanner

Rev

Rev 139 | Rev 142 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 139 Rev 141
Line 4... Line 4...
4
#include <cstdint>
4
#include <cstdint>
5
#endif
5
#endif
6
 
6
 
7
#include <stdio.h>
7
#include <stdio.h>
8
 
8
 
-
 
9
#include <png.h>
-
 
10
#include <zlib.h>
-
 
11
 
9
namespace cvtools{
12
namespace cvtools{
10
 
13
 
-
 
14
//bool imwriteTiff(std::string filename, cv::Mat img){
-
 
15
 
-
 
16
//    int channels = img.channels();
-
 
17
//    int width = img.cols, height = img.rows;
-
 
18
//    int depth = img.depth();
-
 
19
 
-
 
20
//    int bitsPerChannel = -1;
-
 
21
//    switch (depth)
-
 
22
//    {
-
 
23
//        case CV_8U:
-
 
24
//        {
-
 
25
//            bitsPerChannel = 8;
-
 
26
//            break;
-
 
27
//        }
-
 
28
//        case CV_16U:
-
 
29
//        {
-
 
30
//            bitsPerChannel = 16;
-
 
31
//            break;
-
 
32
//        }
-
 
33
//        default:
-
 
34
//        {
-
 
35
//            return false;
-
 
36
//        }
-
 
37
//    }
-
 
38
 
-
 
39
//    const int bitsPerByte = 8;
-
 
40
//    size_t fileStep = (width * channels * bitsPerChannel) / bitsPerByte;
-
 
41
 
-
 
42
//    int rowsPerStrip = (int)((1 << 13)/fileStep);
-
 
43
//    readParam(params, TIFFTAG_ROWSPERSTRIP, rowsPerStrip);
-
 
44
 
-
 
45
//    if( rowsPerStrip < 1 )
-
 
46
//        rowsPerStrip = 1;
-
 
47
 
-
 
48
//    if( rowsPerStrip > height )
-
 
49
//        rowsPerStrip = height;
-
 
50
 
-
 
51
 
-
 
52
//    // do NOT put "wb" as the mode, because the b means "big endian" mode, not "binary" mode.
-
 
53
//    // http://www.remotesensing.org/libtiff/man/TIFFOpen.3tiff.html
-
 
54
//    TIFF* pTiffHandle = TIFFOpen(m_filename.c_str(), "w");
-
 
55
//    if (!pTiffHandle)
-
 
56
//    {
-
 
57
//        return false;
-
 
58
//    }
-
 
59
 
-
 
60
//    // defaults for now, maybe base them on params in the future
-
 
61
//    int   compression  = COMPRESSION_LZW;
-
 
62
//    int   predictor    = PREDICTOR_HORIZONTAL;
-
 
63
 
-
 
64
//    readParam(params, TIFFTAG_COMPRESSION, compression);
-
 
65
//    readParam(params, TIFFTAG_PREDICTOR, predictor);
-
 
66
 
-
 
67
//    int   colorspace = channels > 1 ? PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK;
-
 
68
 
-
 
69
//    if ( !TIFFSetField(pTiffHandle, TIFFTAG_IMAGEWIDTH, width)
-
 
70
//      || !TIFFSetField(pTiffHandle, TIFFTAG_IMAGELENGTH, height)
-
 
71
//      || !TIFFSetField(pTiffHandle, TIFFTAG_BITSPERSAMPLE, bitsPerChannel)
-
 
72
//      || !TIFFSetField(pTiffHandle, TIFFTAG_COMPRESSION, compression)
-
 
73
//      || !TIFFSetField(pTiffHandle, TIFFTAG_PHOTOMETRIC, colorspace)
-
 
74
//      || !TIFFSetField(pTiffHandle, TIFFTAG_SAMPLESPERPIXEL, channels)
-
 
75
//      || !TIFFSetField(pTiffHandle, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG)
-
 
76
//      || !TIFFSetField(pTiffHandle, TIFFTAG_ROWSPERSTRIP, rowsPerStrip)
-
 
77
//      || !TIFFSetField(pTiffHandle, TIFFTAG_PREDICTOR, predictor)
-
 
78
//       )
-
 
79
//    {
-
 
80
//        TIFFClose(pTiffHandle);
-
 
81
//        return false;
-
 
82
//    }
-
 
83
 
-
 
84
//    // row buffer, because TIFFWriteScanline modifies the original data!
-
 
85
//    size_t scanlineSize = TIFFScanlineSize(pTiffHandle);
-
 
86
//    AutoBuffer<uchar> _buffer(scanlineSize+32);
-
 
87
//    uchar* buffer = _buffer;
-
 
88
//    if (!buffer)
-
 
89
//    {
-
 
90
//        TIFFClose(pTiffHandle);
-
 
91
//        return false;
-
 
92
//    }
-
 
93
 
-
 
94
//    for (int y = 0; y < height; ++y)
-
 
95
//    {
-
 
96
//        switch(channels)
-
 
97
//        {
-
 
98
//            case 1:
-
 
99
//            {
-
 
100
//                memcpy(buffer, img.data + img.step * y, scanlineSize);
-
 
101
//                break;
-
 
102
//            }
-
 
103
 
-
 
104
//            case 3:
-
 
105
//            {
-
 
106
//                if (depth == CV_8U)
-
 
107
//                    icvCvt_BGR2RGB_8u_C3R( img.data + img.step*y, 0, buffer, 0, cvSize(width,1) );
-
 
108
//                else
-
 
109
//                    icvCvt_BGR2RGB_16u_C3R( (const ushort*)(img.data + img.step*y), 0, (ushort*)buffer, 0, cvSize(width,1) );
-
 
110
//                break;
-
 
111
//            }
-
 
112
 
-
 
113
//            case 4:
-
 
114
//            {
-
 
115
//                if (depth == CV_8U)
-
 
116
//                    icvCvt_BGRA2RGBA_8u_C4R( img.data + img.step*y, 0, buffer, 0, cvSize(width,1) );
-
 
117
//                else
-
 
118
//                    icvCvt_BGRA2RGBA_16u_C4R( (const ushort*)(img.data + img.step*y), 0, (ushort*)buffer, 0, cvSize(width,1) );
-
 
119
//                break;
-
 
120
//            }
-
 
121
 
-
 
122
//            default:
-
 
123
//            {
-
 
124
//                TIFFClose(pTiffHandle);
-
 
125
//                return false;
-
 
126
//            }
-
 
127
//        }
-
 
128
 
-
 
129
//        int writeResult = TIFFWriteScanline(pTiffHandle, buffer, y, 0);
-
 
130
//        if (writeResult != 1)
-
 
131
//        {
-
 
132
//            TIFFClose(pTiffHandle);
-
 
133
//            return false;
-
 
134
//        }
-
 
135
//    }
-
 
136
 
-
 
137
//    TIFFClose(pTiffHandle);
-
 
138
//    return true;
-
 
139
//}
-
 
140
 
-
 
141
 
-
 
142
// imwrite for png images, with little compression for good speed. Defaults to rgb, not bgr (as cv::imwrite).
-
 
143
bool imwritePNG(const std::string& filename, const cv::Mat img){
-
 
144
    png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
-
 
145
    png_infop info_ptr = 0;
-
 
146
    FILE* f = 0;
-
 
147
    int y, width = img.cols, height = img.rows;
-
 
148
    int depth = img.depth(), channels = img.channels();
-
 
149
    bool result = false;
-
 
150
    cv::AutoBuffer<uchar*> buffer;
-
 
151
 
-
 
152
    if( depth != CV_8U && depth != CV_16U )
-
 
153
        return false;
-
 
154
 
-
 
155
    if( png_ptr )
-
 
156
    {
-
 
157
        info_ptr = png_create_info_struct( png_ptr );
-
 
158
 
-
 
159
        if( info_ptr )
-
 
160
        {
-
 
161
            if( setjmp( png_jmpbuf ( png_ptr ) ) == 0 )
-
 
162
            {
-
 
163
 
-
 
164
                f = fopen( filename.c_str(), "wb" );
-
 
165
                if(f)
-
 
166
                    png_init_io( png_ptr, f );
-
 
167
 
-
 
168
                png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_FILTER_NONE);
-
 
169
                png_set_compression_level( png_ptr, Z_BEST_SPEED );
-
 
170
                png_set_compression_strategy(png_ptr, Z_FILTERED);
-
 
171
                //png_set_filter_heuristics(png_ptr, )
-
 
172
 
-
 
173
                bool isBilevel = (depth == CV_16UC3 || depth == CV_16UC1);
-
 
174
 
-
 
175
                png_set_IHDR( png_ptr, info_ptr, width, height, depth == CV_8U ? isBilevel?1:8 : 16,
-
 
176
                    channels == 1 ? PNG_COLOR_TYPE_GRAY :
-
 
177
                    channels == 3 ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA,
-
 
178
                    PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
-
 
179
                    PNG_FILTER_TYPE_DEFAULT );
-
 
180
 
-
 
181
                png_write_info( png_ptr, info_ptr );
-
 
182
 
-
 
183
                if (isBilevel)
-
 
184
                    png_set_packing(png_ptr);
-
 
185
 
-
 
186
                png_set_bgr( png_ptr );
-
 
187
                bool isBigEndian = (((const int*)"\0\x1\x2\x3\x4\x5\x6\x7")[0] & 255) != 0;
-
 
188
                if( !isBigEndian )
-
 
189
                    png_set_swap( png_ptr );
-
 
190
 
-
 
191
                buffer.allocate(height);
-
 
192
                for( y = 0; y < height; y++ )
-
 
193
                    buffer[y] = img.data + y*img.step;
-
 
194
 
-
 
195
                png_write_image( png_ptr, buffer );
-
 
196
                png_write_end( png_ptr, info_ptr );
-
 
197
 
-
 
198
                result = true;
-
 
199
 
-
 
200
            }
-
 
201
        }
-
 
202
    }
-
 
203
 
-
 
204
    png_destroy_write_struct( &png_ptr, &info_ptr );
-
 
205
    if(f) fclose( f );
-
 
206
 
-
 
207
    return result;
-
 
208
}
-
 
209
 
-
 
210
 
11
// Convert an BGR 3-channel image back into a Bayered image
211
// Convert an BGR 3-channel image back into a Bayered image
12
void cvtColorBGRToBayerBG(const cv::Mat &imBGR, cv::Mat &imBayerBG){
212
void cvtColorBGRToBayerBG(const cv::Mat &imBGR, cv::Mat &imBayerBG){
13
 
213
 
14
    imBayerBG.create(imBGR.size(), CV_8UC1);
214
    imBayerBG.create(imBGR.size(), CV_8UC1);
15
 
215
 
Line 101... Line 301...
101
//    }
301
//    }
102
 
302
 
103
    return;
303
    return;
104
}
304
}
105
 
305
 
106
// Lightly modified OpenCV function which accepts a line width argument
-
 
107
void drawChessboardCorners(cv::InputOutputArray _image, cv::Size patternSize, cv::InputArray _corners, bool patternWasFound, int line_width){
306
// Performs bitwise right-shift on every value of matrix I. This is done e.g. to remove the least significant bits in a 16 to 8-bit image conversion.
108
    cv::Mat corners = _corners.getMat();
-
 
109
    if( corners.empty() )
-
 
110
        return;
-
 
111
    cv::Mat image = _image.getMat(); CvMat c_image = _image.getMat();
-
 
112
    int nelems = corners.checkVector(2, CV_32F, true);
-
 
113
    CV_Assert(nelems >= 0);
-
 
114
    cvDrawChessboardCorners( &c_image, patternSize, (CvPoint2D32f*)corners.data,
-
 
115
                             nelems, patternWasFound, line_width);
-
 
116
}
-
 
117
 
-
 
118
void rshift(cv::Mat& I, unsigned int shift){
307
void rshift(cv::Mat& I, unsigned int shift){
119
 
308
 
120
    int nRows = I.rows;
309
    int nRows = I.rows;
121
    int nCols = I.cols;
310
    int nCols = I.cols;
122
 
311
 
Line 133... Line 322...
133
            p[j] = p[j]>>shift;
322
            p[j] = p[j]>>shift;
134
        }
323
        }
135
    }
324
    }
136
}
325
}
137
 
326
 
-
 
327
 
-
 
328
// Lightly modified OpenCV function which accepts a line width argument
-
 
329
void drawChessboardCorners(cv::InputOutputArray _image, cv::Size patternSize, cv::InputArray _corners, bool patternWasFound, int line_width){
-
 
330
    cv::Mat corners = _corners.getMat();
-
 
331
    if( corners.empty() )
-
 
332
        return;
-
 
333
    cv::Mat image = _image.getMat(); CvMat c_image = _image.getMat();
-
 
334
    int nelems = corners.checkVector(2, CV_32F, true);
-
 
335
    CV_Assert(nelems >= 0);
-
 
336
    cvtools::cvDrawChessboardCorners( &c_image, patternSize, (CvPoint2D32f*)corners.data,
-
 
337
                             nelems, patternWasFound, line_width);
-
 
338
}
-
 
339
 
-
 
340
 
-
 
341
 
138
void cvDrawChessboardCorners(CvArr* _image, CvSize pattern_size, CvPoint2D32f* corners, int count, int found, int line_width){
342
void cvDrawChessboardCorners(CvArr* _image, CvSize pattern_size, CvPoint2D32f* corners, int count, int found, int line_width){
139
    const int shift = 0;
343
    const int shift = 0;
140
    const int radius = 12;
344
    const int radius = 12;
141
    const int r = radius*(1 << shift);
345
    const int r = radius*(1 << shift);
142
    int i;
346
    int i;