Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Graphics
  4. Cropping Surrounding White Area

Cropping Surrounding White Area

Scheduled Pinned Locked Moved Graphics
questiontutorial
3 Posts 3 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zubair_ahmed
    wrote on last edited by
    #1

    I am building a image cropper that should automatically detect the image boundry surrounded by white area and then crop it. The question is: how to detect the boundry of image surrounded by white area? I have tried the following code with little success. for (int j = 0; j < (height/2); j++) { count = 0; for (i = 0; i < (width/2); i++) { color = grays.GetPixel(j,i); if ((color.R == 255) && (color.B == 255) && (color.G == 255)) { height_top = i; width_left = j; } else break; } } This code should provide the upper left corner co-orninates but it does not. Thanks in Advance.

    Z.A

    M I 2 Replies Last reply
    0
    • Z zubair_ahmed

      I am building a image cropper that should automatically detect the image boundry surrounded by white area and then crop it. The question is: how to detect the boundry of image surrounded by white area? I have tried the following code with little success. for (int j = 0; j < (height/2); j++) { count = 0; for (i = 0; i < (width/2); i++) { color = grays.GetPixel(j,i); if ((color.R == 255) && (color.B == 255) && (color.G == 255)) { height_top = i; width_left = j; } else break; } } This code should provide the upper left corner co-orninates but it does not. Thanks in Advance.

      Z.A

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      I think it would be easier to scan top-down horizontally until you find a non-white pixel. That gives you the top. Then scan left-right vertically until you find a non-white pixel. That gives you the left. I think the code you've shown is just to get the top - maybe - unless there's colored pixels higher on the right half of the image :) Also, did you mean to put these arguments in this order? GetPixel(j,i); Mark

      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

      1 Reply Last reply
      0
      • Z zubair_ahmed

        I am building a image cropper that should automatically detect the image boundry surrounded by white area and then crop it. The question is: how to detect the boundry of image surrounded by white area? I have tried the following code with little success. for (int j = 0; j < (height/2); j++) { count = 0; for (i = 0; i < (width/2); i++) { color = grays.GetPixel(j,i); if ((color.R == 255) && (color.B == 255) && (color.G == 255)) { height_top = i; width_left = j; } else break; } } This code should provide the upper left corner co-orninates but it does not. Thanks in Advance.

        Z.A

        I Offline
        I Offline
        Ian Shlasko
        wrote on last edited by
        #3

        Simple bug... Unless I'm reading this wrong, that "break" in the else block is going to knock you right out of the "i" loop, but not out of the surrounding "j" loop. Hence it'll find the first non-white pixel, then continue on the next row. Also, as another reply mentioned, check your i/j for row/column. I suggest using variable names like "row" and "col" to avoid confusion, because it looks like "j" is counting rows, but being placed in "width_left", and vice versa. Oh, and if you want a more efficient algorithm, you might want to check pixels in a different order... What you're doing now is, using numbers to represent outer loop steps: 11111111 22222222 33333333 etc. I think what you really want to do is: 12345 22345 33345 44445 55555 etc. Little more complicated, but might work better.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups