# How to go about obtaining coordinates of the pixel from annotated images?

**URL:** <https://forum.opencv.org/t/how-to-go-about-obtaining-coordinates-of-the-pixel-from-annotated-images/18924>\
**Category:** Python\
**Created:** [October 8, 2024, 6:37pm UTC](https://forum.opencv.org/t/how-to-go-about-obtaining-coordinates-of-the-pixel-from-annotated-images/18924 "2024-10-08T18:37:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fantastichornet](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@fantastichornet](https://forum.opencv.org/u/fantastichornet)\
**Post date:** [October 8, 2024, 6:37pm UTC](https://forum.opencv.org/t/how-to-go-about-obtaining-coordinates-of-the-pixel-from-annotated-images/18924/1 "2024-10-08T18:37:13Z")

</div>

![owl-2](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/f/fcc4e93a2674beeb1b29246a3041a711a6c9cc6f.png)

I’ve annotated some pictures and I want to find the coordinates of where the annotations occur. I want the coordinates of the pixel values of the pictures. I am new to Python/opencv and not sure what method I should look into.  
The image above is an example of what my annotations would look like. My actual pictures have better resolution and have the same dimensions. I used the RBG value (255, 0, 0) to annotate my images. I want my program to return the coordinates into a column in an excel.  
I’ve tried using the opencv subtract and absdiff methods but I don’t think it is doing what I need it to do.

---

<div class="post-metadata">

**Author:** ![sjhalayka](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sjhalayka/32/4700_2.png) [@sjhalayka](https://forum.opencv.org/u/sjhalayka)\
**Post date:** [October 8, 2024, 7:37pm UTC](https://forum.opencv.org/t/how-to-go-about-obtaining-coordinates-of-the-pixel-from-annotated-images/18924/2 "2024-10-08T19:37:31Z")

</div>

You’re right about being wrong.

Look into:  
inRange  
findContours  
moments

An example code is:

```auto
	vector<glm::vec3> loop_centres;
	vector<glm::vec3> loop_colours;

	for (size_t i = 0; i < distinct_colours.size(); i++)
	{
		Mat mask;

		inRange(input_light_mat,
			Scalar(static_cast<unsigned char>(distinct_colours[i].r * 255.0f), static_cast<unsigned char>(distinct_colours[i].g * 255.0f), static_cast<unsigned char>(distinct_colours[i].b * 255.0f), 255),
			Scalar(static_cast<unsigned char>(distinct_colours[i].r * 255.0f), static_cast<unsigned char>(distinct_colours[i].g * 255.0f), static_cast<unsigned char>(distinct_colours[i].b * 255.0f), 255),
			mask);

		vector<vector<Point> > loop_contours;
		vector<Vec4i> loop_hierarchy;
		findContours(mask, loop_contours, loop_hierarchy, RETR_LIST, CHAIN_APPROX_NONE);

		for (size_t j = 0; j < loop_contours.size(); j++)
		{
			cv::Moments M = cv::moments(loop_contours[j]);

			if (M.m00 == 0)
				continue;

			cv::Point2f loop_centre(M.m10 / M.m00, M.m01 / M.m00);

			Vec4b pixelValue = input_light_mat.at<Vec4b>(loop_centre.y, loop_centre.x);
			loop_centres.push_back(glm::vec3(loop_centre.x, loop_centre.y, 0));
			loop_colours.push_back(glm::vec3(pixelValue[0] / 255.0f, pixelValue[1] / 255.0f, pixelValue[2] / 255.0f));
		}
	}

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [October 8, 2024, 8:16pm UTC](https://forum.opencv.org/t/how-to-go-about-obtaining-coordinates-of-the-pixel-from-annotated-images/18924/3 "2024-10-08T20:16:49Z")

</div>

> [@fantastichornet](#):
>
> the coordinates

and what do you want to _do_ with that information? beyond stuffing it in an excel file, of course.

the answer to that question determines what you _actually need_. it may not be coordinates that you need.

---

<div class="post-metadata">

**Author:** ![fantastichornet](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@fantastichornet](https://forum.opencv.org/u/fantastichornet)\
**Post date:** [October 9, 2024, 6:05am UTC](https://forum.opencv.org/t/how-to-go-about-obtaining-coordinates-of-the-pixel-from-annotated-images/18924/4 "2024-10-09T06:05:51Z")

</div>

I am trying to do some image detection. So based on the annotated image, I want to get where the coordinates of object is in the original picture. I will use the numbers here for some other statistical analysis later on.
