PageRenderTime 144ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/curious-vision/src/test/java/se/caglabs/opencvsamples/BlobDemo.java

https://bitbucket.org/cagcontactor/jcurious
Java | 270 lines | 219 code | 31 blank | 20 comment | 64 complexity | dae307acd24ce82daa66e00c4ab25bf5 MD5 | raw file
Possible License(s): GPL-2.0
  1. package se.caglabs.opencvsamples;
  2. import org.bytedeco.javacv.Blobs;
  3. import org.bytedeco.javacv.CanvasFrame;
  4. import static org.bytedeco.javacpp.opencv_core.*;
  5. import static org.bytedeco.javacpp.opencv_highgui.*;
  6. import static org.bytedeco.javacpp.opencv_imgproc.*;
  7. ///////////////////////////////////////////////////////////////////
  8. //* *//
  9. //* As the author of this code, I place all of this code into *//
  10. //* the public domain. Users can use it for any legal purpose. *//
  11. //* *//
  12. //* - Dave Grossman *//
  13. //* *//
  14. ///////////////////////////////////////////////////////////////////
  15. public class BlobDemo
  16. {
  17. public static void main(String[] args)
  18. {
  19. System.out.println("STARTING...\n");
  20. demo();
  21. System.out.println("ALL DONE");
  22. }
  23. public static void demo()
  24. {
  25. int MinArea = 6;
  26. int ErodeCount =0;
  27. int DilateCount = 0;
  28. IplImage RawImage = null;
  29. // Read an image.
  30. for(int k = 0; k < 7; k++)
  31. {
  32. if(k == 0) { RawImage = cvLoadImage("BlackBalls.jpg"); MinArea = 250; ErodeCount = 0; DilateCount = 1; }
  33. else if(k == 1) { RawImage = cvLoadImage("Shapes1.jpg"); MinArea = 6; ErodeCount = 0; DilateCount = 1; }
  34. else if(k == 2) { RawImage = cvLoadImage("Shapes2.jpg"); MinArea = 250; ErodeCount = 0; DilateCount = 1; }
  35. else if(k == 3) { RawImage = cvLoadImage("Blob1.jpg"); MinArea = 2800; ErodeCount = 1; DilateCount = 1; }
  36. else if(k == 4) { RawImage = cvLoadImage("Blob2.jpg"); MinArea = 2800; ErodeCount = 1; DilateCount = 1; }
  37. else if(k == 5) { RawImage = cvLoadImage("Blob3.jpg"); MinArea = 2800; ErodeCount = 1; DilateCount = 1; }
  38. else if(k == 6) { RawImage = cvLoadImage("Rice.jpg"); MinArea = 30; ErodeCount = 2; DilateCount = 1; }
  39. //ShowImage(RawImage, "RawImage", 512);
  40. IplImage GrayImage = cvCreateImage(cvGetSize(RawImage), IPL_DEPTH_8U, 1);
  41. cvCvtColor(RawImage, GrayImage, CV_BGR2GRAY);
  42. //ShowImage(GrayImage, "GrayImage", 512);
  43. IplImage BWImage = cvCreateImage(cvGetSize(GrayImage), IPL_DEPTH_8U, 1);
  44. cvThreshold(GrayImage, BWImage, 127, 255, CV_THRESH_BINARY);
  45. //ShowImage(BWImage, "BWImage");
  46. IplImage WorkingImage = cvCreateImage(cvGetSize(BWImage), IPL_DEPTH_8U, 1);
  47. cvErode(BWImage, WorkingImage, null, ErodeCount);
  48. cvDilate(WorkingImage, WorkingImage, null, DilateCount);
  49. //ShowImage(WorkingImage, "WorkingImage", 512);
  50. //cvSaveImage("Working.jpg", WorkingImage);
  51. //PrintGrayImage(WorkingImage, "WorkingImage");
  52. //BinaryHistogram(WorkingImage);
  53. Blobs Regions = new Blobs();
  54. Regions.BlobAnalysis(
  55. WorkingImage, // image
  56. -1, -1, // ROI start col, row
  57. -1, -1, // ROI cols, rows
  58. 1, // border (0 = black; 1 = white)
  59. MinArea); // minarea
  60. Regions.PrintRegionData();
  61. for(int i = 1; i <= Blobs.MaxLabel; i++)
  62. {
  63. double [] Region = Blobs.RegionData[i];
  64. int Parent = (int) Region[Blobs.BLOBPARENT];
  65. int Color = (int) Region[Blobs.BLOBCOLOR];
  66. int MinX = (int) Region[Blobs.BLOBMINX];
  67. int MaxX = (int) Region[Blobs.BLOBMAXX];
  68. int MinY = (int) Region[Blobs.BLOBMINY];
  69. int MaxY = (int) Region[Blobs.BLOBMAXY];
  70. Highlight(RawImage, MinX, MinY, MaxX, MaxY, 1);
  71. }
  72. ShowImage(RawImage, "RawImage", 512);
  73. cvReleaseImage(GrayImage); GrayImage = null;
  74. cvReleaseImage(BWImage); BWImage = null;
  75. cvReleaseImage(WorkingImage); WorkingImage = null;
  76. }
  77. cvReleaseImage(RawImage); RawImage = null;
  78. }
  79. // Versions with 2, 3, and 4 parms respectively
  80. public static void ShowImage(IplImage image, String caption)
  81. {
  82. CvMat mat = image.asCvMat();
  83. int width = mat.cols(); if(width < 1) width = 1;
  84. int height = mat.rows(); if(height < 1) height = 1;
  85. double aspect = 1.0 * width / height;
  86. if(height < 128) { height = 128; width = (int) ( height * aspect ); }
  87. if(width < 128) width = 128;
  88. height = (int) ( width / aspect );
  89. ShowImage(image, caption, width, height);
  90. }
  91. public static void ShowImage(IplImage image, String caption, int size)
  92. {
  93. if(size < 128) size = 128;
  94. CvMat mat = image.asCvMat();
  95. int width = mat.cols(); if(width < 1) width = 1;
  96. int height = mat.rows(); if(height < 1) height = 1;
  97. double aspect = 1.0 * width / height;
  98. if(height != size) { height = size; width = (int) ( height * aspect ); }
  99. if(width != size) width = size;
  100. height = (int) ( width / aspect );
  101. ShowImage(image, caption, width, height);
  102. }
  103. public static void ShowImage(IplImage image, String caption, int width, int height)
  104. {
  105. CanvasFrame canvas = new CanvasFrame(caption, 1); // gamma=1
  106. canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
  107. canvas.setCanvasSize(width, height);
  108. canvas.showImage(image);
  109. }
  110. public static void Highlight(IplImage image, int [] inVec)
  111. {
  112. Highlight(image, inVec[0], inVec[1], inVec[2], inVec[3], 1);
  113. }
  114. public static void Highlight(IplImage image, int [] inVec, int Thick)
  115. {
  116. Highlight(image, inVec[0], inVec[1], inVec[2], inVec[3], Thick);
  117. }
  118. public static void Highlight(IplImage image, int xMin, int yMin, int xMax, int yMax)
  119. {
  120. Highlight(image, xMin, yMin, xMax, yMax, 1);
  121. }
  122. public static void Highlight(IplImage image, int xMin, int yMin, int xMax, int yMax, int Thick)
  123. {
  124. CvPoint pt1 = cvPoint(xMin,yMin);
  125. CvPoint pt2 = cvPoint(xMax,yMax);
  126. CvScalar color = cvScalar(255,0,0,0); // blue [green] [red]
  127. cvRectangle(image, pt1, pt2, color, Thick, 4, 0);
  128. }
  129. public static void PrintGrayImage(IplImage image, String caption)
  130. {
  131. int size = 512; // impractical to print anything larger
  132. CvMat mat = image.asCvMat();
  133. int cols = mat.cols(); if(cols < 1) cols = 1;
  134. int rows = mat.rows(); if(rows < 1) rows = 1;
  135. double aspect = 1.0 * cols / rows;
  136. if(rows > size) { rows = size; cols = (int) ( rows * aspect ); }
  137. if(cols > size) cols = size;
  138. rows = (int) ( cols / aspect );
  139. PrintGrayImage(image, caption, 0, cols, 0, rows);
  140. }
  141. public static void PrintGrayImage(IplImage image, String caption, int MinX, int MaxX, int MinY, int MaxY)
  142. {
  143. int size = 512; // impractical to print anything larger
  144. CvMat mat = image.asCvMat();
  145. int cols = mat.cols(); if(cols < 1) cols = 1;
  146. int rows = mat.rows(); if(rows < 1) rows = 1;
  147. if(MinX < 0) MinX = 0; if(MinX > cols) MinX = cols;
  148. if(MaxX < 0) MaxX = 0; if(MaxX > cols) MaxX = cols;
  149. if(MinY < 0) MinY = 0; if(MinY > rows) MinY = rows;
  150. if(MaxY < 0) MaxY = 0; if(MaxY > rows) MaxY = rows;
  151. System.out.println("\n" + caption);
  152. System.out.print(" +");
  153. for(int icol = MinX; icol < MaxX; icol++) System.out.print("-");
  154. System.out.println("+");
  155. for(int irow = MinY; irow < MaxY; irow++)
  156. {
  157. if(irow<10) System.out.print(" ");
  158. if(irow<100) System.out.print(" ");
  159. System.out.print(irow);
  160. System.out.print("|");
  161. for(int icol = MinX; icol < MaxX; icol++)
  162. {
  163. int val = (int) mat.get(irow,icol);
  164. String C = " ";
  165. if(val == 0) C = "*";
  166. System.out.print(C);
  167. }
  168. System.out.println("|");
  169. }
  170. System.out.print(" +");
  171. for(int icol = MinX; icol < MaxX; icol++) System.out.print("-");
  172. System.out.println("+");
  173. }
  174. public static void PrintImageProperties(IplImage image)
  175. {
  176. CvMat mat = image.asCvMat();
  177. int cols = mat.cols();
  178. int rows = mat.rows();
  179. int depth = mat.depth();
  180. System.out.println("ImageProperties for " + image + " : cols=" + cols + " rows=" + rows + " depth=" + depth);
  181. }
  182. public static float BinaryHistogram(IplImage image)
  183. {
  184. CvScalar Sum = cvSum(image);
  185. float WhitePixels = (float) ( Sum.getVal(0) / 255 );
  186. CvMat mat = image.asCvMat();
  187. float TotalPixels = mat.cols() * mat.rows();
  188. //float BlackPixels = TotalPixels - WhitePixels;
  189. return WhitePixels / TotalPixels;
  190. }
  191. // Counterclockwise small angle rotation by skewing - Does not stretch border pixels
  192. public static IplImage SkewGrayImage(IplImage Src, double angle) // angle is in radians
  193. {
  194. //double radians = - Math.PI * angle / 360.0; // Half because skew is horizontal and vertical
  195. double sin = - Math.sin(angle);
  196. double AbsSin = Math.abs(sin);
  197. int nChannels = Src.nChannels();
  198. if(nChannels != 1)
  199. {
  200. System.out.println("ERROR: SkewGrayImage: Require 1 channel: nChannels=" + nChannels);
  201. System.exit(1);
  202. }
  203. CvMat SrcMat = Src.asCvMat();
  204. int SrcCols = SrcMat.cols();
  205. int SrcRows = SrcMat.rows();
  206. double WidthSkew = AbsSin * SrcRows;
  207. double HeightSkew = AbsSin * SrcCols;
  208. int DstCols = (int) ( SrcCols + WidthSkew );
  209. int DstRows = (int) ( SrcRows + HeightSkew );
  210. CvMat DstMat = cvCreateMat(DstRows, DstCols, CV_8UC1); // Type matches IPL_DEPTH_8U
  211. cvSetZero(DstMat);
  212. cvNot(DstMat, DstMat);
  213. for(int irow = 0; irow < DstRows; irow++)
  214. {
  215. int dcol = (int) ( WidthSkew * irow / SrcRows );
  216. for(int icol = 0; icol < DstCols; icol++)
  217. {
  218. int drow = (int) ( HeightSkew - HeightSkew * icol / SrcCols );
  219. int jrow = irow - drow;
  220. int jcol = icol - dcol;
  221. if(jrow < 0 || jcol < 0 || jrow >= SrcRows || jcol >= SrcCols) DstMat.put(irow, icol, 255);
  222. else DstMat.put(irow, icol, (int) SrcMat.get(jrow,jcol));
  223. }
  224. }
  225. IplImage Dst = cvCreateImage(cvSize(DstCols, DstRows), IPL_DEPTH_8U, 1);
  226. Dst = DstMat.asIplImage();
  227. return Dst;
  228. }
  229. public static IplImage TransposeImage(IplImage SrcImage)
  230. {
  231. CvMat mat = SrcImage.asCvMat();
  232. int cols = mat.cols();
  233. int rows = mat.rows();
  234. IplImage DstImage = cvCreateImage(cvSize(rows, cols), IPL_DEPTH_8U, 1);
  235. cvTranspose(SrcImage, DstImage);
  236. cvFlip(DstImage,DstImage,1);
  237. return DstImage;
  238. }
  239. }