What's the easiest way to find the coordinates of an object in a image?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















Imagine having an image of circles of different colors on a background of one color. What would be the easiest way to find the coordinates of the circles' centers (of course programmatically)?










share|improve this question























  • Do the circles overlap each other? Could they be concentric? Is your image JPEG or PNG or other? What OS do you use? What languages can you program in? What have you tried? Are the circles all just one single solid colour each? Are the circles fully contained in the image, or do some parts fall outside the background?

    – Mark Setchell
    Nov 23 '18 at 22:57











  • They don't overlap neither are concentric. The format of the images can be anything that helps the process. Both Windows and Linux. Only C and a tiny bit of Java, but I'm practicing Python right now. I'm a beginner working on a project that I came up with so except of Google-ing around I tried nothing. The circles are of solid colour and they never fall outside the image.

    – Böszörményi Dániel
    Nov 24 '18 at 12:05


















2















Imagine having an image of circles of different colors on a background of one color. What would be the easiest way to find the coordinates of the circles' centers (of course programmatically)?










share|improve this question























  • Do the circles overlap each other? Could they be concentric? Is your image JPEG or PNG or other? What OS do you use? What languages can you program in? What have you tried? Are the circles all just one single solid colour each? Are the circles fully contained in the image, or do some parts fall outside the background?

    – Mark Setchell
    Nov 23 '18 at 22:57











  • They don't overlap neither are concentric. The format of the images can be anything that helps the process. Both Windows and Linux. Only C and a tiny bit of Java, but I'm practicing Python right now. I'm a beginner working on a project that I came up with so except of Google-ing around I tried nothing. The circles are of solid colour and they never fall outside the image.

    – Böszörményi Dániel
    Nov 24 '18 at 12:05














2












2








2








Imagine having an image of circles of different colors on a background of one color. What would be the easiest way to find the coordinates of the circles' centers (of course programmatically)?










share|improve this question














Imagine having an image of circles of different colors on a background of one color. What would be the easiest way to find the coordinates of the circles' centers (of course programmatically)?







image api object coordinates analysis






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 21:09









Böszörményi DánielBöszörményi Dániel

132




132













  • Do the circles overlap each other? Could they be concentric? Is your image JPEG or PNG or other? What OS do you use? What languages can you program in? What have you tried? Are the circles all just one single solid colour each? Are the circles fully contained in the image, or do some parts fall outside the background?

    – Mark Setchell
    Nov 23 '18 at 22:57











  • They don't overlap neither are concentric. The format of the images can be anything that helps the process. Both Windows and Linux. Only C and a tiny bit of Java, but I'm practicing Python right now. I'm a beginner working on a project that I came up with so except of Google-ing around I tried nothing. The circles are of solid colour and they never fall outside the image.

    – Böszörményi Dániel
    Nov 24 '18 at 12:05



















  • Do the circles overlap each other? Could they be concentric? Is your image JPEG or PNG or other? What OS do you use? What languages can you program in? What have you tried? Are the circles all just one single solid colour each? Are the circles fully contained in the image, or do some parts fall outside the background?

    – Mark Setchell
    Nov 23 '18 at 22:57











  • They don't overlap neither are concentric. The format of the images can be anything that helps the process. Both Windows and Linux. Only C and a tiny bit of Java, but I'm practicing Python right now. I'm a beginner working on a project that I came up with so except of Google-ing around I tried nothing. The circles are of solid colour and they never fall outside the image.

    – Böszörményi Dániel
    Nov 24 '18 at 12:05

















Do the circles overlap each other? Could they be concentric? Is your image JPEG or PNG or other? What OS do you use? What languages can you program in? What have you tried? Are the circles all just one single solid colour each? Are the circles fully contained in the image, or do some parts fall outside the background?

– Mark Setchell
Nov 23 '18 at 22:57





Do the circles overlap each other? Could they be concentric? Is your image JPEG or PNG or other? What OS do you use? What languages can you program in? What have you tried? Are the circles all just one single solid colour each? Are the circles fully contained in the image, or do some parts fall outside the background?

– Mark Setchell
Nov 23 '18 at 22:57













They don't overlap neither are concentric. The format of the images can be anything that helps the process. Both Windows and Linux. Only C and a tiny bit of Java, but I'm practicing Python right now. I'm a beginner working on a project that I came up with so except of Google-ing around I tried nothing. The circles are of solid colour and they never fall outside the image.

– Böszörményi Dániel
Nov 24 '18 at 12:05





They don't overlap neither are concentric. The format of the images can be anything that helps the process. Both Windows and Linux. Only C and a tiny bit of Java, but I'm practicing Python right now. I'm a beginner working on a project that I came up with so except of Google-ing around I tried nothing. The circles are of solid colour and they never fall outside the image.

– Böszörményi Dániel
Nov 24 '18 at 12:05












2 Answers
2






active

oldest

votes


















0














I felt like doing it in Python with OpenCV as well, using the same starting image as my other answer.



enter image description here



The code looks like this:



#!/usr/bin/env python3

import numpy as np
import cv2

# Load image
im = cv2.imread('start.png')

# Convert to grayscale and threshold
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,1,255,0)

# Find contours, draw on image and save
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im, contours, -1, (0,255,0), 3)
cv2.imwrite('result.png',im)

# Show user what we found
for cnt in contours:
(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
print('Contour: centre {},{}, radius {}'.format(x,y,radius))


That gives this on the Terminal:



Contour: centre 400.0,200.0, radius 10
Contour: centre 500.0,200.0, radius 80
Contour: centre 200.0,150.0, radius 90
Contour: centre 50.0,50.0, radius 40


And this as the result image:



enter image description here






share|improve this answer
























  • That's actually both very detailed and useful! I'm very grateful for your answer, thank you very much!

    – Böszörményi Dániel
    Nov 26 '18 at 21:37



















1














There's a very easy way with ImageMagick which is free and installed on most Linux distros and is available for macOS and Windows - no programming required!



Let's start with this image:



enter image description here



Now you just run this in Terminal or Command Prompt:



magick input.png -define connected-components:verbose=true -connected-components 8 -auto-level output.png


Output



Objects (id: bounding-box centroid area mean-color):
0: 600x300+0+0 297.4,145.3 128391 srgb(0,0,0) <--- black background
2: 181x181+110+60 200.0,150.0 25741 srgb(0,0,255) <--- blue circle
3: 161x161+420+120 500.0,200.0 20353 srgb(255,0,255) <--- magenta circle
1: 81x81+10+10 50.0,50.0 5166 srgb(0,255,0) <--- green circle
4: 21x21+390+190 400.0,200.0 349 srgb(255,255,0). <--- yellow circle


I added the comments above after <---.



Looking at the blue circle, you can see its colour is srgb(0,0,255) which is blue and it measures 181x181 pixels - so its radius is 90 pixels. The top-left corner of the containing rectangle is at [110,60] so the centre is at [200,150], which matches the 200.00,150.00 given for the centroid.



Likewise, looking at the yellow circle, its colour is srgb(255,255,0) which is yellow. Its height and width are 21 pixels which means the radius is 10. The top-left corner of the containing square is at [390,190] which means the centre is at [400,200], matching the centroid given as 400.0,200.0.






share|improve this answer
























  • @fmw42 That's what I wrote, isn't it? That's what I meant anyway! Thank you.

    – Mark Setchell
    Nov 25 '18 at 18:52











  • I deleted my comment, Mark, when I saw you have asked the same thing. But for the OP's benefit, you could show how to extract only the colors and centroids excluding the black background as an addition to your answer.

    – fmw42
    Nov 25 '18 at 18:52














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452962%2fwhats-the-easiest-way-to-find-the-coordinates-of-an-object-in-a-image%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I felt like doing it in Python with OpenCV as well, using the same starting image as my other answer.



enter image description here



The code looks like this:



#!/usr/bin/env python3

import numpy as np
import cv2

# Load image
im = cv2.imread('start.png')

# Convert to grayscale and threshold
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,1,255,0)

# Find contours, draw on image and save
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im, contours, -1, (0,255,0), 3)
cv2.imwrite('result.png',im)

# Show user what we found
for cnt in contours:
(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
print('Contour: centre {},{}, radius {}'.format(x,y,radius))


That gives this on the Terminal:



Contour: centre 400.0,200.0, radius 10
Contour: centre 500.0,200.0, radius 80
Contour: centre 200.0,150.0, radius 90
Contour: centre 50.0,50.0, radius 40


And this as the result image:



enter image description here






share|improve this answer
























  • That's actually both very detailed and useful! I'm very grateful for your answer, thank you very much!

    – Böszörményi Dániel
    Nov 26 '18 at 21:37
















0














I felt like doing it in Python with OpenCV as well, using the same starting image as my other answer.



enter image description here



The code looks like this:



#!/usr/bin/env python3

import numpy as np
import cv2

# Load image
im = cv2.imread('start.png')

# Convert to grayscale and threshold
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,1,255,0)

# Find contours, draw on image and save
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im, contours, -1, (0,255,0), 3)
cv2.imwrite('result.png',im)

# Show user what we found
for cnt in contours:
(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
print('Contour: centre {},{}, radius {}'.format(x,y,radius))


That gives this on the Terminal:



Contour: centre 400.0,200.0, radius 10
Contour: centre 500.0,200.0, radius 80
Contour: centre 200.0,150.0, radius 90
Contour: centre 50.0,50.0, radius 40


And this as the result image:



enter image description here






share|improve this answer
























  • That's actually both very detailed and useful! I'm very grateful for your answer, thank you very much!

    – Böszörményi Dániel
    Nov 26 '18 at 21:37














0












0








0







I felt like doing it in Python with OpenCV as well, using the same starting image as my other answer.



enter image description here



The code looks like this:



#!/usr/bin/env python3

import numpy as np
import cv2

# Load image
im = cv2.imread('start.png')

# Convert to grayscale and threshold
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,1,255,0)

# Find contours, draw on image and save
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im, contours, -1, (0,255,0), 3)
cv2.imwrite('result.png',im)

# Show user what we found
for cnt in contours:
(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
print('Contour: centre {},{}, radius {}'.format(x,y,radius))


That gives this on the Terminal:



Contour: centre 400.0,200.0, radius 10
Contour: centre 500.0,200.0, radius 80
Contour: centre 200.0,150.0, radius 90
Contour: centre 50.0,50.0, radius 40


And this as the result image:



enter image description here






share|improve this answer













I felt like doing it in Python with OpenCV as well, using the same starting image as my other answer.



enter image description here



The code looks like this:



#!/usr/bin/env python3

import numpy as np
import cv2

# Load image
im = cv2.imread('start.png')

# Convert to grayscale and threshold
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,1,255,0)

# Find contours, draw on image and save
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im, contours, -1, (0,255,0), 3)
cv2.imwrite('result.png',im)

# Show user what we found
for cnt in contours:
(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
print('Contour: centre {},{}, radius {}'.format(x,y,radius))


That gives this on the Terminal:



Contour: centre 400.0,200.0, radius 10
Contour: centre 500.0,200.0, radius 80
Contour: centre 200.0,150.0, radius 90
Contour: centre 50.0,50.0, radius 40


And this as the result image:



enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 19:49









Mark SetchellMark Setchell

93.4k785191




93.4k785191













  • That's actually both very detailed and useful! I'm very grateful for your answer, thank you very much!

    – Böszörményi Dániel
    Nov 26 '18 at 21:37



















  • That's actually both very detailed and useful! I'm very grateful for your answer, thank you very much!

    – Böszörményi Dániel
    Nov 26 '18 at 21:37

















That's actually both very detailed and useful! I'm very grateful for your answer, thank you very much!

– Böszörményi Dániel
Nov 26 '18 at 21:37





That's actually both very detailed and useful! I'm very grateful for your answer, thank you very much!

– Böszörményi Dániel
Nov 26 '18 at 21:37













1














There's a very easy way with ImageMagick which is free and installed on most Linux distros and is available for macOS and Windows - no programming required!



Let's start with this image:



enter image description here



Now you just run this in Terminal or Command Prompt:



magick input.png -define connected-components:verbose=true -connected-components 8 -auto-level output.png


Output



Objects (id: bounding-box centroid area mean-color):
0: 600x300+0+0 297.4,145.3 128391 srgb(0,0,0) <--- black background
2: 181x181+110+60 200.0,150.0 25741 srgb(0,0,255) <--- blue circle
3: 161x161+420+120 500.0,200.0 20353 srgb(255,0,255) <--- magenta circle
1: 81x81+10+10 50.0,50.0 5166 srgb(0,255,0) <--- green circle
4: 21x21+390+190 400.0,200.0 349 srgb(255,255,0). <--- yellow circle


I added the comments above after <---.



Looking at the blue circle, you can see its colour is srgb(0,0,255) which is blue and it measures 181x181 pixels - so its radius is 90 pixels. The top-left corner of the containing rectangle is at [110,60] so the centre is at [200,150], which matches the 200.00,150.00 given for the centroid.



Likewise, looking at the yellow circle, its colour is srgb(255,255,0) which is yellow. Its height and width are 21 pixels which means the radius is 10. The top-left corner of the containing square is at [390,190] which means the centre is at [400,200], matching the centroid given as 400.0,200.0.






share|improve this answer
























  • @fmw42 That's what I wrote, isn't it? That's what I meant anyway! Thank you.

    – Mark Setchell
    Nov 25 '18 at 18:52











  • I deleted my comment, Mark, when I saw you have asked the same thing. But for the OP's benefit, you could show how to extract only the colors and centroids excluding the black background as an addition to your answer.

    – fmw42
    Nov 25 '18 at 18:52


















1














There's a very easy way with ImageMagick which is free and installed on most Linux distros and is available for macOS and Windows - no programming required!



Let's start with this image:



enter image description here



Now you just run this in Terminal or Command Prompt:



magick input.png -define connected-components:verbose=true -connected-components 8 -auto-level output.png


Output



Objects (id: bounding-box centroid area mean-color):
0: 600x300+0+0 297.4,145.3 128391 srgb(0,0,0) <--- black background
2: 181x181+110+60 200.0,150.0 25741 srgb(0,0,255) <--- blue circle
3: 161x161+420+120 500.0,200.0 20353 srgb(255,0,255) <--- magenta circle
1: 81x81+10+10 50.0,50.0 5166 srgb(0,255,0) <--- green circle
4: 21x21+390+190 400.0,200.0 349 srgb(255,255,0). <--- yellow circle


I added the comments above after <---.



Looking at the blue circle, you can see its colour is srgb(0,0,255) which is blue and it measures 181x181 pixels - so its radius is 90 pixels. The top-left corner of the containing rectangle is at [110,60] so the centre is at [200,150], which matches the 200.00,150.00 given for the centroid.



Likewise, looking at the yellow circle, its colour is srgb(255,255,0) which is yellow. Its height and width are 21 pixels which means the radius is 10. The top-left corner of the containing square is at [390,190] which means the centre is at [400,200], matching the centroid given as 400.0,200.0.






share|improve this answer
























  • @fmw42 That's what I wrote, isn't it? That's what I meant anyway! Thank you.

    – Mark Setchell
    Nov 25 '18 at 18:52











  • I deleted my comment, Mark, when I saw you have asked the same thing. But for the OP's benefit, you could show how to extract only the colors and centroids excluding the black background as an addition to your answer.

    – fmw42
    Nov 25 '18 at 18:52
















1












1








1







There's a very easy way with ImageMagick which is free and installed on most Linux distros and is available for macOS and Windows - no programming required!



Let's start with this image:



enter image description here



Now you just run this in Terminal or Command Prompt:



magick input.png -define connected-components:verbose=true -connected-components 8 -auto-level output.png


Output



Objects (id: bounding-box centroid area mean-color):
0: 600x300+0+0 297.4,145.3 128391 srgb(0,0,0) <--- black background
2: 181x181+110+60 200.0,150.0 25741 srgb(0,0,255) <--- blue circle
3: 161x161+420+120 500.0,200.0 20353 srgb(255,0,255) <--- magenta circle
1: 81x81+10+10 50.0,50.0 5166 srgb(0,255,0) <--- green circle
4: 21x21+390+190 400.0,200.0 349 srgb(255,255,0). <--- yellow circle


I added the comments above after <---.



Looking at the blue circle, you can see its colour is srgb(0,0,255) which is blue and it measures 181x181 pixels - so its radius is 90 pixels. The top-left corner of the containing rectangle is at [110,60] so the centre is at [200,150], which matches the 200.00,150.00 given for the centroid.



Likewise, looking at the yellow circle, its colour is srgb(255,255,0) which is yellow. Its height and width are 21 pixels which means the radius is 10. The top-left corner of the containing square is at [390,190] which means the centre is at [400,200], matching the centroid given as 400.0,200.0.






share|improve this answer













There's a very easy way with ImageMagick which is free and installed on most Linux distros and is available for macOS and Windows - no programming required!



Let's start with this image:



enter image description here



Now you just run this in Terminal or Command Prompt:



magick input.png -define connected-components:verbose=true -connected-components 8 -auto-level output.png


Output



Objects (id: bounding-box centroid area mean-color):
0: 600x300+0+0 297.4,145.3 128391 srgb(0,0,0) <--- black background
2: 181x181+110+60 200.0,150.0 25741 srgb(0,0,255) <--- blue circle
3: 161x161+420+120 500.0,200.0 20353 srgb(255,0,255) <--- magenta circle
1: 81x81+10+10 50.0,50.0 5166 srgb(0,255,0) <--- green circle
4: 21x21+390+190 400.0,200.0 349 srgb(255,255,0). <--- yellow circle


I added the comments above after <---.



Looking at the blue circle, you can see its colour is srgb(0,0,255) which is blue and it measures 181x181 pixels - so its radius is 90 pixels. The top-left corner of the containing rectangle is at [110,60] so the centre is at [200,150], which matches the 200.00,150.00 given for the centroid.



Likewise, looking at the yellow circle, its colour is srgb(255,255,0) which is yellow. Its height and width are 21 pixels which means the radius is 10. The top-left corner of the containing square is at [390,190] which means the centre is at [400,200], matching the centroid given as 400.0,200.0.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 17:15









Mark SetchellMark Setchell

93.4k785191




93.4k785191













  • @fmw42 That's what I wrote, isn't it? That's what I meant anyway! Thank you.

    – Mark Setchell
    Nov 25 '18 at 18:52











  • I deleted my comment, Mark, when I saw you have asked the same thing. But for the OP's benefit, you could show how to extract only the colors and centroids excluding the black background as an addition to your answer.

    – fmw42
    Nov 25 '18 at 18:52





















  • @fmw42 That's what I wrote, isn't it? That's what I meant anyway! Thank you.

    – Mark Setchell
    Nov 25 '18 at 18:52











  • I deleted my comment, Mark, when I saw you have asked the same thing. But for the OP's benefit, you could show how to extract only the colors and centroids excluding the black background as an addition to your answer.

    – fmw42
    Nov 25 '18 at 18:52



















@fmw42 That's what I wrote, isn't it? That's what I meant anyway! Thank you.

– Mark Setchell
Nov 25 '18 at 18:52





@fmw42 That's what I wrote, isn't it? That's what I meant anyway! Thank you.

– Mark Setchell
Nov 25 '18 at 18:52













I deleted my comment, Mark, when I saw you have asked the same thing. But for the OP's benefit, you could show how to extract only the colors and centroids excluding the black background as an addition to your answer.

– fmw42
Nov 25 '18 at 18:52







I deleted my comment, Mark, when I saw you have asked the same thing. But for the OP's benefit, you could show how to extract only the colors and centroids excluding the black background as an addition to your answer.

– fmw42
Nov 25 '18 at 18:52




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452962%2fwhats-the-easiest-way-to-find-the-coordinates-of-an-object-in-a-image%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud