PageRenderTime 109ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/samples/BlobDemo.java

https://gitlab.com/cvtung/javacv
Java | 271 lines | 220 code | 31 blank | 20 comment | 64 complexity | 12086c569f0b679c9414da3b19bd5663 MD5 | raw file
  1. import org.bytedeco.javacv.Blobs;
  2. import org.bytedeco.javacv.CanvasFrame;
  3. import org.bytedeco.javacv.OpenCVFrameConverter;
  4. import static org.bytedeco.javacpp.opencv_core.*;
  5. import static org.bytedeco.javacpp.opencv_imgcodecs.*;
  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. OpenCVFrameConverter converter = new OpenCVFrameConverter.ToIplImage();
  109. canvas.showImage(converter.convert(image));
  110. }
  111. public static void Highlight(IplImage image, int [] inVec)
  112. {
  113. Highlight(image, inVec[0], inVec[1], inVec[2], inVec[3], 1);
  114. }
  115. public static void Highlight(IplImage image, int [] inVec, int Thick)
  116. {
  117. Highlight(image, inVec[0], inVec[1], inVec[2], inVec[3], Thick);
  118. }
  119. public static void Highlight(IplImage image, int xMin, int yMin, int xMax, int yMax)
  120. {
  121. Highlight(image, xMin, yMin, xMax, yMax, 1);
  122. }
  123. public static void Highlight(IplImage image, int xMin, int yMin, int xMax, int yMax, int Thick)
  124. {
  125. CvPoint pt1 = cvPoint(xMin,yMin);
  126. CvPoint pt2 = cvPoint(xMax,yMax);
  127. CvScalar color = cvScalar(255,0,0,0); // blue [green] [red]
  128. cvRectangle(image, pt1, pt2, color, Thick, 4, 0);
  129. }
  130. public static void PrintGrayImage(IplImage image, String caption)
  131. {
  132. int size = 512; // impractical to print anything larger
  133. CvMat mat = image.asCvMat();
  134. int cols = mat.cols(); if(cols < 1) cols = 1;
  135. int rows = mat.rows(); if(rows < 1) rows = 1;
  136. double aspect = 1.0 * cols / rows;
  137. if(rows > size) { rows = size; cols = (int) ( rows * aspect ); }
  138. if(cols > size) cols = size;
  139. rows = (int) ( cols / aspect );
  140. PrintGrayImage(image, caption, 0, cols, 0, rows);
  141. }
  142. public static void PrintGrayImage(IplImage image, String caption, int MinX, int MaxX, int MinY, int MaxY)
  143. {
  144. int size = 512; // impractical to print anything larger
  145. CvMat mat = image.asCvMat();
  146. int cols = mat.cols(); if(cols < 1) cols = 1;
  147. int rows = mat.rows(); if(rows < 1) rows = 1;
  148. if(MinX < 0) MinX = 0; if(MinX > cols) MinX = cols;
  149. if(MaxX < 0) MaxX = 0; if(MaxX > cols) MaxX = cols;
  150. if(MinY < 0) MinY = 0; if(MinY > rows) MinY = rows;
  151. if(MaxY < 0) MaxY = 0; if(MaxY > rows) MaxY = rows;
  152. System.out.println("\n" + caption);
  153. System.out.print(" +");
  154. for(int icol = MinX; icol < MaxX; icol++) System.out.print("-");
  155. System.out.println("+");
  156. for(int irow = MinY; irow < MaxY; irow++)
  157. {
  158. if(irow<10) System.out.print(" ");
  159. if(irow<100) System.out.print(" ");
  160. System.out.print(irow);
  161. System.out.print("|");
  162. for(int icol = MinX; icol < MaxX; icol++)
  163. {
  164. int val = (int) mat.get(irow,icol);
  165. String C = " ";
  166. if(val == 0) C = "*";
  167. System.out.print(C);
  168. }
  169. System.out.println("|");
  170. }
  171. System.out.print(" +");
  172. for(int icol = MinX; icol < MaxX; icol++) System.out.print("-");
  173. System.out.println("+");
  174. }
  175. public static void PrintImageProperties(IplImage image)
  176. {
  177. CvMat mat = image.asCvMat();
  178. int cols = mat.cols();
  179. int rows = mat.rows();
  180. int depth = mat.depth();
  181. System.out.println("ImageProperties for " + image + " : cols=" + cols + " rows=" + rows + " depth=" + depth);
  182. }
  183. public static float BinaryHistogram(IplImage image)
  184. {
  185. CvScalar Sum = cvSum(image);
  186. float WhitePixels = (float) ( Sum.getVal(0) / 255 );
  187. CvMat mat = image.asCvMat();
  188. float TotalPixels = mat.cols() * mat.rows();
  189. //float BlackPixels = TotalPixels - WhitePixels;
  190. return WhitePixels / TotalPixels;
  191. }
  192. // Counterclockwise small angle rotation by skewing - Does not stretch border pixels
  193. public static IplImage SkewGrayImage(IplImage Src, double angle) // angle is in radians
  194. {
  195. //double radians = - Math.PI * angle / 360.0; // Half because skew is horizontal and vertical
  196. double sin = - Math.sin(angle);
  197. double AbsSin = Math.abs(sin);
  198. int nChannels = Src.nChannels();
  199. if(nChannels != 1)
  200. {
  201. System.out.println("ERROR: SkewGrayImage: Require 1 channel: nChannels=" + nChannels);
  202. System.exit(1);
  203. }
  204. CvMat SrcMat = Src.asCvMat();
  205. int SrcCols = SrcMat.cols();
  206. int SrcRows = SrcMat.rows();
  207. double WidthSkew = AbsSin * SrcRows;
  208. double HeightSkew = AbsSin * SrcCols;
  209. int DstCols = (int) ( SrcCols + WidthSkew );
  210. int DstRows = (int) ( SrcRows + HeightSkew );
  211. CvMat DstMat = cvCreateMat(DstRows, DstCols, CV_8UC1); // Type matches IPL_DEPTH_8U
  212. cvSetZero(DstMat);
  213. cvNot(DstMat, DstMat);
  214. for(int irow = 0; irow < DstRows; irow++)
  215. {
  216. int dcol = (int) ( WidthSkew * irow / SrcRows );
  217. for(int icol = 0; icol < DstCols; icol++)
  218. {
  219. int drow = (int) ( HeightSkew - HeightSkew * icol / SrcCols );
  220. int jrow = irow - drow;
  221. int jcol = icol - dcol;
  222. if(jrow < 0 || jcol < 0 || jrow >= SrcRows || jcol >= SrcCols) DstMat.put(irow, icol, 255);
  223. else DstMat.put(irow, icol, (int) SrcMat.get(jrow,jcol));
  224. }
  225. }
  226. IplImage Dst = cvCreateImage(cvSize(DstCols, DstRows), IPL_DEPTH_8U, 1);
  227. Dst = DstMat.asIplImage();
  228. return Dst;
  229. }
  230. public static IplImage TransposeImage(IplImage SrcImage)
  231. {
  232. CvMat mat = SrcImage.asCvMat();
  233. int cols = mat.cols();
  234. int rows = mat.rows();
  235. IplImage DstImage = cvCreateImage(cvSize(rows, cols), IPL_DEPTH_8U, 1);
  236. cvTranspose(SrcImage, DstImage);
  237. cvFlip(DstImage,DstImage,1);
  238. return DstImage;
  239. }
  240. }