11// created by harshvardhan
22#include " imageOverlayAndAugmentor.h"
33
4+ #if __has_include(<filesystem>)
5+ #include < filesystem>
6+ namespace fs = std::filesystem;
7+ #elif __has_include(<experimental/filesystem>)
8+ #include < experimental/filesystem>
9+ namespace fs = std::experimental::filesystem;
10+ #endif
11+
412
513void imageOverlayAndAugmentor::imageController (std::string& analysisCustom,std::string& analysis,const long analysisLineNumbers,const long analysisCustomLineNumbers)
614{
@@ -50,6 +58,7 @@ void imageOverlayAndAugmentor::imageController(std::string& analysisCustom,std::
5058 std::uniform_int_distribution<std::mt19937::result_type> n2 (0 ,analysisCustomLineNumbers);
5159 std::uniform_int_distribution<std::mt19937::result_type> imageAdjuster (0 ,10 );
5260 std::uniform_int_distribution<std::mt19937::result_type> randomer (0 , 1000 );
61+ std::uniform_int_distribution<std::mt19937::result_type> noise_stddev (0 , 30 );
5362 num = 0 ;
5463
5564
@@ -115,7 +124,8 @@ void imageOverlayAndAugmentor::imageController(std::string& analysisCustom,std::
115124 }
116125 else
117126 {
118- datasetImage1 = datasetImage;
127+ datasetImage1 = GaussianNoise (datasetImage, 0.0 , (int )noise_stddev (mt4));
128+ datasetImage1 = ColorJitter (datasetImage1, 20.0 );
119129 }
120130 std::string Name = std::to_string (num) + " customImages_" + std::to_string (i);
121131 std::string imgName = Name + " .jpeg" ;
@@ -124,10 +134,12 @@ void imageOverlayAndAugmentor::imageController(std::string& analysisCustom,std::
124134 image = imageOverlay (customImage, datasetImage1, (int )x (mt3), (int )y (mt4), Name);
125135 }
126136 else
127- {
137+ {
128138 image = imageRotater (customImage, datasetImage1, (int )x (mt3), (int )y (mt5), Name);
129139 }
140+
130141 imageSaver (imgName, image);
142+
131143 }
132144 // closing csvReader Custom
133145
@@ -152,6 +164,46 @@ cv::Mat imageOverlayAndAugmentor::imageBrightnessAndContrastControl(cv::Mat imag
152164}
153165
154166
167+ cv::Mat imageOverlayAndAugmentor::GaussianNoise (const cv::Mat src, double Mean, double StdDev)
168+ {
169+ cv::Mat src_16SC, dst;
170+ cv::Mat Gauss_noise = cv::Mat (src.size (), CV_16SC3);
171+ cv::randn (Gauss_noise, cv::Scalar::all (Mean), cv::Scalar::all (StdDev));
172+
173+ src.convertTo (src_16SC, CV_16SC3);
174+ cv::addWeighted (src_16SC, 1.0 , Gauss_noise, 1.0 , 0.0 , src_16SC);
175+ src_16SC.convertTo (dst, src.type ());
176+ return dst;
177+ }
178+
179+ cv::Mat imageOverlayAndAugmentor::ColorJitter (const cv::Mat src, double maxjit)
180+ {
181+ cv::Mat img, dst;
182+ cv::cvtColor (src, img, cv::COLOR_BGR2HSV);
183+
184+ std::random_device rd;
185+ std::mt19937 mt (rd ());
186+
187+ std::uniform_int_distribution<std::mt19937::result_type> jitter (-maxjit,maxjit);
188+
189+ for (int c = 0 ; c<img.channels (); c++)
190+ {
191+ double jit = (int )jitter (mt);
192+
193+ for (int i=0 ; i<img.rows ; i++)
194+ {
195+ for (int j=0 ; j<img.cols ; j++)
196+ {
197+ img.at <cv::Vec3b>(i,j)[c] = cv::saturate_cast<uchar>(img.at <cv::Vec3b>(i,j)[c]+ jit);
198+ }
199+ }
200+ }
201+
202+ cv::cvtColor (img, dst, cv::COLOR_HSV2BGR);
203+
204+ return dst;
205+ }
206+
155207cv::Mat imageOverlayAndAugmentor::imageOverlay (cv::Mat customImage, cv::Mat datasetImage,int x,int y,std::string imgName)
156208{
157209
@@ -192,6 +244,8 @@ cv::Mat imageOverlayAndAugmentor::imageOverlay(cv::Mat customImage, cv::Mat data
192244 return datasetImage;
193245}
194246
247+
248+
195249cv::Mat imageOverlayAndAugmentor::imageRotater (cv::Mat customImage,cv::Mat randomImage,int X,int Y,std::string imageName)
196250{
197251 bool negative = false ;
@@ -216,8 +270,8 @@ cv::Mat imageOverlayAndAugmentor::imageRotater(cv::Mat customImage,cv::Mat rando
216270 cv::warpAffine (customImage, dst, rot, bbox.size ());
217271 int height = dst.rows ;
218272 int width = dst.cols ;
219- cv::imwrite (" test.png" , dst);
220- cv::waitKey (2 );
273+ // cv::imwrite("test.png", dst);
274+ // cv::waitKey(2);
221275
222276 if (1920 > dst.cols + x)
223277 {
@@ -248,13 +302,13 @@ cv::Mat imageOverlayAndAugmentor::imageRotater(cv::Mat customImage,cv::Mat rando
248302
249303 }
250304
251- cv::Mat image = randomImage;
252- cv::Mat croppedImage;
253- croppedImage = image (cv::Rect (x, y, dst.cols , dst.rows ));
254- dst.copyTo (croppedImage);
305+ // cv::Mat image = randomImage;
306+ // cv::Mat croppedImage;
307+ // croppedImage = image(cv::Rect(x, y, dst.cols, dst.rows));
308+ // dst.copyTo(croppedImage);
255309 std::string bboxName = imageName + " .txt" ;
256310 boundingBox (x, y, width, height, bboxName);
257- return image ;
311+ return dst ;
258312}
259313
260314
@@ -266,13 +320,13 @@ void imageOverlayAndAugmentor::boundingBox(int x, int y, int h, int w, std::stri
266320 double H = (double )h / 1080 ;
267321 double W = (double ) w / 1920 ;
268322 std::string bboxName;
269- if (std::filesystem ::exists (" bbox" ))
323+ if (fs ::exists (" bbox" ))
270324 {
271325 bboxName = " bbox\\ " + name;
272326 }
273327 else
274328 {
275- std::filesystem ::create_directory (" bbox" );
329+ fs ::create_directory (" bbox" );
276330 bboxName = " bbox\\ " + name;
277331 }
278332 std::fstream fout;
@@ -284,13 +338,13 @@ void imageOverlayAndAugmentor::boundingBox(int x, int y, int h, int w, std::stri
284338void imageOverlayAndAugmentor::imageSaver (std::string imgName,cv::Mat image)
285339{
286340 std::string imageName;
287- if (std::filesystem ::exists (" image" ))
341+ if (fs ::exists (" image" ))
288342 {
289343 imageName = " image\\ " + imgName;
290344 }
291345 else
292346 {
293- std::filesystem ::create_directory (" image" );
347+ fs ::create_directory (" image" );
294348 imageName = " image\\ " + imgName;
295349 }
296350 cv::imwrite (imageName, image);
0 commit comments