1 /******************************************************************************
2 * Copyright (c) 2018 Texas Instruments Incorporated - http://www.ti.com/
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Texas Instruments Incorporated nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 *****************************************************************************/
29 #include <iostream>
30 #include "imgutil.h"
32 using namespace tinn;
34 bool tinn::imgutil::PreProcImage(
35 Mat& image, char *ptr, int16_t roi, int16_t n,
36 int16_t width, int16_t height, int16_t pitch,
37 int32_t chOffset, int32_t frameCount, int32_t preProcType)
38 {
39 int32_t i0, i1, i2, i3;
40 Mat spl[3];
41 int32_t enableMeanSub = 0;
42 Mat tempImage;
43 int32_t meanVlaues[3];
45 if(preProcType == 0) // Caffe-Jacinto Models
46 {
47 int32_t crop_width = width;
48 int32_t crop_height = height;
49 int32_t half_the_width = 256/ 2;
50 int32_t half_the_height = 256/ 2;
52 int32_t startX = half_the_width -crop_width/2;
53 int32_t startY = half_the_height -crop_height/2;
55 cv::Rect myROI(startX,
56 startY,
57 crop_width,
58 crop_height);
59 cv::resize(image, tempImage, Size(256,256), 0,0,cv::INTER_AREA);
60 image = tempImage(myROI);
61 enableMeanSub = 0;
62 }
63 else if (preProcType == 1) // Caffe Models , eg : SqueezeNet
64 {
66 int32_t crop_width = width;
67 int32_t crop_height = height;
68 int32_t half_the_width = 256/ 2;
69 int32_t half_the_height = 256/ 2;
71 int32_t startX = half_the_width -crop_width/2;
72 int32_t startY = half_the_height -crop_height/2;
74 cv::Rect myROI(startX,
75 startY,
76 crop_width,
77 crop_height);
78 cv::resize(image, tempImage, Size(256,256), 0,0,cv::INTER_AREA);
79 image = tempImage(myROI);
80 enableMeanSub = 1;
81 meanVlaues[0] = 104;
82 meanVlaues[1] = 117;
83 meanVlaues[2] = 123;
85 }
86 else if (preProcType == 2)
87 {
88 cv::cvtColor(image, image, CV_BGR2RGB);
89 float factor = 0.875;
90 int32_t orgWidth = image.size[1];
91 int32_t orgHeight = image.size[0];
93 int32_t crop_width = orgWidth*factor;
94 int32_t crop_height = orgHeight*factor;
95 int32_t half_the_width = orgWidth/ 2;
96 int32_t half_the_height = orgHeight / 2;
98 int32_t startX = half_the_width -crop_width/2;
99 int32_t startY = half_the_height -crop_height/2;
101 cv::Rect myROI(startX,
102 startY,
103 crop_width,
104 crop_height);
105 tempImage = image(myROI);
106 cv::resize(tempImage, image, Size(width,height), 0,0,CV_INTER_AREA);
107 enableMeanSub = 1;
108 meanVlaues[0] = 128;
109 meanVlaues[1] = 128;
110 meanVlaues[2] = 128;
111 }
112 else if (preProcType == 3)
113 {
114 cv::cvtColor(image, image, CV_BGR2RGB);
115 int32_t crop_width = width;
116 int32_t crop_height = height;
117 int32_t half_the_width = 32/ 2;
118 int32_t half_the_height = 32/ 2;
120 int32_t startX = half_the_width -crop_width/2;
121 int32_t startY = half_the_height -crop_height/2;
123 cv::Rect myROI(startX,
124 startY,
125 crop_width,
126 crop_height);
127 cv::resize(image, tempImage, Size(32,32), 0,0,cv::INTER_AREA);
128 image = tempImage(myROI);
129 enableMeanSub = 0;
130 }
131 else if (preProcType == 4)
132 {
133 //cv::cvtColor(image, image, CV_BGR2RGB);
134 int32_t crop_width = width;
135 int32_t crop_height = height;
136 int32_t half_the_width = width/ 2;
137 int32_t half_the_height = height/ 2;
139 int32_t startX = half_the_width -crop_width/2;
140 int32_t startY = half_the_height -crop_height/2;
142 cv::Rect myROI(startX,
143 startY,
144 crop_width,
145 crop_height);
146 cv::resize(image, tempImage, Size(width,height), 0,0,cv::INTER_AREA);
147 image = tempImage(myROI);
148 enableMeanSub = 0;
149 }
150 else
151 {
152 std::cerr << "Unsupported preProcType : " << preProcType << std::endl;
153 return false;
154 }
156 if(image.channels() > 3)
157 {
158 return false;
159 }
160 if(image.total() != (unsigned int) (height * width))
161 {
162 return false;
163 }
164 int size = (int)image.total()*image.channels();
165 uint8_t * data = (uint8_t*)malloc(size);
167 if(data == NULL)
168 {
169 return false;
170 }
172 split(image,spl);
173 for(i0 = 0; i0 < image.channels(); i0++)
174 {
175 std::memcpy(&data[i0*((int)image.total())],spl[i0].data,((int)image.total()) * sizeof(uint8_t));
176 }
177 for(i2 = 0; i2 < roi; i2++)
178 {
179 for(i0 = 0; i0 < n; i0++)
180 {
181 for(i1 = 0; i1 < height; i1++)
182 {
183 for(i3 = 0; i3 < width; i3++)
184 {
185 int32_t in;
187 if(enableMeanSub)
188 {
189 if(n != 1)
190 {
191 in = data[i2*n*width*height + i0*width*height+ i1*width + i3] - meanVlaues[i0];
192 }
193 else
194 {
195 in = data[i2*1*width*height + i1*width + i3] - meanVlaues[i0];
196 }
198 if(in > 127) in = 127;
199 if(in < -128) in = -128;
200 }
201 else
202 {
204 if(n != 1)
205 {
206 in = data[i2*n*width*height + i0*width*height+ i1*width + i3];
207 }
208 else
209 {
210 in = data[i2*1*width*height + i1*width + i3];
211 }
212 }
214 ptr[i2*n*chOffset + i0*chOffset + i1*pitch + i3] = in;
215 }
216 }
217 }
218 }
220 std::free(data);
221 return true;
222 }