cvfilters.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  1. #include <QImage>
  2. #include <QDebug>
  3. #include <math.h>
  4. #include <QTime>
  5. #define PI (3.141592653589793)
  6. float **generateGaussianKernel(float variance, int dim)
  7. {
  8. float **gaussianKernel;
  9. int i,j;
  10. gaussianKernel = new float*[dim];
  11. for (i = 0; i < dim; ++i)
  12. {
  13. gaussianKernel[i] = new float[dim];
  14. }
  15. float exponentialArg;
  16. int centerX=dim/2;
  17. int centerY=dim/2;
  18. for (i=-centerX;i<=centerX;i++)
  19. {
  20. for(j=-centerY;j<=centerY;j++)
  21. {
  22. exponentialArg=-(pow(i,2)+pow(j,2))/(2*pow(variance,2));
  23. gaussianKernel[i+centerX][j+centerY]= exp(exponentialArg)/((2*PI)*(pow(variance,2)));
  24. }
  25. }
  26. return gaussianKernel;
  27. }
  28. float **matrixConvolutionNbyN(QImage originalImage, float **kernel, int dim)
  29. {
  30. int i,j;
  31. int rows = originalImage.width()-(dim-1);
  32. int cols = originalImage.height()-(dim-1);
  33. int iKernel,jKernel;
  34. float **matrixImgA = new float*[rows];
  35. for (i = 0; i < rows; ++i)
  36. {
  37. matrixImgA[i] = new float[cols];
  38. }
  39. float computation=0;
  40. int centerX=dim/2;
  41. int centerY=dim/2;
  42. for(i=centerX;i<originalImage.width()-centerX;i++)
  43. {
  44. for(j=centerY;j<originalImage.height()-centerY;j++)
  45. {
  46. for(iKernel=0;iKernel<dim;iKernel++)
  47. {
  48. for(jKernel=0;jKernel<dim;jKernel++)
  49. {
  50. computation+=kernel[iKernel][jKernel]*originalImage.pixelIndex(i-centerX+iKernel,j-centerY+jKernel);
  51. }
  52. }
  53. matrixImgA[i-centerX][j-centerY]=computation;
  54. computation=0;
  55. }
  56. }
  57. for (int w = 0; w < dim; w++)
  58. delete[] kernel[w];
  59. delete[] kernel;
  60. return matrixImgA;
  61. }
  62. void matrixConvolutionNbyN(QImage originalImage, float **kernel, int dim, float **matrixImgA)
  63. {
  64. int i,j;
  65. int rows = originalImage.width()-(dim-1);
  66. int cols = originalImage.height()-(dim-1);
  67. int iKernel,jKernel;
  68. float computation=0;
  69. int centerX=dim/2;
  70. int centerY=dim/2;
  71. for(i=centerX;i<rows;i++)
  72. {
  73. for(j=centerY;j<cols;j++)
  74. {
  75. for(iKernel=0;iKernel<dim;iKernel++)
  76. {
  77. for(jKernel=0;jKernel<dim;jKernel++)
  78. {
  79. computation+=kernel[iKernel][jKernel]*originalImage.pixelIndex(i-centerX+iKernel,j-centerY+jKernel);
  80. }
  81. }
  82. matrixImgA[i-centerX][j-centerY]=computation;
  83. computation=0;
  84. }
  85. }
  86. for (int w = 0; w < dim; w++)
  87. delete[] kernel[w];
  88. delete[] kernel;
  89. }
  90. float **testImageConvolution2by2(QImage originalImage, float kernel[][2])
  91. {
  92. int i,j;
  93. int rows=originalImage.width()-1;
  94. int cols = originalImage.height()-1;
  95. float **matrixImg = new float*[rows];
  96. for (i = 0; i < rows; ++i)
  97. {
  98. matrixImg[i] = new float[cols];
  99. }
  100. float computation;
  101. for(i=0;i<rows;i++)
  102. {
  103. for(j=0;j<cols;j++)
  104. {
  105. computation=kernel[0][0]*originalImage.pixelIndex(i,j)+
  106. kernel[0][1]*originalImage.pixelIndex(i,j+1)+
  107. kernel[1][0]*originalImage.pixelIndex(i+1,j)+
  108. kernel[1][1]*originalImage.pixelIndex(i+1,j+1);
  109. matrixImg[i][j]=computation;
  110. }
  111. }
  112. return matrixImg;
  113. }
  114. QImage normalizeImage(QImage originalImage)
  115. {
  116. int i,j;
  117. int min,max;
  118. QImage normalizedImage=originalImage;
  119. min=originalImage.pixelIndex(0,0);
  120. max=originalImage.pixelIndex(0,0);
  121. for(i=0;i<originalImage.width();i++)
  122. {
  123. for(j=0;j<originalImage.height();j++)
  124. {
  125. if(originalImage.pixelIndex(i,j)<min)
  126. {
  127. min=originalImage.pixelIndex(i,j);
  128. }
  129. if(originalImage.pixelIndex(i,j)>max)
  130. {
  131. max=originalImage.pixelIndex(i,j);
  132. }
  133. }
  134. }
  135. float normalizedPixel;
  136. for(i=0;i<originalImage.width();i++)
  137. {
  138. for(j=0;j<originalImage.height();j++)
  139. {
  140. normalizedPixel=(originalImage.pixelIndex(i,j)-min)*(float)(255/(max-min));
  141. normalizedImage.setPixel(i,j,normalizedPixel);
  142. }
  143. }
  144. return normalizedImage;
  145. }
  146. void normalizeMatrix(float **matrixImage, int rows, int cols)
  147. {
  148. float normalizedMatrix[rows][cols];
  149. int i,j;
  150. float min,max;
  151. float normalizedPixel,normalizingFactor;
  152. min=matrixImage[0][0];
  153. max=matrixImage[0][0];
  154. for(i=0;i<rows;i++)
  155. {
  156. for(j=0;j<cols;j++)
  157. {
  158. if(matrixImage[i][j]<min)
  159. {
  160. min=matrixImage[i][j];
  161. }
  162. if(matrixImage[i][j]>max)
  163. {
  164. max=matrixImage[i][j];
  165. }
  166. }
  167. }
  168. for(i=0;i<rows;i++)
  169. {
  170. for(j=0;j<cols;j++)
  171. {
  172. normalizingFactor=(float)255/(max-min);
  173. normalizedPixel=round((matrixImage[i][j]-min)*normalizingFactor);
  174. normalizedMatrix[i][j]=normalizedPixel;
  175. }
  176. }
  177. for(i=0;i<rows;i++)
  178. {
  179. for(j=0;j<cols;j++)
  180. {
  181. matrixImage[i][j]=normalizedMatrix[i][j];
  182. }
  183. }
  184. }
  185. QImage matrixToImage(float **matrixImg, int rows, int cols)
  186. {
  187. QImage img(rows, cols, QImage::Format_Indexed8);
  188. int i,j;
  189. float roundedMatrixElement;
  190. QRgb value;
  191. for (int i = 0; i < 256; ++i) {
  192. value=qRgb(i,i,i);
  193. img.setColor(i, value);
  194. }
  195. for(i=0;i<rows;i++)
  196. {
  197. for(j=0;j<cols;j++)
  198. {
  199. roundedMatrixElement=round(matrixImg[i][j]);
  200. img.setPixel(i,j,(int)roundedMatrixElement);
  201. }
  202. }
  203. return img;
  204. }
  205. QImage imageConvolution3by3(QImage originalImage, float kernel[][3])
  206. {
  207. QImage filteredImage;
  208. int i,j;
  209. int rows=originalImage.width()-2;
  210. int cols = originalImage.height()-2;
  211. float **matrixImg = new float*[rows];
  212. for (i = 0; i < rows; ++i)
  213. {
  214. matrixImg[i] = new float[cols];
  215. }
  216. float computation;
  217. for(i=1;i<originalImage.width()-1;i++)
  218. {
  219. for(j=1;j<originalImage.height()-1;j++)
  220. {
  221. computation=kernel[0][0]*originalImage.pixelIndex(i-1,j-1)+
  222. kernel[0][1]*originalImage.pixelIndex(i-1,j)+
  223. kernel[0][2]*originalImage.pixelIndex(i-1,j+1)+
  224. kernel[1][0]*originalImage.pixelIndex(i,j-1)+
  225. kernel[1][1]*originalImage.pixelIndex(i,j)+
  226. kernel[1][2]*originalImage.pixelIndex(i,j+1)+
  227. kernel[2][0]*originalImage.pixelIndex(i+1,j-1)+
  228. kernel[2][1]*originalImage.pixelIndex(i+1,j)+
  229. kernel[2][2]*originalImage.pixelIndex(i+1,j+1);
  230. matrixImg[i-1][j-1]=round(computation);
  231. }
  232. }
  233. normalizeMatrix(matrixImg, rows, cols);
  234. filteredImage=matrixToImage(matrixImg,rows,cols);
  235. return filteredImage;
  236. }
  237. float **imageConvolution5by5(QImage originalImage, float kernel[][5])
  238. {
  239. int i,j;
  240. int rows=originalImage.width()-4;
  241. int cols = originalImage.height()-4;
  242. float **matrixImg = new float*[rows];
  243. for (i = 0; i < rows; ++i)
  244. {
  245. matrixImg[i] = new float[cols];
  246. }
  247. float computation;
  248. for(i=2;i<originalImage.width()-2;i++)
  249. {
  250. for(j=2;j<originalImage.height()-2;j++)
  251. {
  252. computation=kernel[0][0]*originalImage.pixelIndex(i-2,j-2)+
  253. kernel[0][1]*originalImage.pixelIndex(i-2,j-1)+
  254. kernel[0][2]*originalImage.pixelIndex(i-2,j)+
  255. kernel[0][3]*originalImage.pixelIndex(i-2,j+1)+
  256. kernel[0][4]*originalImage.pixelIndex(i-2,j+2)+
  257. kernel[1][0]*originalImage.pixelIndex(i-1,j-2)+
  258. kernel[1][1]*originalImage.pixelIndex(i-1,j-1)+
  259. kernel[1][2]*originalImage.pixelIndex(i-1,j)+
  260. kernel[1][3]*originalImage.pixelIndex(i-1,j+1)+
  261. kernel[1][4]*originalImage.pixelIndex(i-1,j+2)+
  262. kernel[2][0]*originalImage.pixelIndex(i,j-2)+
  263. kernel[2][1]*originalImage.pixelIndex(i,j-1)+
  264. kernel[2][2]*originalImage.pixelIndex(i,j)+
  265. kernel[2][3]*originalImage.pixelIndex(i,j+1)+
  266. kernel[2][4]*originalImage.pixelIndex(i,j+2)+
  267. kernel[3][0]*originalImage.pixelIndex(i+1,j-2)+
  268. kernel[3][1]*originalImage.pixelIndex(i+1,j-1)+
  269. kernel[3][2]*originalImage.pixelIndex(i+1,j)+
  270. kernel[3][3]*originalImage.pixelIndex(i+1,j+1)+
  271. kernel[3][4]*originalImage.pixelIndex(i+1,j+2)+
  272. kernel[4][0]*originalImage.pixelIndex(i+2,j-2)+
  273. kernel[4][1]*originalImage.pixelIndex(i+2,j-1)+
  274. kernel[4][2]*originalImage.pixelIndex(i+2,j)+
  275. kernel[4][3]*originalImage.pixelIndex(i+2,j+1)+
  276. kernel[4][4]*originalImage.pixelIndex(i+2,j+2);
  277. matrixImg[i-2][j-2]=computation;
  278. }
  279. }
  280. return matrixImg;
  281. }
  282. float **matrixImageConvolution3by3(QImage originalImage, float kernel[][3])
  283. {
  284. int i,j;
  285. int rows=originalImage.width()-2;
  286. int cols = originalImage.height()-2;
  287. float **matrixImg = new float*[rows];
  288. for (i = 0; i < rows; ++i)
  289. {
  290. matrixImg[i] = new float[cols];
  291. }
  292. float computation;
  293. for(i=1;i<originalImage.width()-1;i++)
  294. {
  295. for(j=1;j<originalImage.height()-1;j++)
  296. {
  297. computation=kernel[0][0]*originalImage.pixelIndex(i-1,j-1)+
  298. kernel[0][1]*originalImage.pixelIndex(i-1,j)+
  299. kernel[0][2]*originalImage.pixelIndex(i-1,j+1)+
  300. kernel[1][0]*originalImage.pixelIndex(i,j-1)+
  301. kernel[1][1]*originalImage.pixelIndex(i,j)+
  302. kernel[1][2]*originalImage.pixelIndex(i,j+1)+
  303. kernel[2][0]*originalImage.pixelIndex(i+1,j-1)+
  304. kernel[2][1]*originalImage.pixelIndex(i+1,j)+
  305. kernel[2][2]*originalImage.pixelIndex(i+1,j+1);
  306. matrixImg[i-1][j-1]=computation;
  307. }
  308. }
  309. return matrixImg;
  310. }
  311. QImage boxFilter(QImage originalImage)
  312. {
  313. QImage filteredImage;
  314. int rows=originalImage.width()-2;
  315. int cols=originalImage.height()-2;
  316. float **resultMatrix;
  317. float kernelBoxFilter[3][3]={1.0/9.0,1.0/9.0,1.0/9.0,1.0/9.0,1.0/9.0,1.0/9.0,1.0/9.0,1.0/9.0,1.0/9.0};
  318. resultMatrix=matrixImageConvolution3by3(originalImage,kernelBoxFilter);
  319. filteredImage=matrixToImage(resultMatrix,rows,cols);
  320. return filteredImage;
  321. }
  322. QImage medianFilter(QImage originalImage)
  323. {
  324. QImage filteredImage;
  325. int rows=originalImage.width()-2;
  326. int cols=originalImage.height()-2;
  327. QVector<int> elemVector;
  328. int medianValue=4;
  329. int i,j;
  330. float **matrixImg = new float*[rows];
  331. for (i = 0; i < rows; ++i)
  332. {
  333. matrixImg[i] = new float[cols];
  334. }
  335. for(i=1;i<originalImage.width()-1;i++)
  336. {
  337. for(j=1;j<originalImage.height()-1;j++)
  338. {
  339. elemVector.append(originalImage.pixelIndex(i-1,j-1));
  340. elemVector.append(originalImage.pixelIndex(i-1,j));
  341. elemVector.append(originalImage.pixelIndex(i-1,j+1));
  342. elemVector.append(originalImage.pixelIndex(i,j-1));
  343. elemVector.append(originalImage.pixelIndex(i,j));
  344. elemVector.append(originalImage.pixelIndex(i,j+1));
  345. elemVector.append(originalImage.pixelIndex(i+1,j-1));
  346. elemVector.append(originalImage.pixelIndex(i+1,j));
  347. elemVector.append(originalImage.pixelIndex(i+1,j+1));
  348. qSort(elemVector);
  349. matrixImg[i-1][j-1]=elemVector.value(medianValue);
  350. elemVector.clear();
  351. }
  352. }
  353. //normalizeMatrix(resultMatrix, rows, cols);
  354. filteredImage=matrixToImage(matrixImg,rows,cols);
  355. return filteredImage;
  356. }
  357. QImage rankFilter(QImage originalImage, int dim)
  358. {
  359. QImage filteredImage;
  360. int rows=originalImage.width()-2;
  361. int cols=originalImage.height()-2;
  362. QVector<int> elemVector;
  363. int medianValue=4;
  364. int i,j;
  365. float **matrixImg = new float*[rows];
  366. for (i = 0; i < rows; ++i)
  367. {
  368. matrixImg[i] = new float[cols];
  369. }
  370. for(i=1;i<originalImage.width()-1;i++)
  371. {
  372. for(j=1;j<originalImage.height()-1;j++)
  373. {
  374. elemVector.append(originalImage.pixelIndex(i-1,j-1));
  375. elemVector.append(originalImage.pixelIndex(i-1,j));
  376. elemVector.append(originalImage.pixelIndex(i-1,j+1));
  377. elemVector.append(originalImage.pixelIndex(i,j-1));
  378. elemVector.append(originalImage.pixelIndex(i,j));
  379. elemVector.append(originalImage.pixelIndex(i,j+1));
  380. elemVector.append(originalImage.pixelIndex(i+1,j-1));
  381. elemVector.append(originalImage.pixelIndex(i+1,j));
  382. elemVector.append(originalImage.pixelIndex(i+1,j+1));
  383. qSort(elemVector);
  384. if(dim==3)
  385. {
  386. matrixImg[i-1][j-1]=(elemVector.value(medianValue)+elemVector.value(medianValue+1)+elemVector.value(medianValue-1))/3.0;
  387. elemVector.clear();
  388. }
  389. else if(dim==5)
  390. {
  391. matrixImg[i-1][j-1]=(elemVector.value(medianValue)+elemVector.value(medianValue+1)+elemVector.value(medianValue-1)+elemVector.value(medianValue+2)+elemVector.value(medianValue-2))/5.0;
  392. elemVector.clear();
  393. }
  394. else
  395. {
  396. matrixImg[i-1][j-1]=(elemVector.value(medianValue)+elemVector.value(medianValue+1)+elemVector.value(medianValue-1)+elemVector.value(medianValue+2)+elemVector.value(medianValue-2)+elemVector.value(medianValue+3)+elemVector.value(medianValue-3))/7.0;
  397. elemVector.clear();
  398. }
  399. }
  400. }
  401. filteredImage=matrixToImage(matrixImg,rows,cols);
  402. return filteredImage;
  403. }
  404. float getVarianceFromArray(QVector<int> array)
  405. {
  406. float variance;
  407. int i;
  408. int numElem=array.size();
  409. float mean;
  410. float sum=0;
  411. for(i=0;i<numElem;i++)
  412. {
  413. sum+=array.value(i);
  414. }
  415. mean=(float)sum/numElem;
  416. sum=0;
  417. for(i=0;i<numElem;i++)
  418. {
  419. sum+=pow(array.value(i)-mean,2);
  420. }
  421. variance=(float)sum/numElem;
  422. return variance;
  423. }
  424. float getMeanFromArray(QVector<int> array)
  425. {
  426. int i;
  427. int numElem=array.size();
  428. float mean;
  429. float sum=0;
  430. for(i=0;i<numElem;i++)
  431. {
  432. sum+=array.value(i);
  433. }
  434. mean=(float)sum/numElem;
  435. return mean;
  436. }
  437. QImage nagaoFilter(QImage originalImage)
  438. {
  439. QImage filteredImage;
  440. float** matrixImg;
  441. int i,j;
  442. float currentMean;
  443. QVector<int> nagaoArray;
  444. int rows = originalImage.width()-4;
  445. int cols = originalImage.height()-4;
  446. matrixImg = new float*[rows];
  447. for (i = 0; i < rows; ++i)
  448. {
  449. matrixImg[i] = new float[cols];
  450. }
  451. float currentVariance,minVariance;
  452. for(i=2;i<originalImage.width()-2;i++)
  453. {
  454. for(j=2;j<originalImage.height()-2;j++)
  455. {
  456. //FIRST NAGAO SUBBLOCK
  457. nagaoArray.append(originalImage.pixelIndex(i-1,j-1));
  458. nagaoArray.append(originalImage.pixelIndex(i-1,j));
  459. nagaoArray.append(originalImage.pixelIndex(i-1,j+1));
  460. nagaoArray.append(originalImage.pixelIndex(i,j-1));
  461. nagaoArray.append(originalImage.pixelIndex(i,j));
  462. nagaoArray.append(originalImage.pixelIndex(i,j+1));
  463. nagaoArray.append(originalImage.pixelIndex(i+1,j-1));
  464. nagaoArray.append(originalImage.pixelIndex(i+1,j));
  465. nagaoArray.append(originalImage.pixelIndex(i+1,j+1));
  466. currentVariance=getVarianceFromArray(nagaoArray);
  467. currentMean=getMeanFromArray(nagaoArray);
  468. minVariance=currentVariance;
  469. nagaoArray.clear();
  470. //SECOND NAGAO SUBBLOCK
  471. nagaoArray.append(originalImage.pixelIndex(i-2,j-1));
  472. nagaoArray.append(originalImage.pixelIndex(i-2,j));
  473. nagaoArray.append(originalImage.pixelIndex(i-2,j+1));
  474. nagaoArray.append(originalImage.pixelIndex(i-1,j));
  475. nagaoArray.append(originalImage.pixelIndex(i-1,j-1));
  476. nagaoArray.append(originalImage.pixelIndex(i-1,j+1));
  477. nagaoArray.append(originalImage.pixelIndex(i,j));
  478. currentVariance=getVarianceFromArray(nagaoArray);
  479. if(currentVariance<minVariance)
  480. {
  481. minVariance=currentVariance;
  482. currentMean=getMeanFromArray(nagaoArray);
  483. }
  484. nagaoArray.clear();
  485. //THIRD NAGAO SUBBLOCK
  486. nagaoArray.append(originalImage.pixelIndex(i-1,j-2));
  487. nagaoArray.append(originalImage.pixelIndex(i-1,j-1));
  488. nagaoArray.append(originalImage.pixelIndex(i,j-2));
  489. nagaoArray.append(originalImage.pixelIndex(i,j-1));
  490. nagaoArray.append(originalImage.pixelIndex(i,j));
  491. nagaoArray.append(originalImage.pixelIndex(i+1,j-2));
  492. nagaoArray.append(originalImage.pixelIndex(i+1,j-1));
  493. currentVariance=getVarianceFromArray(nagaoArray);
  494. if(currentVariance<minVariance)
  495. {
  496. minVariance=currentVariance;
  497. currentMean=getMeanFromArray(nagaoArray);
  498. }
  499. nagaoArray.clear();
  500. //FOURTH NAGAO SUBBLOCK
  501. nagaoArray.append(originalImage.pixelIndex(i-1,j+1));
  502. nagaoArray.append(originalImage.pixelIndex(i-1,j+2));
  503. nagaoArray.append(originalImage.pixelIndex(i,j));
  504. nagaoArray.append(originalImage.pixelIndex(i,j+1));
  505. nagaoArray.append(originalImage.pixelIndex(i,j+2));
  506. nagaoArray.append(originalImage.pixelIndex(i+1,j+1));
  507. nagaoArray.append(originalImage.pixelIndex(i+1,j+2));
  508. currentVariance=getVarianceFromArray(nagaoArray);
  509. if(currentVariance<minVariance)
  510. {
  511. minVariance=currentVariance;
  512. currentMean=getMeanFromArray(nagaoArray);
  513. }
  514. nagaoArray.clear();
  515. //FIFTH NAGAO SUBBLOCK
  516. nagaoArray.append(originalImage.pixelIndex(i,j));
  517. nagaoArray.append(originalImage.pixelIndex(i+1,j-1));
  518. nagaoArray.append(originalImage.pixelIndex(i+1,j));
  519. nagaoArray.append(originalImage.pixelIndex(i+1,j+1));
  520. nagaoArray.append(originalImage.pixelIndex(i+2,j-1));
  521. nagaoArray.append(originalImage.pixelIndex(i+2,j));
  522. nagaoArray.append(originalImage.pixelIndex(i+2,j+1));
  523. currentVariance=getVarianceFromArray(nagaoArray);
  524. if(currentVariance<minVariance)
  525. {
  526. minVariance=currentVariance;
  527. currentMean=getMeanFromArray(nagaoArray);
  528. }
  529. nagaoArray.clear();
  530. //SIXTH NAGAO SUBBLOCK
  531. nagaoArray.append(originalImage.pixelIndex(i-2,j-2));
  532. nagaoArray.append(originalImage.pixelIndex(i-2,j-1));
  533. nagaoArray.append(originalImage.pixelIndex(i-1,j-2));
  534. nagaoArray.append(originalImage.pixelIndex(i-1,j-1));
  535. nagaoArray.append(originalImage.pixelIndex(i-1,j));
  536. nagaoArray.append(originalImage.pixelIndex(i,j-1));
  537. nagaoArray.append(originalImage.pixelIndex(i,j));
  538. currentVariance=getVarianceFromArray(nagaoArray);
  539. if(currentVariance<minVariance)
  540. {
  541. minVariance=currentVariance;
  542. currentMean=getMeanFromArray(nagaoArray);
  543. }
  544. nagaoArray.clear();
  545. //SEVENTH NAGAO SUBBLOCK
  546. nagaoArray.append(originalImage.pixelIndex(i-2,j+1));
  547. nagaoArray.append(originalImage.pixelIndex(i-2,j+2));
  548. nagaoArray.append(originalImage.pixelIndex(i-1,j));
  549. nagaoArray.append(originalImage.pixelIndex(i-1,j+1));
  550. nagaoArray.append(originalImage.pixelIndex(i-1,j+2));
  551. nagaoArray.append(originalImage.pixelIndex(i,j));
  552. nagaoArray.append(originalImage.pixelIndex(i,j+1));
  553. currentVariance=getVarianceFromArray(nagaoArray);
  554. if(currentVariance<minVariance)
  555. {
  556. minVariance=currentVariance;
  557. currentMean=getMeanFromArray(nagaoArray);
  558. }
  559. nagaoArray.clear();
  560. //EIGHT NAGAO SUBBLOCK
  561. nagaoArray.append(originalImage.pixelIndex(i,j-1));
  562. nagaoArray.append(originalImage.pixelIndex(i,j));
  563. nagaoArray.append(originalImage.pixelIndex(i+1,j-2));
  564. nagaoArray.append(originalImage.pixelIndex(i+1,j-1));
  565. nagaoArray.append(originalImage.pixelIndex(i+1,j));
  566. nagaoArray.append(originalImage.pixelIndex(i+2,j-2));
  567. nagaoArray.append(originalImage.pixelIndex(i+2,j-1));
  568. currentVariance=getVarianceFromArray(nagaoArray);
  569. if(currentVariance<minVariance)
  570. {
  571. minVariance=currentVariance;
  572. currentMean=getMeanFromArray(nagaoArray);
  573. }
  574. nagaoArray.clear();
  575. //NINTH NAGAO SUBBLOCK
  576. nagaoArray.append(originalImage.pixelIndex(i,j));
  577. nagaoArray.append(originalImage.pixelIndex(i,j+1));
  578. nagaoArray.append(originalImage.pixelIndex(i+1,j));
  579. nagaoArray.append(originalImage.pixelIndex(i+1,j+1));
  580. nagaoArray.append(originalImage.pixelIndex(i+1,j+2));
  581. nagaoArray.append(originalImage.pixelIndex(i+2,j+1));
  582. nagaoArray.append(originalImage.pixelIndex(i+2,j+2));
  583. currentVariance=getVarianceFromArray(nagaoArray);
  584. if(currentVariance<minVariance)
  585. {
  586. minVariance=currentVariance;
  587. currentMean=getMeanFromArray(nagaoArray);
  588. }
  589. nagaoArray.clear();
  590. matrixImg[i-2][j-2]=currentMean;
  591. }
  592. }
  593. normalizeMatrix(matrixImg, rows, cols);
  594. filteredImage=matrixToImage(matrixImg,rows,cols);
  595. return filteredImage;
  596. }
  597. QImage sharpeningFilter(QImage originalImage)
  598. {
  599. QImage filteredImage;
  600. float kernelBoxFilter[3][3]={1.0/9.0,1.0/9.0,1.0/9.0,1.0/9.0,1.0/9.0,1.0/9.0,1.0/9.0,1.0/9.0,1.0/9.0};
  601. float kernelBoxFIlter2Centered[3][3]={0,0,0,0,2.0,0,0,0,0};
  602. float kernelSharpeningFilter[3][3];
  603. int i=0,j=0;
  604. int rows=originalImage.width()-2;
  605. int cols=originalImage.height()-2;
  606. float **resultMatrix;
  607. for(i=0;i<3;i++)
  608. {
  609. for(j=0;j<3;j++)
  610. {
  611. kernelSharpeningFilter[i][j]=kernelBoxFIlter2Centered[i][j]-kernelBoxFilter[i][j];
  612. }
  613. }
  614. resultMatrix=matrixImageConvolution3by3(originalImage,kernelSharpeningFilter);
  615. normalizeMatrix(resultMatrix, rows, cols);
  616. filteredImage=matrixToImage(resultMatrix,rows,cols);
  617. return filteredImage;
  618. }
  619. float **sobelVerticalFilter(QImage originalImage)
  620. {
  621. QImage filteredImage;
  622. float kernelSobelVerticalFilter[3][3]={-1,-2,-1,0,0,0,1,2,1};
  623. float **resultMatrix;
  624. resultMatrix=matrixImageConvolution3by3(originalImage,kernelSobelVerticalFilter);
  625. return resultMatrix;
  626. //return filteredImage;
  627. }
  628. float **sobelHorizontalFilter(QImage originalImage)
  629. {
  630. float kernelSobelHorizontalFilter[3][3]={-1,0,1,-2,0,2,-1,0,1};
  631. float **resultMatrix;
  632. resultMatrix=matrixImageConvolution3by3(originalImage,kernelSobelHorizontalFilter);
  633. return resultMatrix;
  634. }
  635. QImage laplacianFilter(QImage originalImage)
  636. {
  637. QImage filteredImage;
  638. int rows=originalImage.width()-4;
  639. int cols=originalImage.height()-4;
  640. float **resultMatrix;
  641. float kernelLaplacianFilter[5][5]={0,0,-1,0,0,0,-1,-2,-1,0,-1,-2,16,-2,-1,0,-1,-2,-1,0,0,0,-1,0,0};
  642. resultMatrix=imageConvolution5by5(originalImage,kernelLaplacianFilter);
  643. normalizeMatrix(resultMatrix, rows, cols);
  644. filteredImage=matrixToImage(resultMatrix,rows,cols);
  645. return filteredImage;
  646. }
  647. QImage gaussianFilter(QImage originalImage)
  648. {
  649. QImage filteredImage;
  650. int rows=originalImage.width()-4;
  651. int cols=originalImage.height()-4;
  652. float **resultMatrix;
  653. float kernelGaussianFilter[5][5]={0.03,0.013,0.022,0.013,0.003,0.0013,0.059,0.097,0.059,0.013,0.022,0.097,0.159,0.097,0.022,0.013,0.059,0.097,0.059,0.013,0.003,0.013,0.022,0.013,0.003};
  654. resultMatrix=imageConvolution5by5(originalImage,kernelGaussianFilter);
  655. filteredImage=matrixToImage(resultMatrix,rows,cols);
  656. return filteredImage;
  657. }
  658. QImage sobelModuleFilter(QImage originalImage)
  659. {
  660. QImage filteredImage;
  661. float** matrixImgVertical;
  662. float** matrixImgHorizontal;
  663. float** matrixImg;
  664. int i,j;
  665. int rows = originalImage.width()-2;
  666. int cols = originalImage.height()-2;
  667. matrixImg = new float*[rows];
  668. for (i = 0; i < rows; ++i)
  669. {
  670. matrixImg[i] = new float[cols];
  671. }
  672. matrixImgVertical=sobelVerticalFilter(originalImage);
  673. matrixImgHorizontal=sobelHorizontalFilter(originalImage);
  674. float module;
  675. for(i=0;i<rows;i++)
  676. {
  677. for(j=0;j<cols;j++)
  678. {
  679. module=sqrt(pow(matrixImgVertical[i][j],2)+pow(matrixImgHorizontal[i][j],2));
  680. matrixImg[i][j]=module;
  681. }
  682. }
  683. normalizeMatrix(matrixImg, rows, cols);
  684. filteredImage=matrixToImage(matrixImg,rows,cols);
  685. return filteredImage;
  686. }
  687. QImage sobelPhaseFilter(QImage originalImage)
  688. {
  689. float **matrixImgVertical;
  690. float **matrixImgHorizontal;
  691. float** matrixImg;
  692. int i,j;
  693. int rows = originalImage.width()-2;
  694. int cols = originalImage.height()-2;
  695. QImage filteredImage;
  696. float phase;
  697. matrixImg = new float*[rows];
  698. for (i = 0; i < rows; ++i)
  699. {
  700. matrixImg[i] = new float[cols];
  701. }
  702. matrixImgVertical=sobelVerticalFilter(originalImage);
  703. matrixImgHorizontal=sobelHorizontalFilter(originalImage);
  704. for(i=0;i<rows;i++)
  705. {
  706. for(j=0;j<cols;j++)
  707. {
  708. phase=atan2(matrixImgVertical[i][j],matrixImgHorizontal[i][j]);
  709. //phaseDegree=(phase*180)/3.14159265359;
  710. matrixImg[i][j]=phase;
  711. }
  712. }
  713. normalizeMatrix(matrixImg, rows, cols);
  714. filteredImage=matrixToImage(matrixImg,rows,cols);
  715. return filteredImage;
  716. }
  717. QImage robertsModuleFilter(QImage originalImage)
  718. {
  719. QImage filteredImage;
  720. float **filteredImageMainDiag;
  721. float **filteredImageSecDiag;
  722. float **matrixImg;
  723. int rows=originalImage.width()-1;
  724. int cols=originalImage.height()-1;
  725. float kernelRobertsMainDiagFilter[2][2]={1,0,0,-1};
  726. float kernelRobertsSecDiagFilter[2][2]={0,1,-1,0};
  727. int i,j;
  728. matrixImg = new float*[rows];
  729. for (i = 0; i < rows; ++i)
  730. {
  731. matrixImg[i] = new float[cols];
  732. }
  733. float module;
  734. filteredImageMainDiag=testImageConvolution2by2(originalImage,kernelRobertsMainDiagFilter);
  735. filteredImageSecDiag=testImageConvolution2by2(originalImage,kernelRobertsSecDiagFilter);
  736. for(i=0;i<rows;i++)
  737. {
  738. for(j=0;j<cols;j++)
  739. {
  740. module=sqrt(pow(filteredImageMainDiag[i][j],2)+pow(filteredImageSecDiag[i][j],2));
  741. matrixImg[i][j]=module;
  742. }
  743. }
  744. normalizeMatrix(matrixImg, rows, cols);
  745. filteredImage=matrixToImage(matrixImg,rows,cols);
  746. return filteredImage;
  747. }
  748. QImage robertsPhaseFilter(QImage originalImage)
  749. {
  750. float **filteredImageMainDiag;
  751. float **filteredImageSecDiag;
  752. float **matrixImg;
  753. int i,j;
  754. int rows = originalImage.width()-1;
  755. int cols = originalImage.height()-1;
  756. float kernelRobertsMainDiagFilter[2][2]={1,0,0,-1};
  757. float kernelRobertsSecDiagFilter[2][2]={0,1,-1,0};
  758. QImage filteredImage;
  759. float phase;
  760. filteredImageMainDiag=testImageConvolution2by2(originalImage,kernelRobertsMainDiagFilter);
  761. filteredImageSecDiag=testImageConvolution2by2(originalImage,kernelRobertsSecDiagFilter);
  762. matrixImg = new float*[rows];
  763. for (i = 0; i < rows; ++i)
  764. {
  765. matrixImg[i] = new float[cols];
  766. }
  767. for(i=0;i<rows;i++)
  768. {
  769. for(j=0;j<cols;j++)
  770. {
  771. phase=atan2(filteredImageSecDiag[i][j],filteredImageMainDiag[i][j])+(PI/4);
  772. matrixImg[i][j]=phase;
  773. }
  774. }
  775. normalizeMatrix(matrixImg, rows, cols);
  776. filteredImage=matrixToImage(matrixImg,rows,cols);
  777. return filteredImage;
  778. }
  779. float **isotropicVerticalFilter(QImage originalImage)
  780. {
  781. float **resultMatrix;
  782. float kernelIsotropicVerticalFilter[3][3]={-1,0,1,-sqrt(2),0,sqrt(2),-1,0,1};
  783. resultMatrix=matrixImageConvolution3by3(originalImage,kernelIsotropicVerticalFilter);
  784. return resultMatrix;
  785. }
  786. float **isotropicHorizontalFilter(QImage originalImage)
  787. {
  788. float **resultMatrix;
  789. float kernelIsotropicHorizontalFilter[3][3]={1,sqrt(2),1,0,0,0,-1,-sqrt(2),-1};
  790. resultMatrix=matrixImageConvolution3by3(originalImage,kernelIsotropicHorizontalFilter);
  791. return resultMatrix;
  792. }
  793. QImage isotropicModuleFilter(QImage originalImage)
  794. {
  795. float **filteredImageVertical=isotropicVerticalFilter(originalImage);
  796. float **filteredImageHorizontal=isotropicHorizontalFilter(originalImage);
  797. int rows=originalImage.width()-2;
  798. int cols=originalImage.height()-2;
  799. QImage filteredImage;
  800. float module=0;
  801. int i,j;
  802. float **matrixImg;
  803. matrixImg = new float*[rows];
  804. for (i = 0; i < rows; ++i)
  805. {
  806. matrixImg[i] = new float[cols];
  807. }
  808. for(i=0;i<rows;i++)
  809. {
  810. for(j=0;j<cols;j++)
  811. {
  812. module=sqrt(pow(filteredImageVertical[i][j],2)+pow(filteredImageHorizontal[i][j],2));
  813. matrixImg[i][j]=module;
  814. }
  815. }
  816. normalizeMatrix(matrixImg, rows, cols);
  817. filteredImage=matrixToImage(matrixImg,rows,cols);
  818. return filteredImage;
  819. }
  820. QImage isotropicPhaseFilter(QImage originalImage)
  821. {
  822. float **filteredImageVertical=isotropicVerticalFilter(originalImage);
  823. float **filteredImageHorizontal=isotropicHorizontalFilter(originalImage);
  824. float **matrixImg;
  825. QImage filteredImage;
  826. int rows=originalImage.width()-2;
  827. int cols=originalImage.height()-2;
  828. float phase=0;
  829. int i,j;
  830. matrixImg = new float*[rows];
  831. for (i = 0; i < rows; ++i)
  832. {
  833. matrixImg[i] = new float[cols];
  834. }
  835. for(i=0;i<rows;i++)
  836. {
  837. for(j=0;j<cols;j++)
  838. {
  839. phase=atan2(filteredImageVertical[i][j],filteredImageHorizontal[i][j]);
  840. matrixImg[i][j]=phase;
  841. }
  842. }
  843. normalizeMatrix(matrixImg, rows, cols);
  844. filteredImage=matrixToImage(matrixImg,rows,cols);
  845. return filteredImage;
  846. }
  847. float **prewittVerticalFilter(QImage originalImage)
  848. {
  849. float **resultImg;
  850. float kernelPrewittVerticalFilter[3][3]={-1,-1,-1,0,0,0,1,1,1};
  851. resultImg=matrixImageConvolution3by3(originalImage,kernelPrewittVerticalFilter);
  852. return resultImg;
  853. }
  854. float **prewittHorizontalFilter(QImage originalImage)
  855. {
  856. float **resultImg;
  857. float kernelPrewittHorizontalFilter[3][3]={-1,0,1,-1,0,1,-1,0,1};
  858. resultImg=matrixImageConvolution3by3(originalImage,kernelPrewittHorizontalFilter);
  859. return resultImg;
  860. }
  861. QImage prewittModuleFilter(QImage originalImage)
  862. {
  863. float **filteredImageVertical=prewittVerticalFilter(originalImage);
  864. float **filteredImageHorizontal=prewittHorizontalFilter(originalImage);
  865. QImage filteredImage;
  866. float module=0;
  867. int i,j;
  868. float **matrixImg;
  869. int rows=originalImage.width()-2;
  870. int cols=originalImage.height()-2;
  871. matrixImg = new float*[rows];
  872. for (i = 0; i < rows; ++i)
  873. {
  874. matrixImg[i] = new float[cols];
  875. }
  876. for(i=0;i<rows;i++)
  877. {
  878. for(j=0;j<cols;j++)
  879. {
  880. module=sqrt(pow(filteredImageVertical[i][j],2)+pow(filteredImageHorizontal[i][j],2));
  881. matrixImg[i][j]=module;
  882. }
  883. }
  884. normalizeMatrix(matrixImg, rows, cols);
  885. filteredImage=matrixToImage(matrixImg,rows,cols);
  886. return filteredImage;
  887. }
  888. QImage prewittPhaseFilter(QImage originalImage)
  889. {
  890. float **filteredImageVertical=prewittVerticalFilter(originalImage);
  891. float **filteredImageHorizontal=prewittHorizontalFilter(originalImage);
  892. QImage filteredImage;
  893. double phase=0;
  894. int i,j;
  895. float **matrixImg;
  896. int rows=originalImage.width()-2;
  897. int cols=originalImage.height()-2;
  898. matrixImg = new float*[rows];
  899. for (i = 0; i < rows; ++i)
  900. {
  901. matrixImg[i] = new float[cols];
  902. }
  903. for(i=0;i<rows;i++)
  904. {
  905. for(j=0;j<cols;j++)
  906. {
  907. phase=atan2(filteredImageVertical[i][j],filteredImageHorizontal[i][j]);
  908. matrixImg[i][j]=phase;
  909. }
  910. }
  911. normalizeMatrix(matrixImg, rows, cols);
  912. filteredImage=matrixToImage(matrixImg,rows,cols);
  913. return filteredImage;
  914. }
  915. void rotateKernel(float kernel[3][3])
  916. {
  917. float rotatedNewKernel[3][3];
  918. int i,j;
  919. rotatedNewKernel[0][0]=kernel[0][1];
  920. rotatedNewKernel[0][1]=kernel[0][2];
  921. rotatedNewKernel[0][2]=kernel[1][2];
  922. rotatedNewKernel[1][0]=kernel[0][0];
  923. rotatedNewKernel[1][1]=kernel[1][1];
  924. rotatedNewKernel[1][2]=kernel[2][2];
  925. rotatedNewKernel[2][0]=kernel[1][0];
  926. rotatedNewKernel[2][1]=kernel[2][0];
  927. rotatedNewKernel[2][2]=kernel[2][1];
  928. for(i=0;i<3;i++)
  929. {
  930. for(j=0;j<3;j++)
  931. {
  932. kernel[i][j]=rotatedNewKernel[i][j];
  933. }
  934. }
  935. }
  936. QImage kirschFilter(QImage originalImage)
  937. {
  938. float **filteredMatrix;
  939. float **finalFilteredImage;
  940. QImage filteredImage;
  941. int rows=originalImage.width()-2;
  942. int cols=originalImage.height()-2;
  943. float **matrixImg;
  944. int i,j,k;
  945. matrixImg = new float*[rows];
  946. for (i = 0; i < rows; ++i)
  947. {
  948. matrixImg[i] = new float[cols];
  949. }
  950. float kernelKirschFilter[3][3]={5,5,5,-3,0,-3,-3,-3,-3};
  951. filteredMatrix=matrixImageConvolution3by3(originalImage,kernelKirschFilter);
  952. finalFilteredImage=matrixImageConvolution3by3(originalImage,kernelKirschFilter);
  953. rotateKernel(kernelKirschFilter);
  954. for(i=0;i<7;i++)
  955. {
  956. filteredMatrix=matrixImageConvolution3by3(originalImage,kernelKirschFilter);
  957. for(j=0;j<rows;j++)
  958. {
  959. for(k=0;k<cols;k++)
  960. {
  961. if(filteredMatrix[j][k]>finalFilteredImage[j][k])
  962. {
  963. finalFilteredImage[j][k]=filteredMatrix[j][k];
  964. }
  965. }
  966. }
  967. rotateKernel(kernelKirschFilter);
  968. }
  969. normalizeMatrix(finalFilteredImage, rows, cols);
  970. filteredImage=matrixToImage(finalFilteredImage,rows,cols);
  971. return filteredImage;
  972. }
  973. float **thresholdMatrix(float **matrixToThreshold, int rows, int cols)
  974. {
  975. int i,j;
  976. float **thresholdedMatrix;
  977. thresholdedMatrix = new float*[rows];
  978. for (i = 0; i < rows; ++i)
  979. {
  980. thresholdedMatrix[i] = new float[cols];
  981. }
  982. for(i=0;i<rows;i++)
  983. {
  984. for(j=0;j<cols;j++)
  985. {
  986. if(matrixToThreshold[i][j]>=0)
  987. {
  988. thresholdedMatrix[i][j]=255;
  989. }
  990. else
  991. {
  992. thresholdedMatrix[i][j]=0;
  993. }
  994. }
  995. }
  996. return thresholdedMatrix;
  997. }
  998. QImage threeNineFilter(QImage originalImage)
  999. {
  1000. float **filteredMatrix;
  1001. float **sumMatrix;
  1002. float **finalFilteredImage;
  1003. QImage filteredImage;
  1004. int rows=originalImage.width()-2;
  1005. int cols=originalImage.height()-2;
  1006. float **matrixImg;
  1007. int i,j,k;
  1008. float kernelBlockSum[3][3]={1,1,1,1,1,1,1,1,1};
  1009. float kernelThreeNineFilter[3][3]={1,1,1,0,0,0,0,0,0};
  1010. matrixImg = new float*[rows];
  1011. for (i = 0; i < rows; ++i)
  1012. {
  1013. matrixImg[i] = new float[cols];
  1014. }
  1015. sumMatrix=matrixImageConvolution3by3(originalImage,kernelBlockSum);
  1016. filteredMatrix=matrixImageConvolution3by3(originalImage,kernelThreeNineFilter);
  1017. finalFilteredImage=matrixImageConvolution3by3(originalImage,kernelThreeNineFilter);
  1018. rotateKernel(kernelThreeNineFilter);
  1019. for(i=0;i<7;i++)
  1020. {
  1021. filteredMatrix=matrixImageConvolution3by3(originalImage,kernelThreeNineFilter);
  1022. for(j=0;j<rows;j++)
  1023. {
  1024. for(k=0;k<cols;k++)
  1025. {
  1026. if(filteredMatrix[j][k]>finalFilteredImage[j][k])
  1027. {
  1028. finalFilteredImage[j][k]=filteredMatrix[j][k];
  1029. }
  1030. }
  1031. }
  1032. rotateKernel(kernelThreeNineFilter);
  1033. }
  1034. for(j=0;j<rows;j++)
  1035. {
  1036. for(k=0;k<cols;k++)
  1037. {
  1038. if(sumMatrix[j][k]==0)
  1039. {
  1040. matrixImg[j][k]=0;
  1041. }
  1042. else
  1043. {
  1044. matrixImg[j][k]=1.5*( (finalFilteredImage[j][k]/sumMatrix[j][k])-0.333 );
  1045. }
  1046. }
  1047. }
  1048. normalizeMatrix(matrixImg, rows, cols);
  1049. filteredImage=matrixToImage(matrixImg,rows,cols);
  1050. return filteredImage;
  1051. }
  1052. QImage DoGFilter(QImage originalImage, float firstVar, float secondVar)
  1053. {
  1054. QImage filteredImage,thresholdedImage;
  1055. float **maxVarKernel;
  1056. float **minVarKernel;
  1057. float **diffKernel;
  1058. float **convolutedMatrix;
  1059. float **thresholdedMatrix;
  1060. float maxVar,minVar;
  1061. int kernelMaxDim;
  1062. int i,j;
  1063. if(firstVar>=secondVar)
  1064. {
  1065. maxVar=firstVar;
  1066. minVar=secondVar;
  1067. }
  1068. else
  1069. {
  1070. maxVar=secondVar;
  1071. minVar=firstVar;
  1072. }
  1073. float floatKernelMaxDim=6.0*maxVar;
  1074. if((int)floatKernelMaxDim%2==0)
  1075. {
  1076. kernelMaxDim=(int)floatKernelMaxDim+1;
  1077. }
  1078. else
  1079. {
  1080. kernelMaxDim=(int)floatKernelMaxDim;
  1081. }
  1082. int rows = originalImage.width()-(kernelMaxDim-1);
  1083. int cols = originalImage.height()-(kernelMaxDim-1);
  1084. diffKernel = new float*[kernelMaxDim];
  1085. for (i = 0; i < kernelMaxDim; ++i)
  1086. {
  1087. diffKernel[i] = new float[kernelMaxDim];
  1088. }
  1089. maxVarKernel=generateGaussianKernel(maxVar, kernelMaxDim);
  1090. minVarKernel=generateGaussianKernel(minVar, kernelMaxDim);
  1091. for(i=0;i<kernelMaxDim;i++)
  1092. {
  1093. for(j=0;j<kernelMaxDim;j++)
  1094. {
  1095. diffKernel[i][j]=maxVarKernel[i][j]-minVarKernel[i][j];
  1096. }
  1097. }
  1098. convolutedMatrix=matrixConvolutionNbyN(originalImage,diffKernel,kernelMaxDim);
  1099. //normalizeMatrix(convolutedMatrix,rows,cols);
  1100. thresholdedMatrix=thresholdMatrix(convolutedMatrix,rows,cols);
  1101. //thresholdedImage=matrixToImage(thresholdedMatrix,rows,cols);
  1102. float **sumMatrix;//=matrixImageConvolution3by3(thresholdedImage,unitaryKernel);
  1103. sumMatrix = new float*[rows-2];
  1104. for (i = 0; i <rows-2; ++i)
  1105. {
  1106. sumMatrix[i] = new float[cols-2];
  1107. }
  1108. int center;
  1109. for(i=1;i<rows-1;i++)
  1110. {
  1111. for(j=1;j<cols-1;j++)
  1112. {
  1113. center=thresholdedMatrix[i][j];
  1114. if(thresholdedMatrix[i-1][j-1]!=center || thresholdedMatrix[i-1][j]!=center || thresholdedMatrix[i-1][j+1]!=center || thresholdedMatrix[i][j-1]!=center || thresholdedMatrix[i][j+1]!=center || thresholdedMatrix[i+1][j-1]!=center || thresholdedMatrix[i+1][j]!=center || thresholdedMatrix[i+1][j+1]!=center)
  1115. {
  1116. sumMatrix[i-1][j-1]=255;
  1117. }
  1118. else
  1119. {
  1120. sumMatrix[i-1][j-1]=0;
  1121. }
  1122. }
  1123. }
  1124. filteredImage=matrixToImage(sumMatrix,rows-2,cols-2);
  1125. return filteredImage;
  1126. }
  1127. double randomValue()
  1128. {
  1129. double randNumber = ((double) rand() / (RAND_MAX));
  1130. return randNumber;
  1131. }
  1132. int randomValueFromMinToMax(int min, int max)
  1133. {
  1134. int randNumber=(rand()%(max-min))+min;
  1135. return randNumber;
  1136. }
  1137. double nextGaussian(double *nextNextGaussian, bool *haveNextNextGaussian)
  1138. {
  1139. if (*haveNextNextGaussian)
  1140. {
  1141. *haveNextNextGaussian = false;
  1142. return *nextNextGaussian;
  1143. }
  1144. else
  1145. {
  1146. double v1, v2, s;
  1147. do
  1148. {
  1149. v1 = 2 * randomValue() - 1;
  1150. v2 = 2 * randomValue() - 1;
  1151. s = v1 * v1 + v2 * v2;
  1152. }
  1153. while (s >= 1 || s == 0);
  1154. double multiplier = sqrt(-2 * log(s)/s);
  1155. *nextNextGaussian = v2 * multiplier;
  1156. *haveNextNextGaussian = true;
  1157. return v1 * multiplier;
  1158. }
  1159. }
  1160. QImage uniformNoise(QImage originalImage, int maxNoiseIntensity)
  1161. {
  1162. QImage filteredImage;
  1163. QTime time = QTime::currentTime();
  1164. qsrand((uint)time.msec());
  1165. float **matrixImg;
  1166. int rows=originalImage.width();
  1167. int cols=originalImage.height();
  1168. int i,j;
  1169. double computation=0;
  1170. double computationWithNoise=0;
  1171. matrixImg = new float*[rows];
  1172. for (i = 0; i < rows; ++i)
  1173. {
  1174. matrixImg[i] = new float[cols];
  1175. }
  1176. for(i=0;i<rows;i++)
  1177. {
  1178. for(j=0;j<cols;j++)
  1179. {
  1180. double randNumber = ((double) rand() / (RAND_MAX));
  1181. computation= 2*maxNoiseIntensity*(randNumber-0.5);
  1182. computationWithNoise=originalImage.pixelIndex(i,j)+computation;
  1183. if(computationWithNoise>=255)
  1184. {
  1185. matrixImg[i][j]=255;
  1186. }
  1187. else if(computationWithNoise<=0)
  1188. {
  1189. matrixImg[i][j]=0;
  1190. }
  1191. else
  1192. {
  1193. matrixImg[i][j]=computationWithNoise;
  1194. }
  1195. }
  1196. }
  1197. filteredImage=matrixToImage(matrixImg,rows,cols);
  1198. return filteredImage;
  1199. }
  1200. QImage gaussianNoise(QImage originalImage, float mean, float variance)
  1201. {
  1202. QImage filteredImage;
  1203. double nextNextGaussian=0;
  1204. bool haveNextNextGaussian = false;
  1205. float **matrixImg;
  1206. int rows=originalImage.width();
  1207. int cols=originalImage.height();
  1208. int i,j;
  1209. double computation=0;
  1210. double computationWithNoise=0;
  1211. matrixImg = new float*[rows];
  1212. for (i = 0; i < rows; ++i)
  1213. {
  1214. matrixImg[i] = new float[cols];
  1215. }
  1216. for(i=0;i<rows;i++)
  1217. {
  1218. for(j=0;j<cols;j++)
  1219. {
  1220. computation= (nextGaussian(&nextNextGaussian,&haveNextNextGaussian) * sqrt(variance)) + mean;
  1221. computationWithNoise=originalImage.pixelIndex(i,j)+computation;
  1222. if(computationWithNoise>=255)
  1223. {
  1224. matrixImg[i][j]=255;
  1225. }
  1226. else if(computationWithNoise<=0)
  1227. {
  1228. matrixImg[i][j]=0;
  1229. }
  1230. else
  1231. {
  1232. matrixImg[i][j]=computationWithNoise;
  1233. }
  1234. }
  1235. }
  1236. filteredImage=matrixToImage(matrixImg,rows,cols);
  1237. return filteredImage;
  1238. }
  1239. QImage saltPepperNoise(QImage originalImage, int percentage)
  1240. {
  1241. QImage filteredImage=originalImage;
  1242. float floatedPercentage= (float) percentage/100;
  1243. int rows=originalImage.width();
  1244. int cols=originalImage.height();
  1245. int totalPixels=rows*cols;
  1246. int modifiedPixels= (int) totalPixels*floatedPercentage;
  1247. int i;
  1248. int xCasual, yCasual;
  1249. int flipCoin;
  1250. for(i=0;i<modifiedPixels;i++)
  1251. {
  1252. xCasual=randomValueFromMinToMax(0,rows);
  1253. yCasual=randomValueFromMinToMax(0,cols);
  1254. flipCoin=randomValueFromMinToMax(0,2);
  1255. if(flipCoin==0)
  1256. {
  1257. filteredImage.setPixel(xCasual,yCasual,0);
  1258. }
  1259. else
  1260. {
  1261. filteredImage.setPixel(xCasual,yCasual,255);
  1262. }
  1263. }
  1264. return filteredImage;
  1265. }
  1266. QImage impulseNoise(QImage originalImage, int percentage)
  1267. {
  1268. QImage filteredImage=originalImage;
  1269. float floatedPercentage= (float) percentage/100;
  1270. int rows=originalImage.width();
  1271. int cols=originalImage.height();
  1272. int totalPixels=rows*cols;
  1273. int modifiedPixels= (int) totalPixels*floatedPercentage;
  1274. int i;
  1275. int xCasual, yCasual;
  1276. for(i=0;i<modifiedPixels;i++)
  1277. {
  1278. xCasual=randomValueFromMinToMax(0,rows);
  1279. yCasual=randomValueFromMinToMax(0,cols);
  1280. filteredImage.setPixel(xCasual,yCasual,255);
  1281. }
  1282. return filteredImage;
  1283. }