Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to efficiently compute the average "direction" of pixels in a grayscale image?
    text
    copied!<p>So I figured out I can convert an image to grayscale like this:</p> <pre><code>public static Bitmap GrayScale(this Image img) { var bmp = new Bitmap(img.Width, img.Height); using(var g = Graphics.FromImage(bmp)) { var colorMatrix = new ColorMatrix( new[] { new[] {.30f, .30f, .30f, 0, 0}, new[] {.59f, .59f, .59f, 0, 0}, new[] {.11f, .11f, .11f, 0, 0}, new[] {0, 0, 0, 1.0f, 0}, new[] {0, 0, 0, 0, 1.0f} }); using(var attrs = new ImageAttributes()) { attrs.SetColorMatrix(colorMatrix); g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, attrs); } } return bmp; } </code></pre> <p>Now, I want to compute the average "direction" of the pixels.</p> <p>What I mean by that is that I want to look at say a 3x3 region, and then if the left side is darker than the right side, then the direction would be to the right, if the bottom is darker than the top, then the direction would be upwards, if the bottom-left is darker than the top-right, then the direction would be up-right. (Think of little vector arrows over every 3x3 region). Perhaps a better example is if you draw a grayscale gradient in photoshop, and you want to compute at what angle they drew it.</p> <p>I've done stuff like this MatLab, but that was years ago. I figure I could use a matrix similar to <code>ColorMatrix</code> to compute this, but I'm not quite sure how. It looks like <a href="http://msdn.microsoft.com/en-us/library/k1cfyd2w.aspx" rel="nofollow">this function</a> might be what I want; could I convert it to grayscale (as above) and then do something with the grayscale matrix to compute these directions?</p> <p>IIRC, what I want is quite similar to <a href="http://en.wikipedia.org/wiki/Edge_detection" rel="nofollow">edge detection</a>.</p> <p>After I compute these direction vectors, I'm just going to loop over them and compute the average direction of the image.</p> <p>The end goal is I want to rotate images so that their average direction is always upwards; this way if I have two identical images except one is rotated (90,180 or 270 degrees), they will end up oriented the same way (I'm not concerned if a person ends up upside down).</p> <hr> <p><strong>*snip*</strong> Deleting some spam. You can view the revisions of you want to read the rest of my attempts.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload