简体   繁体   English

根据像素颜色的变化定位坐标

[英]Locate the coordinate based on change in pixel color

I am trying to locate specific coordinates in an image.我正在尝试在图像中定位特定坐标。 I have an image that contains only 2 colours, pink and black as shown in the image.我有一张只包含 2 种颜色的图像,如图所示,粉色和黑色。 If I know an (x,y) coordinate in the pink region (marked in yellow dot at the centre) how can I find the coordinates that are in the boundary of the pink region(as shown in yellow dots at the boundary).如果我知道粉红色区域中的 (x,y) 坐标(在中心用黄点标记),我如何找到粉红色区域边界中的坐标(如边界处的黄点所示)。

在此处输入图像描述

NB: The yellow dots are not part of the image and I'm using this just to represent the region of interest.注意:黄点不是图像的一部分,我只是用它来表示感兴趣的区域。

I just want to know whether there is any fast and better approach for doing this other than nested for loops which may really slow down the process because I've to find the boundary coordinates in multiple regions of the image.我只是想知道除了嵌套的 for 循环之外是否有任何更快更好的方法来执行此操作,这可能会真正减慢过程,因为我必须在图像的多个区域中找到边界坐标。

Thank you!谢谢!

Here is one way using Python/OpenCV and Numpy.这是使用 Python/OpenCV 和 Numpy 的一种方法。

  • Read the input读取输入
  • Convert to gray转换为灰色
  • Otsu threshold大津阈值
  • Crop the row containing the center裁剪包含中心的行
  • Get all the coordinates in the row that are white获取行中所有白色的坐标
  • Print the first and last coordinate打印第一个和最后一个坐标
  • Draw line on input在输入上画线
  • Save results保存结果

Input:输入:

在此处输入图像描述

import cv2
import numpy as np

# load image
img = cv2.imread("pink_blob.png")
hh, ww = img.shape[:2]

# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

# center coords
center = (115,82)
cy = center[1]

# crop row at y center
row = thresh[cy:cy+1, 0:ww]

# get coordinates along row where it is white
# swap x and y between numpy and opencv coords
coords = np.argwhere(row==255)
num_coords = len(coords)
start = coords[0]
end = coords[num_coords-1]
start_pt = (start[1],cy)
end_pt = (end[1],cy)

print(start_pt)
print(end_pt)

# draw line from start to end coordinates on input
result = img.copy()
cv2.line(result, start_pt, end_pt, (255,255,255), 1)

# save result
cv2.imwrite('pink_blob_line.png', result)

# show result
cv2.imshow('result', result)
cv2.waitKey(0)

Start and End Coordinates:

(67, 82)
(160, 82)

Line on input image:输入图像上的线:

在此处输入图像描述

First of all, find contour of the pink region in the image.首先,找到图像中粉色区域的轮廓。 You can do this by first applying Otsu's thresholding on the image and then find contours using cv2.findContours().您可以通过首先在图像上应用 Otsu 的阈值处理然后使用 cv2.findContours() 找到轮廓来做到这一点。

Then in the contour boundary points, find the points having the same y-coordinate as that of the center pixel.然后在轮廓边界点中,找到与中心像素y坐标相同的点。

Among these points, point with the maximum x-coordinate will be the point on the right and point with minimum x-coordinate will be the point on the left.在这些点中,x 坐标最大的点是右边的点,x 坐标最小的点是左边的点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM