ImageStatistics.cs

Description du code

ImageStatistics.cs est un fichier du projet PhotoBrol.
Ce fichier est situé dans /var/www/bin/sniplets/bibliobrol/photobrol/.

Projet PhotoBrol :

Editeur d'images en CSharp.

Code source ou contenu du fichier

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Drawing.Imaging;
  5. using System.Drawing;
  6. using be.gaudry.model.math;
  7. using be.gaudry.model.config;
  8. using be.gaudry.model.drawing.colors.converter;
  9.  
  10. namespace be.gaudry.photobrol.model.image
  11. {
  12. /// <summary>
  13. /// Gather statistics about the image in RGB color space
  14. /// </summary>
  15. ///
  16. /// <remarks></remarks>
  17. ///
  18. public class ImageStatistics
  19. {
  20. private Histogram red;
  21. private Histogram green;
  22. private Histogram blue;
  23. private Histogram gray;
  24.  
  25. private Histogram redWithoutBlack;
  26. private Histogram greenWithoutBlack;
  27. private Histogram blueWithoutBlack;
  28. private Histogram grayWithoutBlack;
  29.  
  30. private int pixels;
  31. private int pixelsWithoutBlack;
  32. private bool grayscale;
  33.  
  34. /// <summary>
  35. /// Histogram of red channel
  36. /// </summary>
  37. ///
  38. public Histogram Red
  39. {
  40. get { return red; }
  41. }
  42.  
  43. /// <summary>
  44. /// Histogram of green channel
  45. /// </summary>
  46. ///
  47. public Histogram Green
  48. {
  49. get { return green; }
  50. }
  51.  
  52. /// <summary>
  53. /// Histogram of blue channel
  54. /// </summary>
  55. ///
  56. public Histogram Blue
  57. {
  58. get { return blue; }
  59. }
  60.  
  61. /// <summary>
  62. /// Histogram of gray channel (intensities)
  63. /// </summary>
  64. ///
  65. public Histogram Gray
  66. {
  67. get { return gray; }
  68. }
  69.  
  70. /// <summary>
  71. /// Histogram of the channel excluding black pixels
  72. /// </summary>
  73. ///
  74. public Histogram RedWithoutBlack
  75. {
  76. get { return redWithoutBlack; }
  77. }
  78.  
  79. /// <summary>
  80. /// Histogram of green channel excluding black pixels
  81. /// </summary>
  82. ///
  83. public Histogram GreenWithoutBlack
  84. {
  85. get { return greenWithoutBlack; }
  86. }
  87.  
  88. /// <summary>
  89. /// Histogram of blue channel excluding black pixels
  90. /// </summary>
  91. ///
  92. public Histogram BlueWithoutBlack
  93. {
  94. get { return blueWithoutBlack; }
  95. }
  96.  
  97. /// <summary>
  98. /// Histogram of gray channel (intensities) channel excluding black pixels
  99. /// </summary>
  100. ///
  101. public Histogram GrayWithoutBlack
  102. {
  103. get { return grayWithoutBlack; }
  104. }
  105.  
  106. /// <summary>
  107. /// Total pixel count of the image
  108. /// </summary>
  109. ///
  110. public int PixelsCount
  111. {
  112. get { return pixels; }
  113. }
  114.  
  115. /// <summary>
  116. /// Total pixel count of the image excluding black pixels
  117. /// </summary>
  118. ///
  119. public int PixelsCountWithoutBlack
  120. {
  121. get { return pixelsWithoutBlack; }
  122. }
  123.  
  124. /// <summary>
  125. /// Value wich specifies if the image is color or grayscale
  126. /// </summary>
  127. ///
  128. public bool IsGrayscale
  129. {
  130. get { return grayscale; }
  131. }
  132.  
  133. /// <summary>
  134. /// Initializes a new instance of the <see cref="ImageStatistics"/> class
  135. /// </summary>
  136. ///
  137. /// <param name="image">Image to gather statistics about</param>
  138. ///
  139. /// <remarks>24 bit per pixel or 8 bit indexed (grayscale) images
  140. /// are supported only.</remarks>
  141. ///
  142. public ImageStatistics(Bitmap image)
  143. {
  144. PixelFormat fmt = (image.PixelFormat == PixelFormat.Format8bppIndexed) ?
  145. PixelFormat.Format8bppIndexed : PixelFormat.Format24bppRgb;
  146.  
  147. // lock bitmap data
  148. BitmapData imageData = image.LockBits(
  149. new Rectangle(0, 0, image.Width, image.Height),
  150. ImageLockMode.ReadOnly, fmt);
  151.  
  152. // gather statistics
  153. ProcessImage(imageData);
  154.  
  155. // unlock image
  156. image.UnlockBits(imageData);
  157. SourcesManager.addSource(new Source(
  158. "AForge.NET",
  159. this.ToString(),
  160. "http://aforge.googlecode.com/svn/trunk/Sources/",
  161. "Informations générales sur le traitement d'images"
  162. )
  163. );
  164. }
  165.  
  166. /// <summary>
  167. /// Initializes a new instance of the <see cref="ImageStatistics"/> class
  168. /// </summary>
  169. ///
  170. /// <param name="imageData">Image data to gather statistics about</param>
  171. ///
  172. /// <remarks>24 bit per pixel or 8 bit indexed (grayscale) images
  173. /// are supported only.</remarks>
  174. ///
  175. public ImageStatistics(BitmapData imageData)
  176. {
  177. ProcessImage(imageData);
  178. SourcesManager.addSource(new Source(
  179. "AForge.NET",
  180. this.ToString(),
  181. "http://aforge.googlecode.com/svn/trunk/Sources/",
  182. "Informations générales sur le traitement d'images"
  183. )
  184. );
  185. }
  186.  
  187. /// <summary>
  188. /// Gather statistics about specified image
  189. /// </summary>
  190. ///
  191. /// <param name="imageData">Image data</param>
  192. ///
  193. private void ProcessImage(BitmapData imageData)
  194. {
  195. // get image dimension
  196. int width = imageData.Width;
  197. int height = imageData.Height;
  198.  
  199. pixels = pixelsWithoutBlack = 0;
  200.  
  201. // check pixel format
  202. if (grayscale = (imageData.PixelFormat == PixelFormat.Format8bppIndexed))
  203. {
  204. // alloc arrays
  205. int[] g = new int[256];
  206. int[] gwb = new int[256];
  207.  
  208. byte value;
  209. int offset = imageData.Stride - width;
  210.  
  211. // do the job
  212. unsafe
  213. {
  214. byte* p = (byte*)imageData.Scan0.ToPointer();
  215.  
  216. // for each pixel
  217. for (int y = 0; y < height; y++)
  218. {
  219. // for each pixel
  220. for (int x = 0; x < width; x++, p++)
  221. {
  222. // get pixel value
  223. value = *p;
  224.  
  225. g[value]++;
  226. pixels++;
  227.  
  228. if (value != 0)
  229. {
  230. gwb[value]++;
  231. pixelsWithoutBlack++;
  232. }
  233. }
  234. p += offset;
  235. }
  236. }
  237.  
  238. // create historgram for gray level
  239. gray = new Histogram(g);
  240. grayWithoutBlack = new Histogram(gwb);
  241. }
  242. else
  243. {
  244. // alloc arrays
  245. int[] r = new int[256];
  246. int[] g = new int[256];
  247. int[] b = new int[256];
  248.  
  249. int[] rwb = new int[256];
  250. int[] gwb = new int[256];
  251. int[] bwb = new int[256];
  252.  
  253. byte rValue, gValue, bValue;
  254. int offset = imageData.Stride - width * 3;
  255.  
  256. // do the job
  257. unsafe
  258. {
  259. byte* p = (byte*)imageData.Scan0.ToPointer();
  260.  
  261. // for each line
  262. for (int y = 0; y < height; y++)
  263. {
  264. // for each pixel
  265. for (int x = 0; x < width; x++, p += 3)
  266. {
  267. // get pixel values
  268. rValue = p[RGB.R];
  269. gValue = p[RGB.G];
  270. bValue = p[RGB.B];
  271.  
  272. r[rValue]++;
  273. g[gValue]++;
  274. b[bValue]++;
  275. pixels++;
  276.  
  277. if ((rValue != 0) || (gValue != 0) || (bValue != 0))
  278. {
  279. rwb[rValue]++;
  280. gwb[gValue]++;
  281. bwb[bValue]++;
  282. pixelsWithoutBlack++;
  283. }
  284. }
  285. p += offset;
  286. }
  287. }
  288.  
  289. // create histograms
  290. red = new Histogram(r);
  291. green = new Histogram(g);
  292. blue = new Histogram(b);
  293.  
  294. redWithoutBlack = new Histogram(rwb);
  295. greenWithoutBlack = new Histogram(gwb);
  296. blueWithoutBlack = new Histogram(bwb);
  297. }
  298. }
  299. }
  300. }

Structure et Fichiers du projet

Afficher/masquer...


Répertoires contenus dans /var/www/bin/sniplets/bibliobrol/photobrol/model/Image/ 
IcôneNomTailleModification
Pas de sous-répertoires.
IcôneNomTailleModification
| _ Répertoire parent0 octets1732500369 25/11/2024 03:06:09
Fichiers contenus dans /var/www/bin/sniplets/bibliobrol/photobrol/model/Image/ 
IcôneNomTailleModificationAction
IcôneNomTailleModificationAction
Afficher le fichier .cs|.csImageStatistics.cs8.92 Ko31/10/2018 18:32:51-refusé-
Afficher le fichier .cs|.csPhotoBrolImage.cs751 octets31/10/2018 18:32:51-refusé-

Utilisation de l'explorateur de code

  • Navigation :
    • Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
    • Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
    • La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
    • Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
  • Affichage :
    • Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
  • Actions :
    • Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 30/10/2009, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/cs-photobrol-source-rf-model/Image//ImageStatistics.cs.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.