-3

I have 3 black dots in a scanned image that its size is 600x400 px, and I like to know the best and simple way in C# to get their coordinates, like this:

dot1 X=400px Y=100px
dot2 X=100px Y=200px
dot3 X=300px Y=300px

enter image description here

Yahya
  • 13,349
  • 6
  • 30
  • 42
CrownFord
  • 709
  • 5
  • 15
  • Could you explain the difficulty you are having? – Crowcoder May 12 '18 at 21:17
  • How many colors are there in the image? Only black dots and white background? – Yahya May 12 '18 at 21:17
  • Are you trying to detect the black dots using some sort of computer vision library? – Lee Oades May 12 '18 at 21:21
  • how do you expect the answer to look like? Some hints of where to start? some algorithm in words? the entire code how to solve this problem? up to now your question is too broad and you have provided no code – Mong Zhu May 12 '18 at 21:27
  • I still have no difficulty (because I still searching for the simplest way). The image has two colors only white (as background) and black as (a dot). I'm not trying to implement any library (OpenCV) and I do not prefer to. – CrownFord May 12 '18 at 21:28
  • I like the solution code to be simple and not too broad like this code in this page: https://dzone.com/articles/how-extract-omr-data-scanned – CrownFord May 12 '18 at 21:30
  • I dumped this into a point detection algo I wrote [for some other question](https://stackoverflow.com/questions/50277978/c-sharp-black-points-recognition-from-a-photo/50282882#50282882), but, the image has far too many similar colours to distinguish specific blobs. In the end, the only way I could get even [this result](https://i.stack.imgur.com/BHK35.png) was by setting the brightness threshold to 0.6 and then filtering out only the detected blobs with more than 100 points in them... both of which are kind of arbitrary for automation. – Nyerguds May 12 '18 at 21:45
  • But yea. The detection is the least of your worries here. you'll probably need to look into colour normalization (auto-contrast) and most likely k-means filtering of most distinctive colours before the blob detection even becomes relevant. – Nyerguds May 12 '18 at 21:51
  • @CrownFord Your question is tagged OCR... are you actually expecting to be able to write code that can _read those dimensions from the text hand-written onto the image?_ – Nyerguds May 12 '18 at 22:37

1 Answers1

1

I will take a long shot and assume that you only have two colors exactly as the image shows: Black dots and White background.

You may work around that and detect the occurrence of the black color, something like this as a start for your algorithm:

int HEIGHT = 400;
int WIDTH = 600;

// get the jpg image
Bitmap bitmap;
using(Stream bmpStream = System.IO.File.Open(fileName, System.IO.FileMode.Open )){
     Image image = Image.FromStream(bmpStream);
     bitmap = new Bitmap(image);
}

for (int x = 0; x < HEIGHT; x++){
  for (int y = 0; y < WIDTH; y++){
    Color pixelColor = bitmap.GetPixel(x, y);
    // check if it's black or a shade of black
    // e.g. if it belongs to an array of colors..etc
    // if so, record the coordinates (x,y)
  }
} 
Yahya
  • 13,349
  • 6
  • 30
  • 42
  • If the image name is "sheet.jpg", where it supposed to be in your code? – CrownFord May 12 '18 at 21:37
  • @CrownFord I will add this bit! – Yahya May 12 '18 at 21:40
  • 1
    @CrownFord are asking how to find the dots or how to load a jpeg into a bitmap object ? – Mong Zhu May 12 '18 at 21:41
  • My last question was about @Yahya code. – CrownFord May 12 '18 at 21:46
  • 3
    @CrownFord To be honest, "how to load an image" is a completely trivial detail considering your question. You should look up the basics of image handling in C# before starting on tasks like this... – Nyerguds May 12 '18 at 21:47
  • @Nyerguds I never used image manipulations in C# at all. – CrownFord May 12 '18 at 21:51
  • 2
    @CrownFord Then, I'm sorry to say, your question is more or less comparable to "Hello, I'm new to the field of medicine and would like to cure cancer. Any tips?" – Nyerguds May 12 '18 at 21:53
  • @Yahya Using GetPixel to loop over an entire image is [really, _really_ slow](https://stackoverflow.com/a/3115178/395685), though. It locks, reads and unlocks the underlying image memory for each of these calls. – Nyerguds May 12 '18 at 23:54
  • @Nyerguds so what do you think is the good solution though? – CrownFord May 13 '18 at 05:21
  • @CrownFord It depends on what you're actually trying to do. As I remarked on the question itself, you tagged this with OCR, giving the impression you want some system that just automagically knows that you drew a square on that that's supposed to be 600x400. That just isn't going to happen. That said, if you do some kind of edge detection you could probably figure out the main horizontal and vertical lines, and I already linked to the algorithm to get the centers of detected blobs, so combining that, and giving the intended dimensions of your rectangle in the program, you could figure it out. – Nyerguds May 13 '18 at 08:07