开发者

how to determine coordinates of element in the image?

开发者 https://www.devze.com 2023-01-13 22:08 出处:网络
I have an image which is pretty much b&w and it has a black square on white background. How can i determine cooridnates of this square?

I have an image which is pretty much b&w and it has a black square on white background. How can i determine cooridnates of this square?

It is actually scanned doc and these squares are used for calibration and positioning of the scanned image. I guess I need AI and neural networks f开发者_开发技巧or this but I'd rather use something simpler. I just need a coordinates of a pixel inside that square.

I can read image pixel by pixel using standard Java API but that seem to be naive solution.

Thanks.


You have to read the image pixel by pixel unless you want to use an image processing framework which uses pretty complex algorithms. But here are some tips to make the task more simple?

  1. How big is the rectangle? If you know that it's pure white and at least 10 pixels wide, you can scale the image down by a 10th. Now you just need to look for a single, white (or very light gray) pixel. Alternatively, you can only read every 10th pixel.

  2. Crop the image before you start your search, that is ask the Java image API for all pixels in a row and then check whether they are all the same. If so, then you can ignore that line.

  3. Of course, reality rarely is that sharp. So you need to run a filter on the image which turns a pixel white if at least 3 of its 8 neighbors are white and black if more than 6 neighbors are black.

  4. If you can, start with a gray image and convert that to B&W yourself. That way, you can optimize the result for your needs (for example, set the cut at 60 or 80 instead of 128).

0

精彩评论

暂无评论...
验证码 换一张
取 消